简体   繁体   English

需要有关Codeigniter路线的帮助

[英]need some help about codeigniter route

Is there a way to make the route in codeigniter work like that in symfony? 有没有办法像在symfony中那样使codeigniter中的路由起作用?

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. 因为您可以在其中重命名路由路径 ,但它仍然不会影响您的输出,因为您调用的是路由名称,而不是与codeigniter不同的路由路径

For example, in symfony, routes are set like this: 例如,在symfony中,路由设置如下:

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: 在codeigniter中,如果我这样设置路线:

$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. 所以我想知道是否有一种方法可以使其像在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. @ kennyk上有一篇博客文章,描述了他使用路由器扩展来处理类似symfony的命名路由的方法。

The article is called Easy Reverse Routing with CodeIgniter . 该文章称为使用CodeIgniter的轻松反向路由

I guess that's what you're looking for. 我想这就是您要寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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