CakePHP - Routing Using 'admin_' Prefix

匿名 (未验证) 提交于 2019-12-03 09:13:36

问题:

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!

回答1:

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 function

if(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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!