我知道 codeigniter 支持自定义路由到另一个类/方法 例如
$route['product/(:any)'] = "catalog/product_lookup";
但是它是否支持基于调用 url 的 http 请求类型的路由,就像在 laravel 中一样?
例如像 Laravel 一样使用 get 方法重新路由
Read PUT /users/X
Route::post('users/{id}','UsersController@update');
最佳答案
正式版、生产版没有。 docs 中对此只字不提.但是……
此功能在 development branch 中实现.它是从 this pull request 合并而来的.以下是引用该功能的文档开发版本的一部分:
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.
快速解决方案是使用特定提交的更改更新 system/core/Router.php
:https://github.com/EllisLab/CodeIgniter/commit/af709d6ebe6ca75913d7a9eedb4fdcd76be45c50 .
此外,拉取请求引用 Pigeon library用于处理高级路由配置。
关于php - codeigniter 2.1.4 支持 http 方法自定义路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22552071/