Friday, March 4, 2022

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

Issue

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");
     }


 }


Answered By - apokryfos

No comments:

Post a Comment

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