Routes.php中的条件


Conditions In Routes.php Laravel

对不起,我是Laravel的新手。我想在Routes.php中使用laravel 5.2当用户登录时,我想调用showExperience函数,否则重定向到login

My routes.php代码

Route::get('/profile', function () {
if (Auth::check()) {
    $url = action('ProfilesController@showExperience');
    return redirect($url);
} else {
    return Redirect::to('login');
}
});

这里是My ProfilesController.php

public function showExperience(){
       $data = Experience::all();
    return view('profile')->with('experienceData',$data);
}

您应该为此目的使用中间件

例如:

Route::get('/profile', ['middleware' => 'auth', 'uses' => 'YourController@index']);

然后您可以到app/Http/Middleware/Authenticate.phpapp/Http/Middleware/RedirectIfAuthenticated.php更改默认的重定向值

这是我的解决方案

Route::get('/profile',[
                        'uses' => 'ProfilesController@showExperience',
                        'as' => 'profile',
                        'middleware' => 'auth'
                              ]);