[英]laravel 5.2 how to protect route only for authenticated user
i'm new to laravel, i had installed auth scaffold, here is the route up to now. 我是laravel的新手,我已经安装了auth支架,这是到现在的路线。
Route::group(['middleware' => ['web']], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
Route::get('addthreadhtml', function()
{
return View::make('addThreadForm');
});
Route::post('thread/add', 'ThreadController@addthread');
Route::get('thread/showall', 'ThreadController@showallthread');
});
i want to protect addthreadhtml from non-authenticated user access, if the user don't login, they will be redirected to another view. 我想保护addthreadhtml免受未经身份验证的用户访问,如果用户不登录,他们将被重定向到另一个视图。
How can i do that? 我怎样才能做到这一点?
Add the auth middleware to the route you want to protect: 将auth中间件添加到您要保护的路由中:
Route::get('addthreadhtml', ['middleware' => 'auth', function () {
return View::make('addThreadForm');
}]);
if you want the auth for the function only, you can put this on the top code 如果您只想要功能的身份验证,则可以将其放在顶部代码上
if(!Auth::user('id')){
//redirect to any view not require auth
}
or this code 或此代码
if (Auth::check()) {
// The user is logged in...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.