PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] What is the alternative to Route::filter in Laravel 8?

 January 28, 2022     laravel, php, routes     No comments   

Issue

I would like to require login for all pages, except /index and /login. I tried to solve it like this:

Route::filter('pattern: ^(index|login)*', 'auth');

In laravel 8 Route::filter is not available anymore. How to solve this issue, without putting every route definition into a large group?


Solution

Create a middleware group in your routes/web.php file.

Route::middleware(['auth'])->group( function () {

    // Your protected routes here

});

Alternatively you can make middleware exceptions for routes:

// In your auth middleware that extends Authenticate

...

/**
 * Routes that should skip handle.
 *
 * @var array $except
 */
protected array $except;


public function __construct(){
    $this->except = [
        route('index'),
        route('login')
    ];
}


/**
 * @param \Illuminate\Http\Request $request
 */
public function handle( Request $request )
{
    $current_route = $request->route();

    if( in_array($current_route, $this->except ) {
        // Route don't needs to be guarded
    }

}

...

If you don't want to have large groups then you can specify the middleware per route (can look messy)

Route::get('/route', [FooController::class, 'index'])->middleware('auth');


Answered By - Samuel Ferdary
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing