change search item url and routing in codeigniter

自古美人都是妖i 提交于 2020-05-17 06:34:36

问题


I want to change the URL with Codeigniter routing:

here is my url:

home/search?location=BD

home/search?location=BD&category[]=123

home/search?location=BD&category[]=123&category[]=124&category[]=125

like above url but I want to routing this url with

home/BD

home/BD/123

home/BD/123+124+125 

or

home/BD/123/124/125

My route.php:

$route['home/(:any)/(:any)'] = 'home/search/$1';

What is my problem in route.php page?


回答1:


Try to use a (.+) pattern on your route.php, the $1 will contain the location value (BD) and the $2 will contain every parameters past the home/BD/ url :

$route['home/(:any)/(.+)'] = 'home/search/$1/$2';

The (.+) pattern is useful if you don't know how many parameters are being passed, it will allow you to capture all of them. And maybe you should use & in place of the + sign on your url since the + sign is probably disallowed by default :

home/BD/123&124&125

Then you could explode the categories on the controller :

public function search($location = '', $categories = '')
{
    if (!empty($categories)) {
        $categories = explode('&',$categories);
    }
    ...
}


来源:https://stackoverflow.com/questions/61006159/change-search-item-url-and-routing-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!