CakePHP动态路由配置,可以吗?或只是一个梦想? [英] CakePHP Dynamic Routes Configurations, is it POSSIBLE?? or just a DREAM?
问题描述
我只是想知道和好奇如何在cakephp中实现动态路由配置,或者像我这样创建两条路由:
Im just wondering and curious about how to achieve dynamic routes configuration in cakephp, or as such that I can create two routes like so:
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard'));
每次用户访问我的网站时都不会出现错误。我想要做的是在用户未登录时将'/'设置为默认着陆页,但是如果用户登录并且Auth会话存在,则我想将该URL设置为'/'但是指向用户的仪表板。
without trigerring the error everytime a user go to my site. What I want to do is to set '/' as my default landing page when user is not logged in, but in other way if the user is logged in and Auth session is present, i would like to set the url to '/' but pointing to user's dashboard.
我认为在routes.php中导入会话将会工作,但不是我的预期方式:
What I thought was importing session in routes.php will work but it was not the way I expected:
App::import('Session', 'Component');
$this->Session = new SessionComponent;
if($this->Session->check('Auth.User')) {
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard'));
} else {
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
}
任何帮助都非常感谢,我确信我们中许多人还在等待回答。
非常感谢您提前。
祝你节日快乐。
Any help is greatly appreciated, im sure many of us are waiting also for the answer. Thank you very much in advance. And wishing u a Happy Holidays.
推荐答案
我只需在控制器级别切换。将您的 /
路线指向 UsersController :: home
,在那里:
I would simply switch this on the controller level. Point your /
route to UsersController::home
, in there do:
function home() {
if ($this->Auth->user()) {
$this->dashboard();
} else {
$this->login();
}
}
function dashboard() {
$this->render('dashboard');
}
function login() {
$this->render('login');
}
这篇关于CakePHP动态路由配置,可以吗?或只是一个梦想?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!