php 如何将带有参数的URI路由到codeigniter中的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31691614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to route the URI with parameters to a method in codeigniter?
提问by Velu N
I don't know much about the routing concept in codeigniter, I want to pass many parameters to a single method as explained in this http://www.codeigniter.com/userguide2/general/controllers.htmltutorial page.
我不太了解 codeigniter 中的路由概念,我想将许多参数传递给一个方法,如http://www.codeigniter.com/userguide2/general/controllers.html教程页面中所述。
In the url I have this
在网址中我有这个
http://localhost/code_igniter/products/display/2/3/4
http://localhost/code_igniter/products/display/2/3/4
In my routes.php I have written
在我的 routes.php 中我写了
$route['products/display/(:any)'] = 'Products_controller/display';
$route['products/display/(:any)'] = 'Products_controller/display';
What I thought is it will pass all the parameters (here 2/3/4) to the method 'display' automatically but I am getting 404 page not found error.
我认为它会自动将所有参数(此处为 2/3/4)传递给方法“显示”,但我收到 404 页面未找到错误。
In general I want to achieve something like, if the URI is controller/method
I want to route to someother_controller/its_method
and pass the parameters if any to that method. How can I do it?
一般来说,我想实现类似的东西,如果 URI 是controller/method
我想路由到的someother_controller/its_method
并将参数(如果有)传递给该方法。我该怎么做?
回答by greco.roamin
In CI 3.x the (:any) parameter matches only a single URI segment. So for example:
在 CI 3.x 中,(:any) 参数仅匹配单个 URI 段。例如:
$route['method/(:any)/(:any)'] = 'controller/method//';
will match exactly two segments and pass them appropriately. If you want to match 1 or 2 you can do this (in order):
将精确匹配两个段并适当地传递它们。如果你想匹配 1 或 2 你可以这样做(按顺序):
$route['method/(:any)/(:any)'] = 'controller/method//';
$route['method/(:any)'] = 'controller/method/';
You can pass multiple segments with the (.+) parameter like this:
您可以使用 (.+) 参数传递多个段,如下所示:
$route['method/(.+)'] = 'controller/method/';
In that case the $1 will contain everything past method/. In general I think its discouraged to use this since you should know what is being passed and handle it appropriately but there are times (.+) comes in handy. For example if you don't know how many parameters are being passed this will allow you to capture all of them. Also remember, you can set default parameters in your methods like this:
在这种情况下,$1 将包含所有过去的方法/。一般来说,我认为不鼓励使用它,因为您应该知道正在传递什么并适当处理它,但有时 (.+) 会派上用场。例如,如果您不知道传递了多少参数,这将允许您捕获所有参数。还请记住,您可以在方法中设置默认参数,如下所示:
public function method($param=''){}
So that if nothing is passed, you still have a valid value.
因此,如果没有传递任何内容,您仍然有一个有效的值。
You can also pass to your index method like this:
您还可以像这样传递给您的索引方法:
$route['method/(:any)/(:any)'] = 'controller/method/index//';
$route['method/(:any)'] = 'controller/method/index/';
Obviously these are just examples. You can also include folders and more complex routing but that should get you started.
显然,这些只是示例。您还可以包含文件夹和更复杂的路由,但这应该可以帮助您入门。
回答by Mr. ED
On codeigniter 3
在代码点火器 3 上
Make sure your controller has first letter upper case on file name and class name
确保您的控制器在文件名和类名上有首字母大写
application > controllers > Products_controller.php
应用程序 > 控制器 > Products_controller.php
<?php
class Products_controller extends CI_Controller {
public function index() {
}
public function display() {
}
}
On Routes
在路线上
$route['products/display'] = 'products_controller/display';
$route['products/display/(:any)'] = 'products_controller/display/';
$route['products/display/(:any)/(:any)'] = 'products_controller/display//';
$route['products/display/(:any)/(:any)/(:any)'] = 'products_controller/display///3';
Docs For Codeigniter 3 and 2
Codeigniter 3 和 2 的文档
回答by Suvash sarker
Maintain your routing rules like this
像这样维护你的路由规则
$route['products/display/(:any)/(:any)/(:any)'] = 'Products_controller/display///';
Please check this link Codeigniter URI Routing
请检查此链接Codeigniter URI 路由