首页 > 解决方案 > Codeigniter 路由在分页和其他 URL 上发生冲突

问题描述

CodeIgniter 路由在分页和另一个 URL 上发生冲突。实际上,我正在尝试在分页时加载以下 URL。

http://127.0.0.1/Mytredin_codesup/snippets

对于我的帖子,我想加载以下 URL

http://127.0.0.1/Mytredin_codesup/snippets/auto-loader-63152391

但是一次只运行一个 URL。

我正在使用以下路由

$route['snippets/(:any)'] = 'snippets/view/$1';
$route['allsnippets/(:num)'] = 'welcome';
$route['snippets/(:num)'] = 'snippets';

标签: phpcodeigniterrouting

解决方案


您是否希望函数中的页码用于第三次路由?

是的,这就是我在这条路线上想要的

因此,尝试将您的第三个路由规则更改为,

$route['snippets/(:num)'] = 'snippets/index/$1';

和你在snippets控制器中的功能

function index(page_no){
    //your code here
}

编辑:

按此顺序保持您的路由规则。

$route['snippets/(:num)'] = 'snippets/index/$1';
$route['snippets/(:any)'] = 'snippets/view/$1';
$route['allsnippets/(:num)'] = 'welcome';

问题是在此any之前num使用any将接受任何字符,除了/并且不会进入num路由。因此,更改路由顺序将起作用。


推荐阅读