路由:'admin' =>true vs '前缀' =>'CakePHP 中的管理员 [英] Routing: 'admin' => true vs 'prefix' => 'admin in CakePHP

查看:25
本文介绍了路由:'admin' =>true vs '前缀' =>'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' => 的意义何在?是的?

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?

推荐答案

通过设置'prefix' =>'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 识别(即不是你通常的 controlleractionplugin, prefix stuff) 在匹配该路由的请求期间被设置为命名参数.

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.

添加 '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).

最后,通过使用命名参数设置像这样的自定义标志并在您的应用程序中使用它而不是通过确切的字符串引用您的路由意味着如果您将前缀更改为其他内容,您将无需更改链接(比如从 adminadministratoredit)...虽然这有点争议,因为您需要重命名所有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' =>true vs '前缀' =>'CakePHP 中的管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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