codeigniter 2.1.4 support for http methods custom routings?

放肆的年华 提交于 2019-12-12 16:02:15

问题


I know that codeigniter supports custom routing to another class / method e.g.

$route['product/(:any)'] = "catalog/product_lookup";

However does it support routings based on the type of http request calling the url like in laravel ?

e.g. using get method reroute like in Laravel

Read PUT /users/X  
Route::post('users/{id}','UsersController@update');

回答1:


The official, production version does not. Not a single word on that in docs. But...

This feature is implemented in the development branch. It was merged from this pull request. Here's a part of the development version of documentation refering to that feature:

Using HTTP Verb in Routes

If you prefer you can use HTTP Verb (or method) to define your routing rules. This is particularly useful when building RESTful application. You can use standard HTTP Verb (GET, PUT, POST, DELETE) or custom HTTP Verb (e.g: PURGE). HTTP Verb rule is case insensitive. All you need to do is add array index using HTTP Verb rule. Example:

    $route['products']['put'] = 'product/insert';

In the above example, a PUT request to URI "products" would call the "product" controller class and "insert" method.

    $route['products/(:num)']['DELETE'] = 'product/delete/$1';

A DELETE request to URL with "products" as first segment and a number in the second will be remapped to the "product" class and "delete" method passing in the match as a variable to the method.

    $route['products/([a-z]+)/(\d+)']['get'] = 'product/$1/$2';

A GET request to a URI similar to products/shirts/123 would call the "product" controller class and "shirt" method with number as method parameter.

Using HTTP Verb is optional, so if you want any HTTP Verb to be handled in one rule You could just write your routing rule without HTTP Verb. Example:

    $route['product'] = 'product';

This way, all incoming request using any HTTP method containing the word "product" in the first segment will be remapped to "product" class.

The quick solution is to update system/core/Router.php with the changes from that specific commit: https://github.com/EllisLab/CodeIgniter/commit/af709d6ebe6ca75913d7a9eedb4fdcd76be45c50.

Also, the pull request references Pigeon library for handling advanced routing configurations.




回答2:


yes you can give custom routing in codeigniter but you have to put this routing in route.php e.g here admin is my folder name and login is controller in admin folder

$route['admin'] = "admin/login";

if you we type url http://sonedomain/CI/project/admin

then it will directly redirect me to login controller of admin folder



来源:https://stackoverflow.com/questions/22552071/codeigniter-2-1-4-support-for-http-methods-custom-routings

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