How to change the default redirection url of a Laravel Auth filter?

when using beforeFilter (to check user login state) in Controller __construct

$this->beforeFilter('auth', array('except'  => array('login','handleLogin'));

it automatically redirects guests to

www.example.com/login

how to set redirect URL for:

www.example.com/user/login
+4
source share
4 answers

In yours app/filters.phpyou can find something like this:

Route::filter('auth', function($route, $request)
{
    if (Auth::guest()) return Redirect::guest('login');
});

You need to change return Redirect::guest('login')to the following:

return Redirect::guest('user/login');
+4
source

Laravel 5.2

If you are using Laravel 5.2, you need to open Authenticate.phpin your folder app/Http/Middlewareand change the function parameters guestto the URL path to which you want to direct them:

return redirect()->guest('auth/login')

(Line 24 since writing on a clean install)

Laravel 5.0

Laravel 5.0, RedirectIfAuthenticated.php Http/Middleware.

:

return new RedirectResponse(url('/home'));

. , :

return new RedirectResponse(url('/'));

+2

Laravel 5. *

/Http//Authenticate.php

return redirect()->guest('auth/login');

to

return redirect()->guest('login');

0

. mysite.com/login mysite/admin/login, , , ( /login ), - login.blade.php (Laravel 5.3):

                <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">

A little oversight by developers who, despite all this wired cleverness, were still stupid enough to hardcode this URL into a presentation template ... This line should really indicate the "postlogin" named route in Routes.php, that is:

                <form class="form-horizontal" role="form" method="POST" action="{{ route('postlogin') }}">

Now your form action attribute will be bound to any URL specified for the route named postloginin Routes.php, i.e.:

    Route::post('login', ['as' => 'postlogin',  'uses' => 'AuthController@login'         ]);     
0
source

Source: https://habr.com/ru/post/1541329/


All Articles