Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created February 13, 2012 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboesch/1821407 to your computer and use it in GitHub Desktop.
Save jboesch/1821407 to your computer and use it in GitHub Desktop.
cakephp 2.0 plugin re-routing problems
<?
// routes.php
Router::connect('/animals/:action/*', array(
'plugin' => 'big',
'controller' => 'BigAnimalsController'
));
/*
* Now I navigate to /animals and I look at my action attribute on my <form> tag
* How come my $form->create(array('controller' => 'animals')); call still outputs:
* /big/animals instead of /animals
* as the action?
*/
@davemo
Copy link

davemo commented Feb 14, 2012

I haven't done cake since 1.3(ish) but it seems to me the default behavior is to include the plugin as part of the path in the emitted form element from your form helper?

@jboesch
Copy link
Author

jboesch commented Feb 14, 2012

Yeah, it's line 386 in FormHelper.php.

$plugin = null;
if ($this->plugin) {
    $plugin = Inflector::underscore($this->plugin);
}

It's grabbing $this->plugin that get's automatically set in the controller.
I'm wondering if this would be considered a bug or if I'm just trying to do something out of the ordinary.

@davemo
Copy link

davemo commented Feb 14, 2012

What's the plugin do in this instance? Required for what you are doing?

@jboesch
Copy link
Author

jboesch commented Feb 14, 2012

Yeah, it's required, I'm just trying to hide the plugin from the URL - which works. Now instead of /plugin/mycontroller I can use /mycontroller and navigate around while hiding the plugin from the URL.

But it works up until I use a plugin view, then the url's become /plugin/mycontroller - so yeah, I'm prob doing something that most people don't do. Shrug.

@davemo
Copy link

davemo commented Feb 14, 2012

If it's not a user facing url I wouldn't sweat it, if it is and you're doing a full page postback you could always submit the form via ajax to mask the action url, no?

@jboesch
Copy link
Author

jboesch commented Feb 14, 2012

What I did to work around it was add another Router::connect to make sure /plugin/controller and /controller route to the same thing. So it works fine, I'm just a consistency whore :p

I wants all me URLz to be /controller RAWR!

@shama
Copy link

shama commented Feb 16, 2012

A couple of reasons:

  1. $form->create(array('controller' => 'animals')); is incorrect. The 1st param of the $this->Form->create() should be the Model name. Cake is simply defaulting to the controller/action it is being called from. You should do this instead: $this->Form->create('BigAnimal', array('url' => array('controller' => 'big_animals'));
  2. When it comes to routing you should use the underscored names for things. Instead of BigAnimalsController it should simply be big_animals. Your Router::connect should be: Router::connect('/animals/:action/*', array('plugin' => 'big', 'controller' => 'big_animals'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment