What's the correct way to use Cakephp urls?

Posted by Pichan on Stack Overflow See other posts from Stack Overflow or by Pichan
Published on 2010-06-13T03:19:17Z Indexed on 2010/06/13 3:22 UTC
Read the original article Hit count: 319

Filed under:
|
|
|

Hello all, 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.

© Stack Overflow or respective owner

Related posts about url

Related posts about cakephp