本文介绍了CakePHP - 使用'admin_'前缀路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我目前使用蛋糕的路由配置为了设置管理视图,不同于非管理员用户的。我读了文档的路由章节(蛋糕的),并且偶然的前缀路由。我认为这是我需要使用,以完成我需要的东西。所以我开始设置config / core.php建议,并取消注释 配置:: write .prefixes',array('admin'));然后,我在routes.php中添加了一个路由: Router :: connect('/ admin',array('controller'=>'donor','action'=>'index','admin' => true)); 从我的理解,使用上面的设置,我可以为管理员定义一个具体的操作,例如:admin_index或admin_view等。 所以我的AppController有一个组件集如下: public $ components = array('DebugKit.Toolbar','Session','Auth'=> array('loginRedirect '=> array('controller'=>'donor','action'=>'index'),'authError' Access Denied','logoutRedirect'=> array('controller'=>'users','action'=>'login'),'authorize'=> array('Controller'))); 所以当非管理员用户登录时,他应该被重定向到'donor / index'当管理员登录我想将他重定向到'donor / admin_index'..我该怎么做? 我试过: public function beforeFilter b $ b if(isset($ this-> params ['admin'])){ $ this-> layout ='stafflayout'; $ this-> Auth-> loginRedirect = array('controller'=>'donor','action'=>'index',' prefix'=>'admin','admin'=> true ); } 在测试过程中,乍一看, 。但URL不会改变像'donor / admin_index ..和仍然被重定向到捐助者/索引或等效,只是/捐助者...为什么这不工作? (seconndary问题)在测试过程中,我将Auth组件LoginRedirect的控制器和操作更改为 'controller'=>'posts' 和 'action'=>'index' 捐赠者,索引,当我登录时,我仍然被重定向到捐助者/索引..是否应该将我重定向到帖子/索引 任何人都可以帮助我解决这两个问题?主要问题虽然更重要! 解决方案好的代码很好! code> Router :: connect('/ admin',array('controller'=>'donor','action'=>'index','admin'=> true)) ; $ / $ c $ b 上面的代码会在/ url中写入/ > 现在,如果你想添加前缀/ donor / admin_index,那么你必须再创建一个规则,例如: Router :: connect('/ donors / admin_index',array('controller'=>'donor','action'=>'index','admin' => true)); 和beforeFilter函数 if(isset($ this-> params ['admin'])){ $ this-> layout ='stafflayout'; $ this-> Auth-> loginRedirect = array('controller'=>'donor','action'=>'admin_index',' admin'=> true ); 上述代码将重定向到/ donor / admin_index,路由将渲染/ p> I am currently using cake's routes config in order to set up admin views, different from that of the non-admin user. I read the routing chapter of the documentation(cake's), and stumbled upon the prefix routing. Which I thought that it is something I need to use, to accomplish what I need. So I started it with setting up the config/core.php as suggested, and uncommented this Configure::write('Routing.prefixes', array('admin'));Then, I added a route in the routes.php :Router::connect('/admin', array('controller' => 'donors', 'action' => 'index', 'admin' => true));From what I understood, with the above set, I can define a specific action for the admin, names like : admin_index or admin_view, etc. .So my AppController has a component set like this : public $components = array( 'DebugKit.Toolbar', 'Session', 'Auth' => array( 'loginRedirect' => array( 'controller' => 'donors', 'action' => 'index' ), 'authError' => 'Access Denied', 'logoutRedirect' => array( 'controller' => 'users', 'action' => 'login' ), 'authorize' => array('Controller') ) );So when a non-admin user logs in he should be redirected to 'donors/index', and when the admin logs in I want to redirect him to 'donors/admin_index'.. How can i do this ?I tried this :public function beforeFilter(){ if(isset($this->params['admin'])){ $this->layout = 'stafflayout'; $this->Auth->loginRedirect = array( 'controller'=>'donors', 'action'=>'index', 'prefix'=>'admin', 'admin'=>true ); }And in the process of testing it out, at first glance I though it worked. but the URL does not change like 'donor/admin_index .. and am still being redirected to donors/index or equivalent, simply to /donors... Why is this not working ?(seconndary question)Also during the process of testing this out, I changed my the controller and actions of the Auth component LoginRedirect to 'controller'=>'posts'and 'action'=>'index'other then 'donors', 'index', and when I logged in, I still got redirected to donors/index.. were it should have redirected me to 'posts/index'Anyone can help me on these two issues? Primary questions is more important though! 解决方案 Well the code is fine!Router::connect('/admin', array('controller' => 'donors','action' => 'index', 'admin' => true));the above will render /donors/index page whenever /admin is written in the url.Now if you want to add prefix like /donors/admin_index then you have to create one more rule such as: Router::connect('/donors/admin_index', array('controller' => 'donors','action' => 'index', 'admin' => true));and in beforeFilter functionif(isset($this->params['admin'])){ $this->layout = 'stafflayout'; $this->Auth->loginRedirect = array( 'controller'=>'donors', 'action'=>'admin_index', 'admin'=>true );the above code will redirect to /donors/admin_index and routing will render /donors/index page 这篇关于CakePHP - 使用'admin_'前缀路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 14:31