Cameron
Cameron

Reputation: 28783

CakePHP broken index method

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?

Upvotes: 1

Views: 1095

Answers (3)

Archit Patel
Archit Patel

Reputation: 49

as per my knowledge is concern you should give like this:

Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); 

I mean you should changes the aliases of the controller name.

Router::connect('/portfolios/*', array('controller' => 'portfolios', 'action' => 'view'));

Upvotes: 0

OpenSorceress
OpenSorceress

Reputation: 2014

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

Upvotes: 6

Foo
Foo

Reputation: 576

I'm new to CakePHP myself, but I believe you can add

Router::connect('/portfolio/', array('controller' => 'portfolio', 'action' => 'index'));

Before the route with the star.

Upvotes: 0

Related Questions