我想在CakePHP中创建一个管理练习和用户结果的应用程序。用户和结果在这个问题中并不重要。CakePHP,使用通用控制器进行可选控制器的路由,否则
我想要添加一个练习,只添加特定的表格和行到.ini配置文件。应用程序应该路由到GenericExercisesController如果特定的不存在。如果特定不存在,控制器应该加载GenericExerciseModel。我会与模型加载和部分地与控制器路由这样的管理:
在route.php
foreach(Configure::read('exercisesTables') as $exerciseName){
if(App::import('Controller', Inflector::pluralize($exerciseName))){
Router::connect('/exercises/'.Inflector::pluralize($exerciseName).'/:action', array('controller' => Inflector::pluralize($exerciseName)));
}else{
Router::connect('/exercises/'.Inflector::pluralize($exerciseName).'/:action', array('controller' => 'GenericExercises', 'fakeModel' => $exerciseName));
}
}
所以,如果我要加载的锻炼美孚我应当使用地址:
http://example.com/exercises/Foos/view
这工作正常,如果具体的控制器存在没关系。
当我使用反向路由在视图中生成链接时,问题就开始了。如果锻炼富有特定的控制器此工作正常:
print $this->Html->url(array('controller' => Inflector::pluralize($exerciseName), 'action' => 'view'));
生产: /exercises/Foos/view
但是当锻炼酒吧没有特定的控制器则相同的代码产生: /Bars
这会导致问题,没有Bars
控制器。
暂时我手动生成这些链接,但我不认为这是最好的解决办法:
print $this->Html->url("/".Configure::read('exerciseRoutingPrefix')."/".Inflector::pluralize($exerciseName)."/view");
也许你认识的人更好的解决方案。感谢您阅读我的问题。
更新1:
这些都是在route.php
路线,以foreach
之前定义为他们在文件中:
Router::connect('/', array('controller' => 'questions', 'action' => 'regulations'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/help', array('controller' => 'pages', 'action' => 'faq'));
为什么从我的问题开始,stackoverflow删除短语“你好,欢迎”? – 2013-05-07 12:46:22
一些编辑删除不必要的短语。虽然它可能是“谢谢你”和“你好”有礼貌地问好,但它不会对问题本身有所贡献,所以有些人喜欢将它们删除。 – Nunser 2013-05-07 13:15:49
你在这里发布的foreach之前是否有'routes.php'中定义的其他路线? – Nunser 2013-05-07 13:45:58