我的PortfolioController中有以下代码:
function index()
{
$this->set('posts', $this->Portfolio->find('all'));
}
function view ( $id, $slug )
{
$post = $this->Portfolio->read(null, Tiny::reverseTiny($id));
$this->set(compact('post'));
}
然而,为了获得从URL中删除/view/
的视图,我在路由中添加了以下内容:Router::connect('/portfolio/*', array('controller' => 'portfolio', 'action' => 'view'));
这会破坏索引方法,因为它通过调用视图方法来覆盖它,并显示一个空白视图
我该如何解决这个问题?
您到底想做什么?($slug是怎么回事?)
听起来你想做的是删除操作(或者至少删除view()操作?)从显示在URL中,amirite?有点像默认的pages_controller display()方法-捕获静态页面的所有操作?
我该如何解决这个问题?
好吧,我建议从不打破这条路线开始,因为否则它会按照你的要求行事:
Router::connect('/portfolio/*',
// * is a wildcard matching anything & everything after /portfolio/
array('controller' => 'portfolio',
// and routing to portfolio's view() action, with or w/o required $args to pass
'action' => 'view'));
因此,当您调用index()时,您看到的不是一个空白视图,而是一个被抑制的致命错误,这就是当index()重新路由到view()并且没有$id来传递第一个arg时会发生的情况。
注意结尾DS。路由顺序很重要;抓住的第一条规则就是胜利。如果省略url的操作,以下路由默认情况下都会映射到索引,但它们并不相同。
// Targets inside the controller (its methods)
Router::connect('/portfolio/',
array('controller' => 'portfolio', 'action' => 'index'));
与不同
// Targets the controller
Router::connect('/portfolio',
// Specifies the default controller action, can be whatever
array('controller' => 'portfolio', 'action' => 'index'));
对于您要做的事情,应该是
// Targets the controller
Router::connect('/portfolio',
// Routes to 'controller default method' which is index() by Cake default
array('controller' => 'portfolio');
这允许Cake在URL中缺少操作时强制执行到控制器index()的自动默认映射
如果没有后面的DS和后面的星号,它仍然可以工作。应该捕获index()的相同规则改为重定向到view(),这要归功于后面的星号针对投资组合中的所有操作。
因此,Foo的建议不起作用->尾随DS+通配符:
Router::connect('/portfolio/',
// the trailing DS changes it to target 'inside portfolio' instead of 'portfolio'
array('controller'=>'portfolio', 'action'=>'index'));
// trailing arbitrary wildcard maps any / all actions directly to view() method
Router::connect('/portfolio/*',
array('controller' => 'portfolio', 'action' => 'view'));
这只是确保投资组合中的所有操作直接映射到投资组合视图()方法(包括/portfolio/index操作)。不要传递go等。任何组合操作都会解析为通配符,从而将整个控制器别名为该方法。因此,您可以将DS从第一条路由中删除,但任何以/portfolio开头的url,如果不是/portfolia,仍将路由到view()。包括url/portfolio/index。
试试这个:
// catches portfolio/index() without index in the url
Router::connect('/portfolio',
array('controller' => 'portfolio'));
// maps to portfolio/view() without view in url, just /portfolio/integer id
Router::connect('/portfolio/:id',
array('action'=>'view', array('id' => '[0-9]+'));
// routes everything else in portfolio as usual
Router::connect('/portfolio/:action/*',
array('controller'=>'portfolio'));
路线可能很棘手。以下是一些链接;HTH:)
http://bakery.cakephp.org/articles/Frank/2009/11/02/cakephp-s-routing-explained
http://book.cakephp.org/view/46/Routes-Configuration
我自己是CakePHP的新手,但我相信你可以添加
Router::connect('/portfolio/', array('controller' => 'portfolio', 'action' => 'index'));
在与星星同行的路线之前。
据我所知你应该这样给:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
我的意思是,您应该更改控制器名称的别名。
Router::connect('/portfolios/*', array('controller' => 'portfolios', 'action' => 'view'));