Search code examples
phplaravellaravel-5.3middleware

Laravel - Middleware How to change route to our desired page other than Login


I m working on fresh laravel project.

I had install the laravel framework through composer then I created a route for testing purpose like this:

Route::get('/', function () {
return view('pages.home');
 });

This worked fine and I got the desired page. Now for understanding middleware I added this line of code:

Route::get('/', function () {
return view('pages.home');
 })->middleware('auth');

Now its throwing error of

Route [login] not defined.

What I know that, it throwing this error because I havent install any voyagers package thats why it not finding 'login' route.

But My question is How can I change that Route [login] to my desired page like Route [pages.notauth].

Please help me for this.


Solution

  • First run php artisan make:auth to make the Laravel auth boilerplate. Then in the LoginController add the the following:

    class LoginController extends Controller {
        use AuthenticatesUsers;
        protected $redirectTo = '/home';
    
        public function __construct()
        {
            $this->middleware('guest')->except('logout');
        }
     
        public function showLoginForm() {
               return view("pages.notlogin");
         }
    
    
     }
    

    Update

    The php artisan make:auth command is no longer available in the base Laravel installation. Since Laravel 6 to get a roughly equivalent effect you need to (a) install laravel/ui and (b) run:

    php artisan ui bootstrap --auth
    

    You can also use react or vue instead of bootstrap depending what you want to use in your UI.