如果登录,Laravel 在访问登录页面时重定向到用户仪表板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28252461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel redirect to user dashboard when visiting login page if logged in
提问by imperium2335
I have a basic login system setup, but I would like the user to be sent to there dashboard page if they try to access the "login" page or "create account" page.
我有一个基本的登录系统设置,但我希望用户在尝试访问“登录”页面或“创建帐户”页面时被发送到仪表板页面。
How do I go about doing this?
我该怎么做?
I am thinking something in the routes file?:
我在考虑路由文件中的某些内容?:
Route::post('/login', array('uses' => 'UserController@login'));
Route::post('/create-account', array('uses' => 'UserController@createAccount'));
Route::group(array('before' => 'auth'), function () {
Route::get('/dashboard', array('uses' => 'DashboardController@index'));
Route::get('/logout', function () {
Auth::logout();
return Redirect::to('/start');
});
});
Perhaps some kind of group around the first two routes?
也许前两条路线周围有某种团体?
回答by lukasgeiter
A before filter is perfect for this. Since it basically will do the opposite of auth
let's call it no_auth
:
一个前置过滤器非常适合这个。因为它基本上与auth
我们称之为相反no_auth
:
Route::filter('no_auth', function(){
if(Auth::check()){
return Redirect::to('dashboard');
}
}
And then wrap a group around your two routes to apply the filter:
然后围绕您的两条路线包裹一组以应用过滤器:
Route::group(array('before' => 'no_auth'), function(){
Route::post('/login', array('uses' => 'UserController@login'));
Route::post('/create-account', array('uses' => 'UserController@createAccount'));
});
Edit
编辑
Actually, as @afarazitpoints out, there's already a filter like that in app/filters.php
called guest
. You just have to change the redirect URL to dashboard
and you're ready to go.
实际上,正如@afarazit指出的那样,在app/filters.php
调用中已经有一个这样的过滤器guest
。您只需将重定向 URL 更改dashboard
为 即可。
回答by afarazit
回答by Emeka Mbah
There are many ways to do this. You can use an Event listener like so:
有很多方法可以做到这一点。您可以像这样使用事件侦听器:
Event::listen('user.login', function (){
if(Auth::check()){
return Redirect::to('dashboard');
}
});
Event::listen('user.create', function (){
if(Auth::check()){
return Redirect::to('dashboard');
}
});
You need a named controller for above like so:
您需要一个命名控制器,如下所示:
Route::post('/login', array(
'uses' => 'UserController@login',
'as' => 'user.login'
));
Route::post('/create-account', array(
'uses' => 'UserController@createAccount',
'as' => 'user.create'
));
回答by mburakergenc
You may use a constructor and include a filter in it. Here is an example; You can modify your code according to the example.
您可以使用构造函数并在其中包含过滤器。这是一个例子;您可以根据示例修改您的代码。
public function __construct(SignInForm $signInForm)
{
$this->signInForm = $signInForm;
$this->beforeFilter('guest', ['except' => 'destroy']);
}
回答by Soorajlal K G
If laravel version is 4.2
如果 Laravel 版本是 4.2
open your app/filters.phpand add
打开你的app/filters.php并添加
Route::filter('no_auth', function(){
if(Auth::check()){
return Redirect::to('dashboard');
}
});
add your login and create account pages to your app/routes.phplike below:
将您的登录名并创建帐户页面添加到您的app/routes.php,如下所示:
Route::group(array('before' => 'no_auth'), function(){
Route::post('/login', array('uses' => 'UserController@login'));
Route::post('/create-account', array('uses' => 'UserController@createAccount'));
});
It worked for me.
它对我有用。