路由:'admin'=> true vs'prefix'=> 'admin在CakePHP [英] Routing: 'admin' => true vs 'prefix' => 'admin in CakePHP

查看:281
本文介绍了路由:'admin'=> true vs'prefix'=> 'admin在CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在CakePHP中设置了管理路由。

Hi I'm setting up admin routing in CakePHP.

这是我当前的路由:

Router::connect('/admin/:controller/:action/*', array('admin' => true, 'prefix' => 'admin', 'controller' => 'pages', 'action' => 'display', 'home'));

它工作正常,但我不明白'admin'=> true,和'prefix'=>'admin'是。

It works fine, but I don't understand what the difference between 'admin' => true, and 'prefix' => 'admin' is.

当我省略'prefix'=> 'admin',则路由器不会使用 admin_index ,而只是使用 index 。那么'admin'=> true c>

When I omitted 'prefix' => 'admin', the router wouldn't use admin_index and would instead just use index. So what's the point of 'admin' => true?

推荐答案

'admin'您告诉CakePHP您要为该路由使用 admin 的前缀;基本上意味着你想使用名称前缀为 admin _ 的控制器操作和视图。

By setting 'prefix' => 'admin' you are telling CakePHP that you want to use a prefix of admin for that route; basically meaning you want to use controller actions and views that have names prefixed with admin_. This part you are already aware of, and things will probably work fine with just this.

当创建路由时,任何传递到第二个参数的数组键都不是这样的由CakePHP识别(即不是你通常的控制器动作插件

When creating routes though, any array keys passed into the second argument that aren't recognised by CakePHP (ie. not your usual controller, action, plugin, prefix stuff) are set as named parameters during requests matching that route.

添加<$ c> <$ c> <$ p> $ c>'admin'=> true 因此在这种情况下只是一个命名参数,但它带有它的优点。

Adding 'admin' => true is therefore just a named parameter in this case, but it comes with its advantages. Firstly, it can make code more succinct.

/* Determine if a request came through admin routing */
// without:
if ($this->params['prefix'] == 'admin') {}
// with:
if ($this->params['admin']) {}

/* Create a link that is reverse-routed to an admin prefixed route */
// without:
$html->link('...', array('prefix' => 'admin', 'controller' => 'users'));
// with:
$html->link('...', array('admin' => true, 'controller' => 'users'));

其次,它提供与管理路由在CakePHP 1.2中工作的方式的向后兼容性上面的例子是你将如何在1.2)管理路由链接。因此,从1.2迁移到1.3的开发者可以防止必须通过保持'admin'=> true 标志(并添加'prefix'=>'admin')。

Secondly, it provides backwards compatibility with the way admin routing worked in CakePHP 1.2 (the last line from the above example is how you would have made admin routing links in 1.2). Therefore, developers migrating from 1.2 to 1.3 can prevent having to change links throughout their application by keeping the 'admin' => true flag in their routes (and adding the 'prefix' => 'admin' one).

最后,通过设置这样的自定义标志并使用命名参数,并在应用程序中使用它,而不是通过精确字符串引用您的路由,这意味着您可以防止自己在更改前缀时更改链接(例如从 admin 管理员编辑 )...虽然这是一个问题点,因为你需要重命名所有的 admin _ * 控制器操作和视图。 :)

Lastly, by setting a custom flag like this with a named parameter and using it in your application instead of referencing your route by an exact string means that you prevent yourself from ever having to change links if you change the prefix to something else (say from admin to administrator or edit)... although this is sort of a moot point, as you would need to rename all your admin_* controller actions and views. :)

这篇关于路由:'admin'=&gt; true vs'prefix'=&gt; 'admin在CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆