【问题标题】:Laravel 5 - Sorry, the page you are looking for could not be foundLaravel 5 - 抱歉,找不到您要查找的页面
【发布时间】:2017-05-15 15:56:37
【问题描述】:

我试图在登录后将Laravel 页面重定向到另一个主页,但它每次都将我重定向到home。我所做的是:我将protected $redirectTo = 的值从home 更改为protected $redirectTo = 'S1CheckUserTables';
我还在web.php 中定义了我的路线如下(我相信我会使用命名路线):

Route::get('/S1CheckUserTables', ['as'=>'S1CheckUserTables', 'uses'=>'S1CheckUserTablesController@index']);

我也尝试使用以下语法来解决此问题,但也没有用:

Route::get('S1CheckUserTables', 'S1CheckUserTablesController@index')->name('S1CheckUserTables');

您能告诉我什么可以解决我的问题吗?非常感谢您。

【问题讨论】:

    标签: laravel redirect login routing


    【解决方案1】:

    laravel 登录后的默认重定向是转到 LoginController 中设置的 /home:

    use AuthenticatesUsers;
    
        /**
         * Where to redirect users after login.
         *
         * @var string
         */
        protected $redirectTo = '/home';
    

    并且有默认的中间件 RedirectIfAuthenticated

    class RedirectIfAuthenticated
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string|null  $guard
         * @return mixed
         */
        public function handle($request, Closure $next, $guard = null)
        {
            if (Auth::guard($guard)->check()) {
                return redirect('/home');
            }
    
            return $next($request);
        }
    }
    

    在 app/Http/Controllers/Auth/RegisterController.php 中

    class RegisterController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Register Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles the registration of new users as well as their
        | validation and creation. By default this controller uses a trait to
        | provide this functionality without requiring any additional code.
        |
        */
    
        use RegistersUsers;
    
        /**
         * Where to redirect users after login / registration.
         *
         * @var string
         */
        protected $redirectTo = '/home';
    

    这就是你需要做出改变才能按照自己的方式工作的地方......

    【讨论】:

    • 非常感谢您的回复,但它仍然重定向到home。除了改变你所说的我应该做的事情之外,还有什么别的吗?我正在使用make:auth
    • 没有。只有这两个:中间件 RedirectIfAuthenticated 和 LoginController
    • 根据下面的页面,我更改了所有三个值,但仍然有同样的问题。是不是我的路由有问题?或者我不应该以某种方式刷新或重置数据库还是......? laravel.com/docs/5.3/authentication#included-authenticating
    • 你有自己创建的中间件吗?
    • 不,实际上我还应该更改App\Http\Controllers\Auth\RegisterController 中的值,以使重定向发生在我想要的位置(注册成功后)。非常非常感谢。我学到了很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2016-11-15
    • 2018-07-31
    • 2019-05-19
    • 2017-12-26
    相关资源
    最近更新 更多