比方說,我們有以下的url, http://www.example.com/controllerName/methodName/param1/param2/param3 在上述URL的param1,參數2,參數3是其將被傳遞到控制器的方法「方法名」參數。笨邏輯用於從URL段傳遞函數參數
現在,我只是想知道背後的邏輯,從URL傳遞函數的參數和
的第二件事是如何參數段的數量從URL中包含笨控制器的方法參數映射?
比方說,我們有以下的url, http://www.example.com/controllerName/methodName/param1/param2/param3 在上述URL的param1,參數2,參數3是其將被傳遞到控制器的方法「方法名」參數。笨邏輯用於從URL段傳遞函數參數
現在,我只是想知道背後的邏輯,從URL傳遞函數的參數和
的第二件事是如何參數段的數量從URL中包含笨控制器的方法參數映射?
如果你有有限的參數數目等3或4比你可以映射他們從路由到控制器的方法等
$route['controllerName/methodName/(:any)/(:any)/(:any)'] = 'controllerName/methodName/$1/$2/$3';
現在你控制器方法可以接受3個參數
function methodName($param1,$param2,$param3) {
echo $param1,' ',$param2,' ',$param3;
}
如果數的參數比uri類長,可以幫助您獲取所有參數,並且您的路線應該是智能的,以便將所有參數發送到像這樣的控制器方法
$route['controllerName/methodName/(.+)'] = 'controllerName/methodName';
控制器方法是這樣
function methodName(){
//you will get all segments in an array
$segments = $this->uri->segment_array();
//get just one segment
$segment = $this->uri->segment(1);
}
用於讀取URI類和路由文檔的詳細信息
https://www.codeigniter.com/user_guide/libraries/uri.html https://www.codeigniter.com/user_guide/general/routing.html
我建立自定義的MVC框架。所以我在尋找代碼如何通過URL段作爲函數參數的邏輯。 –
不要重新發明輪子,如果你正在開發一些不同於任何其他php框架的東西,那麼我已經開發了框架,而不是我能理解你的需求。 – umefarooq
笨有一個自動加載的URI助手,你可以在控制器使用$this->uri->segment(/*segment #*/)
現在uri段的第一段是控制器($this->uri->segment(1)
),第二段是您的con的方法troller($this->uri->segment(2)
)第三個是你想發送給該方法的值($this->uri->segment(1)
)。您可以根據需要傳遞儘可能多的細分。你可以用/
來分開計算。恩。 http://localhost/ci/controller(1)/method(2)/value1(3)/value2(4)/value3(5)/valueb(n)
https://www.codeigniter.com/user_guide/libraries/uri.html – user4419336
https://www.codeigniter.com/user_guide/general/routing.html – user4419336