Cakephp管理和用户登录分开
我想做一个应用程序,用户可以正常登录,后端管理员可以登录 到目前为止,我创建了以下内容: routes.phpCakephp管理和用户登录分开,php,cakephp,Php,Cakephp,我想做一个应用程序,用户可以正常登录,后端管理员可以登录 到目前为止,我创建了以下内容: routes.php $prefix = 'admin'; Router::connect( "/{$prefix}/:plugin/:controller", array('action' => 'index', 'prefix' => $prefix, $prefix => true) ); Router::connect( "/{$prefix}/:plug
$prefix = 'admin';
Router::connect(
"/{$prefix}/:plugin/:controller",
array('action' => 'index', 'prefix' => $prefix, $prefix => true)
);
Router::connect(
"/{$prefix}/:plugin/:controller/:action/*",
array('prefix' => $prefix, $prefix => true)
);
Router::connect(
"/{$prefix}/:controller",
array('action' => 'index', 'prefix' => $prefix, $prefix => true)
);
Router::connect(
"/{$prefix}/:controller/:action/*",
array('prefix' => $prefix, $prefix => true)
);
AppController:
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'display'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authorize' => 'Controller',
'authError' => 'Access denied! Did you really think that you can access that?'
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow('display');
//$this->recordActivity();
if($this->request->prefix == 'admin'){
$this->layout = 'admin';
}
}
当我尝试访问前端需要auth的页面时,它会给我login()操作,但当尝试访问/admin时,它会将我重定向到/users/login
我想有两个独立的登录系统与不同的布局。一个用于普通用户,一个用于管理员用户
有人能帮忙吗?我不建议仅仅为了不同的观点而采取两种
login()
操作。您可以将if语句从beforeFilter()
移动到userscocontroller::login()
,以正确设置布局。但是,如果确实要继续执行单独的操作,请在AppController::beforeFilter()
中设置AuthComponent::loginAction
属性,如下所示:
if($this->request->prefix == 'admin'){
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login', 'plugin' => false);
}
其中admin_登录将是您在UsersController中创建的另一个操作
另一方面,我建议使用cake的默认前缀路由,如中所述。这与您所做的非常相似,但您不必手动创建路由。此外,如文中所述,要访问/管理,您需要定义如下路由:
Router::connect(
'/admin',
array('controller' => 'pages', 'action' => 'index', 'admin' => true)
);