Question

it's my first post here :)

I'm having some difficulties with dealing with urls and parameters. I've gone through the router class api documentation over and over again and found nothing useful.

First of all, I'd like to know if there is any 'universal' format in CakePHP(1.3) for handling urls. I'm currently handling all my urls as simple arrays(in the format that Router::url and $html->link accepts) and it's easy as long as I only need to pass them as arguments to cake's own methods. It usually gets tricky if I need something else.

Mainly I'm having problems with converting string urls to the basic array format. Let's say I want to convert $arrayUrl to string and than again into url:

$arrayUrl=array('controller'=>'SomeController','action'=>'someAction','someValue');
$url=Router::url($arrayUrl);       //$url is now '/path/to/site/someController/someAction/someValue'
$url=Router::normalize($url);      //remove '/path/to/site'
$parsed=Router::parse($url);       /*$parsed is now
Array(
    [controller] => someController
    [action] => someAction
    [named] => Array()
    [pass] => Array([0] => someValue)
    [plugin] => 
) */

That seems an awful lot of code to do something as simple as to convert between 2 core formats. Also, note that $parsed is still not in the same as $arrayUrl. Of course I could tweak $parsed manually and actually I've done that a few times as a quick patch but I'd like to get to the bottom of this.

I also noticed that when using prefix routing, $this->params in controller has the prefix embedded in the action(i.e. [action] => 'admin_edit') and the result of Router::parse() does not. Both of course have the prefix in it's own key.

To summarize, how do I convert an url between any of these 3(or 4, if you include the prefix thing) mentioned formats the right way? Of course it would be easy to hack my way through this, but I'd still like to believe that cake is being developed by a bunch of people who have a lot more experience and insight than me, so I'm guessing there's a good reason for this "perceived misbehavior".

I've tried to present my problem as good as I can, but due to my rusty english skills, I had to take a few detours :) I'll explain more if needed.

Was it helpful?

Solution

The "official" format for Cake URLs should be the array notation array('controller' => 'foo', 'action' => 'bar', 'baz', 'admin' => true). Whenever you write URLs, you should use this format. The Router class will translate those to either strings (/admin/foo/bar/baz) or information needed for the Dispatcher (array('named' => array(), 'pass' => array(), …)), depending on where the URL is used.

You should think of it in terms of which controller action you want to invoke. URLs (as strings) are only a necessary evil to accomplish this in a web context. There shouldn't be any need for you to use the Dispatcher format. You should also not use the string notation when specifying URLs, since these can't be reverse-routed if you ever want to change your URL scheme.

Maybe you could explain with an example why you need to convert these three forms from one to the other?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top