简体   繁体   中英

need some help about codeigniter route

Is there a way to make the route in codeigniter work like that in symfony?

Cause in there you can rename the route path and it still wouldn't affect your output cause what you call is the route name not the route path unlike in codeigniter.

For example, in symfony, routes are set like this:

news_index:
    path:      /news
    defaults:  { _controller: CmsBundle:News:index }

news_view:
    path:      /news/view/{id}
    defaults:  { _controller: CmsBundle:News:view }

and then I can access that in my template by calling the route name , not the route path

<?php 
    // outputs: /news
    echo $view['router']->generate('news_index');

    // outputs: /news/view/1
    echo $view['router']->generate('news_view',array('id' => $news_id)); 
?>

so if I change the route path it wouldn't affect the way i call it in my code because i'm calling the route name , all it will do is change the output:

news_index:
    path:      /articles
    defaults:  { _controller: CmsBundle:News:index }

news_view:
    path:      /articles/item/{id}
    defaults:  { _controller: CmsBundle:News:view }

still same code but output is changed

<?php 

    // outputs: /articles
    echo $view['router']->generate('news_index');

    // outputs: /articles/item/1
    echo $view['router']->generate('news_view',array('id' => $news_id)); 
?>

while in codeigniter, if i set my route like this:

$route['news'] = 'NewsController';
$route['news/view/(:num)'] = 'NewsController/view/$1';

the only way i know to call it is by the route path:

<?php
    echo anchor('news');
    echo anchor('news/view'.$news_id);
?>

and if I change my path, I would have to change what I wrote in view as well. That would be a hassle. So I'm wondering if there could be a way to make it work like in symfony. I would appreciate it if anyone could help me. And I apologize if my explanation is not clear. English is not my mother tongue :)

There is blog post available from @ kennyk that describes his approach to symfony-like named routes using a router extension.

The article is called Easy Reverse Routing with CodeIgniter .

I guess that's what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM