【发布时间】:2012-08-02 13:49:09
【问题描述】:
我有以下想法:我希望能够在任何给定的控制器中定义默认前缀。因此,假设 CitiesController 的默认前缀使用 "admin" prefix ("admin_index", "admin_add", etc.), 实现所有操作,但 ProvincesController 使用
"superadmin" prefix ("superadmin_index", "superadmin_add", etc.)
问题在于,每次我想链接到任何“城市的东西”时,我都必须指定“admin”=>“true”。任何时候我想链接到任何“省的东西”,我都必须指定
"superadmin" => "true".
一开始这已经是相当大的工作量了,但如果我决定要将城市的前缀从“admin”更改为“superadmin”,那就更难了。
所以我想知道是否有办法按照以下方式做一些事情:
class CitiesController extends AppController {
var $defaultPrefix = "admin"
}
然后在 HTML 辅助链接函数中,执行如下操作:
class LinkHelper extends AppHelper {
public $helpers = array('Html');
function myDynamicPrefixLink($title, $options) {
// check whether prefix was set (custom function that checks all known prefixes)
if (! isPrefixSet($options)) {
// no clue how to get the controller here
$controller = functionToGetControllerByName($options['controller']);
// check whether controller has a defined default prefix
$prefix = $controller->defaultPrefix;
if ($prefix) {
// set the given prefix to true
$options[$prefix] = true;
}
// use HTML helper to get link
return $this->Html->link($title, $options);
}
}
我只是不知道如何动态地从帮助程序到给定名称的控制器。
另一种选择是将默认前缀存储在其他地方,但现在我觉得最好的地方是任何给定的控制器本身。
另一个想法是甚至让查找函数同时依赖于控制器和动作,而不仅仅是控制器。
【问题讨论】:
标签: cakephp routing prefix cakephp-2.2