CakePHP折断法 [英] CakePHP broken index method

查看:110
本文介绍了CakePHP折断法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PortfolioController中有以下代码:

 函数索引()
{
$ this-> set('posts',$ this-> Portfolio-> find('all'));
}

函数视图($ id,$ slug)
{
$ post = $ this-> Portfolio-> reverseTiny($ id));

$ this-> set(compact('post'));
}

但是为了获取视图来删除查看/ 从URL中,我已经添加以下到我的路由: Router :: connect('/ portfolio / *',array('controller'=& ','action'=>'view'));



这会打破索引方法,因为它通过调用view方法

解决方案

p>你想做什么? (和$ slug是什么?)



听起来像你想做的是删除操作(或至少是view() URL,amirite?类似于默认的pages_controller display()方法 - 为静态页面捕获所有操作?


如何解决此问题? >

好吧,我建议从不中断的路线开始,因为否则它正是你所说的:

  Router :: connect('/ portfolio / *',
// *是一个通配符匹配任何&
array('controller'=>'portfolio',
//并路由到投资组合的view()动作,带或者需要$ args来传递
'action'=> ;'view'));

所以你在调用index()时看到的不是一个空白视图, ,这是当index()重新路由到view()并且没有为第一个arg传递$ id时发生的情况。



请注意结束DS。路线顺序很重要。第一条规则抓住,胜利。

  / 

如果省略url的操作, /控制器中的目标(其方法)
Router :: connect('/ portfolio /',
array('controller'=>'portfolio','action'=>'index') );

不同

  //目标控制器
Router :: connect('/ portfolio',
//指定默认控制器动作,可以是任何
array '=>'portfolio','action'=>'index'));

对于您要执行的操作, p>

  //目标控制器
Router :: connect('/ portfolio',
//路由到'控制器默认方法,它是index()by Cake default
array('controller'=>'portfolio');

$ b b

这样,只要网址中缺少操作,Cake就可以强制自动将默认映射到控制器的索引()。

除了结尾的DS和结尾的星号之外,它仍然有效。同样的规则应该捕获index()重定向到view(),因为尾随星号指向投资组合中的所有操作。



因此Foo的建议无效 - >结尾的DS +通配符:

 连接('/ portfolio /',
//跟踪DS将其更改为目标'inside portfolio'而不是'portfolio'
array('controller'=>'portfolio','action' >'index'));
//拖尾任意通配符将任何/所有动作直接映射到view()方法
Router :: connect('/ portfolio / *',
array('controller'=>'portfolio ','action'=>'view'));

这只是确保投资组合地图中的所有操作直接到投资组合view()方法(包括/ portfolio / index行动)。不要通过go等。任何投资组合操作解析到通配符,无论什么,将整个控制器混淆到该方法。所以你可以敲掉DS的第一个路由,但任何网址开始/组合是/组合仍然会路由到view()。包含网址/投资组合/索引。



尝试:

  // catchches portfolio / index()in the url 
Router :: connect('/ portfolio',
array('controller'=>'portfolio'));
//映射到portfolio / view()在url中没有视图,just / portfolio / integer id
Router :: connect('/ portfolio /:id',
array =''''''''';'''''''');
//像以往一样路由组合中的所有东西
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 /路由配置


I have the following code in my 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'));
    }

However in order to get the view to remove /view/ from the URL, I have added the following to my routes: Router::connect('/portfolio/*', array('controller' => 'portfolio', 'action' => 'view'));

This breaks the index method as it overrides it by calling the view method instead and shows a blank view

How do I fix this?

解决方案

What are you trying to do exactly? (And what's with $slug?)

It sounds like what you want to do is remove the action (or at least the view() action?) from displaying in the URL, amirite? Kind of like the default pages_controller display() method - catch-all action for static pages?

How do I fix this?

Well, I'd suggest starting with un-breaking that route, because otherwise it's doing exactly what you told it to:

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'));       

so what you see when you call index() is not a blank view, it's a suppressed fatal error, which is what happens when index() reroutes to view() and doesn't have an $id to pass in for the first arg.

Note the ending DS. Route order matters; first rule that catches, wins. The following routes would all map to index by default if the url's action were omitted, but they're not the same.

// Targets inside the controller (its methods)
Router::connect('/portfolio/', 
     array('controller' => 'portfolio', 'action' => 'index'));

is not the same as

// Targets the controller
Router::connect('/portfolio', 
// Specifies the default controller action, can be whatever
    array('controller' => 'portfolio', 'action' => 'index'));

For what you're trying to do, it should be

// Targets the controller
Router::connect('/portfolio', 
// Routes to 'controller default method' which is index() by Cake default 
    array('controller' => 'portfolio');

This allows Cake to enforce auto default mapping to the controller's index() whenever the action is missing from the URL.

It would still have worked except for the trailing DS and trailing asterisk. The same rule that should catch index() reroutes to view() instead, thanks to the trailing asterisk targeting all actions in portfolio.

Hence Foo's suggestion doesn't work -> trailing DS + wildcard:

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'));

Which just ensures ALL actions in portfolio map directly to portfolio view() method (including /portfolio/index action). Do not pass go, etc. Any portfolio action resolves to the wildcard no matter what, aliasing the whole controller to that method. So you could knock the DS off the first route but any url starting with /portfolio that isn't /portfolio would still route to view(). Including the url /portfolio/index.

Try this:

// 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'));

Routes can be tricky. Here are some links; HTH. :)

http://bakery.cakephp.org/articles/Frank/2009/11/02/cakephp-s-routing-explained

http://book.cakephp.org/view/46/Routes-Configuration

这篇关于CakePHP折断法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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