[英]Laravel 4 redirect issue with login page
I am using laravel 4 and here is my AdminController file : 我正在使用laravel 4,这是我的AdminController文件:
class AdminController extends BaseController {
protected $layout = "admin.layout";
public function __construct() {
// security for the forms and access
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('auth.admin' , array('except' =>array('getIndex','postSignin')));
// using this one to display user value if login and is admin
if (Auth::check() && Auth::user()->isAdmin()){
$this->user = Auth::getUser();
View::share('user', $this->user);
}
}
// main admin page
public function getIndex(){
$this->layout->content = View::make('admin.login');
}
// get the dashboard page
public function getDashboard() {
$this->layout->content = View::make('admin.dashboard');
}
// missing pages all redirect to dashboard if user is logged in.
public function missingMethod($parameters = array()){
if (Auth::check() && Auth::user()->isAdmin())
$this->getDashboard();
else
$this->getIndex();
}
Here is my filters.php file : 这是我的filters.php文件:
Route::filter('auth.admin', function()
{
if(!Auth::check() && !(Auth::user()->isAdmin())){
return Redirect::guest('admin');
}
});
in my routes.php file I am doing this: 在我的routes.php文件中,我正在这样做:
Route::controller('admin', 'AdminController');
here is what I want if you could help me :_ 如果您可以帮助我,这就是我想要的:_
1) . 1)。 I want to clean up my code where there is not that much checking for if user is logged and isAdmin.
我想在没有太多检查用户是否已登录和isAdmin的情况下清理代码。
2). 2)。 right now if you are logged in and you go to "admin/" , it will show you the login page ?
现在,如果您已登录并转到“ admin /”,它将显示登录页面? how could I fix it in an effective way.
我怎样才能有效地解决它。
3). 3)。 also if you are not logged in and you go to "admin/dashboard" it will show you dashboard content ?
另外,如果您尚未登录并且转到“ admin / dashboard”,它将显示您的仪表板内容? how to fix
怎么修
Thank you in advance for all your help :) 预先感谢您的所有帮助:)
You can use route groups and use a single filter to validate them 您可以使用路由组并使用单个过滤器对其进行验证
Check the docs 检查文档
http://laravel.com/docs/routing#route-groups http://laravel.com/docs/routing#route-groups
Add this in your routes.php
file: 将此添加到您的
routes.php
文件中:
Route::group(array('before' => 'auth.admin'), function() {
Route::controller('admin', 'AdminController');
})
Declare filter in filters.php
file: 在
filters.php
文件中声明过滤器:
Route::filter('auth.admin', function(){
// Assumed you have a '/login' url
if (Auth::guest() || (Auth::check() && !Auth::user()->isAdmin())) {
return Redirect::guest('login');
}
});
Also make sure you have the user()->isAdmin()
method in your User
model that you are using and it checks whether the user is an admin
or not and returns a Boolean
value, TRUE
if the user is an admin otherwise FALSE
. 还要确保您正在使用的
User
模型中具有user()->isAdmin()
方法,并且该方法将检查用户是否为admin
并返回Boolean
值,如果用户为admin则返回TRUE
,否则返回FALSE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.