开发者

PHP Framework that does Named URLs? (other than Zend Framework)

开发者 https://www.devze.com 2022-12-08 13:06 出处:网络
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, w

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开发者_JS百科 of others seeking the same and finding this post).


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.


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


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

Kohana


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).


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号