views:

260

answers:

8

I really like the named URLs in the Django Framework, and was wondering which of the PHP Frameworks supported such a thing. (I believe Zend Framework does, but please exclude that from your answers, without explanation).

For instance, in Django I can name a path like "/items/###" as "item-detail-page", which would point to a View 'function' that accepts a product ID as a parameter.

Later, I could reference the address to the Item Detail Page by creating a link (via a helper function) to 'item-detail-page 123' or similar. That way, I can change my URL structure later, should I ever need to, without ever changing the references or links in my HTML templates.

Does such a thing already exist in CodeIgniter, CakePHP, etc?

Django users: I know, this post does not contain the correct template syntax for the 'url' template tag. I was only trying to provide the general idea.

EDIT/UPDATE: I suppose this question was a bit unnecessary, as it's obvious now that all of the popular frameworks support a type of routing that works more/less like Django's does. Therefore, I feel it a bit unfair to award a single best answer, except perhaps the one that appears to come closest to matching Django's style of link-generating, since I mentioned that specifically in my question (and therefore for the benefit of others seeking the same and finding this post).

+1  A: 

Kohana, which branched off of the Code Igniter framework, supports PHP5 and allows SEO friendly URLs with their URI library and Router class:

Kohana

cballou
A: 

Codeigniter does what you want. I've been using Codeigniter for a couple years and love it! Almost all my php projects are built on codeigniter.

Mark
+3  A: 

If I understand you correctly, CakePHP can definitely do this. Routes is what you're looking for.

All route/redirect configuration is assembled in one file, routes.php. In that you set your connections like

Router::connect('/archives/*', array('controller' => 'posts', 
                                     'action' => 'archives'));

if you want to route domain.com/archives to domain.com/posts/archives.

A slightly more complex example is

Router::connect(
    '/:year/:month/:day/*',
    array('controller'=>'posts', 'action'=>'view'),
    array(
        'year' => '[12][0-9]{3}',
        'month' => '0[1-9]|1[012]',
        'day' => '0[1-9]|[12][0-9]|3[01]'
    )
);

if you want to route domain.com/YYYY/MM/DD/post-title to domain.com/posts/view/id (the link between post-title and id is not handled in routes here).

Good luck.

anderstornvig
Probably worth mentioning also that CakePHP has a feature called "reverse routing" which has you covered if you ever change the change your URL structure (ie. routes) later. Any link created by passing an array with the necessary parameters (eg. controller/function/id) to CakePHP's provided helpers (any class which internally uses `Router::url()`) will produce a URL that conforms to the first configured route that matches.
deizel
Good point deizel. In addition, Bjorn's comment below about "named route" functionality not existing for CakePHP just yet, Mark Gandolfo has built this, check out: http://github.com/markgandolfo/cakephp_named_route
neilcrookes
I accepted this answer based on the fact that @neilcrookes points out: CakePHP has, at least in a third-party library, a "named routes" functionality. If others do as well, feel free to point that out. Thanks.
anonymous coward
A: 

if you want something simple and I'm mean really simple try Limonade. Anyway you can always create your own router

Gabriel Sosa
A: 

Been using CakePHP for a while now for this exact reason. Router settings will get you the types of URLs you want while keeping the MVC maintainable.

Pauly
+1  A: 

I would second cballou's recommendation of Kohana. It's quite ironic because at first I did use CI and just didn't like it but then I found Kohana (which is a CI fork) and it's great. The documentation is lacking a little but certainly worth the hard work.

And yes, you can do /items/apple-ipod-touch-16gb in Kohana without setting up any routing (as-per CakePHP).

eth0
but until v3 (which is beta and has no docs yet) kohana does not have reverse routing (if you change a route you have to manually modified all the links)
gpilotino
+2  A: 

Symfony has a routing system as well. Routing rules are configured in a configuration file (routing.yml), and your rule would look something like this:

items:
  url: /items/*
  param: { module: somemodule, action: someaction }

And like Cake, you can use the routing rule to generate a URL as well.

http://www.symfony-project.org/book/1_2/09-Links-and-the-Routing-System

Mike
Symfony's routes are very clean and well thought out. I can personally recommend using them.
Jon Winstanley
A: 

I guess you mean you can do something like this:

(CakePHP alike pseudocode)

Router::connect('link_to_homepage', '/', array('controller' => 'foo', 'action' => 'bar');

Template:

$html->link('I want to go home!', 'link_to_homepage');

Instead of

$html->link('I want to go home!', '/');

As far as I'm aware of, such a thing doesn't exsist (yet) for CakePHP.

Bjorn
CakePHP can doRouter::connect('/', array('controller' => 'foo', 'action' => 'bar');I.e. route '/' through to a specific controller and action, then in your views you can do...$html->link('home', array('controller' => 'foo', 'action' => 'bar'));Which will generate:<a href="/">home</a>And like some else already said, you can just change the route at any point to change the url generated by the HtmlHelper::link() method.
neilcrookes
Neil, please read about the Django URL dispatcher. The example you are providing isn't like the way Django parses URLs. Please read the appropriate chapter in the Django Documentation, check http://docs.djangoproject.com/en/dev/topics/http/urls/#topics-http-urls .
Bjorn