简体   繁体   English

Codeigniter逻辑,用于从URL段传递函数参数

[英]Codeigniter Logic for passing function parameters from URL segments

Lets say we have following url, http://www.example.com/controllerName/methodName/param1/param2/param3 In above url param1, param2, param3 are parameters which will be passed to controller method 'methodName'. 假设我们有以下URL, http://www.example.com/controllerName/methodName/param1/param2/param3在上面的URL中,param1,param2,param3是将传递给控制器​​方法“ methodName”的参数。

Now I just want to know the logic behind to pass function parameters from url and 现在我只想知道从url和

the second thing is How to map the number of parameter segments from url with controller method arguments like codeigniter? 第二件事是如何使用控制器方法参数(如codeigniter)来映射url中的参数段数?

if you have limited number of parameters like 3 or 4 than you can mapped them from routes to controller method like 如果参数数量有限(例如3或4),则可以将它们从路由映射到控制器方法,例如

$route['controllerName/methodName/(:any)/(:any)/(:any)'] = 'controllerName/methodName/$1/$2/$3';

Now you controller method can accept 3 parameters 现在您的控制器方法可以接受3个参数

function methodName($param1,$param2,$param3) {
   echo $param1,' ',$param2,' ',$param3;
}

if number of parameters is long than uri class can help you to fetch all parameters and your route should be smart to send all parameter to that controller method like this 如果参数的数量比uri类长,则可以帮助您获取所有参数,并且您的路由应该很聪明,可以将所有参数发送给该控制器方法,如下所示

$route['controllerName/methodName/(.+)'] = 'controllerName/methodName';

controller method be like 控制器方法如

function methodName(){
   //you will get all segments in an array
   $segments = $this->uri->segment_array();

   //get just one segment
   $segment = $this->uri->segment(1);

}

for more information read uri class and routing documentation 有关更多信息,请阅读uri类和路由文档

https://www.codeigniter.com/user_guide/libraries/uri.html https://www.codeigniter.com/user_guide/general/routing.html https://www.codeigniter.com/user_guide/libraries/uri.html https://www.codeigniter.com/user_guide/general/routing.html

Codeigniter has an autoloaded uri helper and you can use it in the controller as $this->uri->segment(/*segment #*/) now uri segment is start with the segment 1 which is the controller (the $this->uri->segment(1) ), the second is the method of your controller (the $this->uri->segment(2) ) the third is the value you want to send to the method (the $this->uri->segment(1) ). Codeigniter有一个自动加载的uri helper,您可以在控制器中使用它,因为$this->uri->segment(/*segment #*/)现在uri segment从控制器的第1段开始( $this->uri->segment(1) ),第二个是控制器的方法( $this->uri->segment(2) ),第三个是要发送给该方法的值( $this->uri->segment(1) )。 you can pass as many segment as you want. 您可以根据需要传递尽可能多的细分。 you can count it with the / seperation. 您可以使用/分隔来计数。 ex. 恩。 http://localhost/ci/controller(1)/method(2)/value1(3)/value2(4)/value3(5)/valueb(n)

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

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