How to change default redirect URL of Laravel 5 Au

2019-01-16 16:41发布

By default if I am not logged and I try visit this in browser:

http://localhost:8000/home

It redirect me to http://localhost:8000/auth/login

How can I change to redirect me to http://localhost:8000/login

标签: php laravel-5
10条回答
成全新的幸福
2楼-- · 2019-01-16 17:27

Since your other question was marked as duplicate..I will try to answer it here..

First you need to change your route like

<?php
Route::get(config('constants.cms_path') . '/login', [
    'as' => 'login',
    'uses' => 'Auth\AuthController@getLogin'
]);

In your blade..make sure you use named route in the Login url link like

{{ route('login') }}

In Middleware/Authenticate.php change the redirect guest to

return redirect()->guest(config('constants.cms_path') . '/login');
查看更多
Ridiculous、
3楼-- · 2019-01-16 17:27

I wanted to do the same thing in Laravel 5.5. Handling authentication has moved to Illuminate\Auth\Middleware\Authenticate which throws an Illuminate\Auth\AuthenticationException.

That exception is handled in Illuminate\Foundation\Exceptions\Hander.php, but you don't want to change the original vendor files, so you can overwrite it with your own project files by adding it to App\Exceptions\Handler.php.

To do this, add the following to the top of the Handler class in App\Exceptions\Handler.php:

use Illuminate\Auth\AuthenticationException;

And then add the following method, editing as necessary:

/**
 * Convert an authentication exception into an unauthenticated response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Auth\AuthenticationException  $exception
 * @return \Illuminate\Http\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('login'); //<----- Change this
}

Just change return redirect()->guest('login'); to return redirect()->guest(route('auth.login')); or anything else.

I wanted to write this down because it took me more than 5 minutes to figure it out. Please drop me a line if you happened to find this in the docs because I couldn't.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-16 17:31

This is Laravel 5.4 Solution:

There is a new unauthenticated() method in app/Exceptions/Handler.php which handles unauthenticated users and redirects to login path.

So change

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

to

return redirect()->guest('auth/login');
查看更多
够拽才男人
5楼-- · 2019-01-16 17:32

Just to extend @ultimate's answer:

  1. You need to modify App\Http\Middleware\Authenticate::handle() method and change auth/login to /login.
  2. Than you need to add $loginPath property to your \App\Http\Controllers\Auth\AuthController class. Why? See Laravel source.

In result you'll have this in your middleware:

namespace App\Http\Middleware;
class Authenticate {
        /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('/login'); // <--- note this
            }
        }

        return $next($request);
    }
}

And this in your AuthController:

namespace App\Http\Controllers\Auth;
class AuthController extends Controller
{
    protected $loginPath = '/login'; // <--- note this

    // ... other properties, constructor, traits, etc 
}
查看更多
爷的心禁止访问
6楼-- · 2019-01-16 17:33

EDIT: On Laravel 5.1, simply add protected $redirectPath = '/url/you/want'; to AuthController would do the trick.

REFER : http://laravel.com/docs/5.1/authentication#included-authenticating


On Laravel 5.1, it is completely moved to another middleware named RedirectIfAuthenticated.php under App\Http\Middleware

public function handle($request, Closure $next)

{
    if ($this->auth->check()) {
        return redirect('/'); //change this part to anywhere you wish to be redirected to
    }

    return $next($request);
}

Hope it helps.

查看更多
一夜七次
7楼-- · 2019-01-16 17:33

Authentication checks are made using middleware in Laravel 5.

And the middleware for auth is App\Http\Middleware\Authenticate.

So, you can change it in handle method of the middleware.

查看更多
登录 后发表回答