I am having problems setting up an RESTful API with Kohana 3.3. I have added the following module (branch 3.3/release/v2.0), https://github.com/michal-m/kohana-modules-restful, to my bootstrap.
我在使用Kohana 3.3设置RESTful API时遇到问题。我已将以下模块(分支3.3 / release / v2.0),https://github.com/michal-m/kohana-modules-restful添加到我的bootstrap中。
Kohana::modules(array(
'restful' => MODPATH.'restful'
));
I've created a new controller, Controller_Api
which extends RESTful_Controller
and has get
, update
, create
, delete
actions. In my routes file I have:
我创建了一个新的控制器,Controller_Api,它扩展了RESTful_Controller并获取,更新,创建,删除操作。在我的路线文件中,我有:
Route::set('api', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'api',
'action' => 'index'
));
But the problem I'm having is when I go to: http://example.com/api/user/1
但我遇到的问题是我去的时候:http://example.com/api/user/1
I get the following error which indicates my route is wrong but I can't work it out:
我得到以下错误,表明我的路线错了,但我无法解决:
Kohana_HTTP_Exception [ 404 ]: Unable to find a route to match the URI: api/user/1
Kohana_HTTP_Exception [404]:无法找到匹配URI的路由:api / user / 1
3 个解决方案
#1
2
Firstly, get the latest 3.3/release/2.0
version. It's not final yet, but I've just pushed 2 important hotfixes.
首先,获取最新的3.3 / release / 2.0版本。它还不是最终的,但我刚推了两个重要的修补程序。
Secondly, you have to add a route filter to translate request method (GET, POST, etc.) to appropriate actions. You can use one supplied with the module, like this:
其次,您必须添加路由过滤器以将请求方法(GET,POST等)转换为适当的操作。您可以使用随模块提供的模块,如下所示:
Route::set('api', '(<controller>(/<action>(/<id>)))')
->filter('RESTful::route_filter')
->defaults(array(
'controller' => 'api',
'action' => 'index'
));
Or create a simple one yourself, e.g.:
或者自己创造一个简单的,例如:
Route::set('api', '(<controller>(/<action>(/<id>)))')
->filter(function($route, $params, $request){
$params['action'] = strtolower($request->method());
return $params;
})
->defaults(array(
'controller' => 'api',
'action' => 'index'
));
#2
0
- Was
restful
module added successfully? You can try, for example,Kohana::autoload('RESTful_Controller')
(returns boolean). - 是否成功添加了restful模块?例如,您可以尝试Kohana :: autoload('RESTful_Controller')(返回布尔值)。
- Checkout your route list. Common mistake is to add your custom route after default catch-all route. Call
Route::all()
and you will see all routes in order they were added. - 查看您的路线列表。常见的错误是在默认的catch-all路由后添加自定义路由。调用Route :: all(),您将看到所有路由按顺序添加。
- Your route looks correct, but you can test it:
- 您的路线看起来正确,但您可以测试它:
$route = Route::get('api');
$request = Request::factory('api/user/1');
// since 3.3 Route::matches requires Request object
$params = $route->matches($request);
$params
should contain Request
param names (controller, action etc). If your route is incorrect, $params
is FALSE.
$ params应该包含Request param名称(控制器,动作等)。如果您的路线不正确,$ params为FALSE。
#3
0
I'm use route filter. Route filters added in 3.3 version. Example:
我正在使用路由过滤器。路由过滤器在3.3版本中添加。例:
bootstrap.php
bootstrap.php中
Route::set('users', '(<controller>(/<id>))')
->filter(function ($route, $params, $request) {
$params['action'] = strtolower($request->method()) . '_' . $params['action'];
return $params;
})
->defaults(array('controller' => 'Users', 'action' => 'user'));
bootstrap.php (If need check authentication)
bootstrap.php(如果需要检查身份验证)
Route::set('users', '(<controller>(/<id>))')
->filter(function ($route, $params, $request) {
if (Auth::instance()->logged_in())
{
$params['action'] = strtolower($request->method()) . '_' . $params['action'];
}
else
{
$params['action'] = 'noauth';
}
return $params;
})
->defaults(array('controller' => 'Users', 'action' => 'user'));
And code cuts from my controller.
并且我的控制器会删除代码。
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Users extends Controller {
public function action_get_user ()
{
// GET method
}
public function action_post_user ()
{
// POST method
}
public function action_put_user ()
{
// PUT method
}
public function action_delete_user ()
{
// DELETE method
}
public function action_noauth ()
{
// No authentication response
}
}
#1
2
Firstly, get the latest 3.3/release/2.0
version. It's not final yet, but I've just pushed 2 important hotfixes.
首先,获取最新的3.3 / release / 2.0版本。它还不是最终的,但我刚推了两个重要的修补程序。
Secondly, you have to add a route filter to translate request method (GET, POST, etc.) to appropriate actions. You can use one supplied with the module, like this:
其次,您必须添加路由过滤器以将请求方法(GET,POST等)转换为适当的操作。您可以使用随模块提供的模块,如下所示:
Route::set('api', '(<controller>(/<action>(/<id>)))')
->filter('RESTful::route_filter')
->defaults(array(
'controller' => 'api',
'action' => 'index'
));
Or create a simple one yourself, e.g.:
或者自己创造一个简单的,例如:
Route::set('api', '(<controller>(/<action>(/<id>)))')
->filter(function($route, $params, $request){
$params['action'] = strtolower($request->method());
return $params;
})
->defaults(array(
'controller' => 'api',
'action' => 'index'
));
#2
0
- Was
restful
module added successfully? You can try, for example,Kohana::autoload('RESTful_Controller')
(returns boolean). - 是否成功添加了restful模块?例如,您可以尝试Kohana :: autoload('RESTful_Controller')(返回布尔值)。
- Checkout your route list. Common mistake is to add your custom route after default catch-all route. Call
Route::all()
and you will see all routes in order they were added. - 查看您的路线列表。常见的错误是在默认的catch-all路由后添加自定义路由。调用Route :: all(),您将看到所有路由按顺序添加。
- Your route looks correct, but you can test it:
- 您的路线看起来正确,但您可以测试它:
$route = Route::get('api');
$request = Request::factory('api/user/1');
// since 3.3 Route::matches requires Request object
$params = $route->matches($request);
$params
should contain Request
param names (controller, action etc). If your route is incorrect, $params
is FALSE.
$ params应该包含Request param名称(控制器,动作等)。如果您的路线不正确,$ params为FALSE。
#3
0
I'm use route filter. Route filters added in 3.3 version. Example:
我正在使用路由过滤器。路由过滤器在3.3版本中添加。例:
bootstrap.php
bootstrap.php中
Route::set('users', '(<controller>(/<id>))')
->filter(function ($route, $params, $request) {
$params['action'] = strtolower($request->method()) . '_' . $params['action'];
return $params;
})
->defaults(array('controller' => 'Users', 'action' => 'user'));
bootstrap.php (If need check authentication)
bootstrap.php(如果需要检查身份验证)
Route::set('users', '(<controller>(/<id>))')
->filter(function ($route, $params, $request) {
if (Auth::instance()->logged_in())
{
$params['action'] = strtolower($request->method()) . '_' . $params['action'];
}
else
{
$params['action'] = 'noauth';
}
return $params;
})
->defaults(array('controller' => 'Users', 'action' => 'user'));
And code cuts from my controller.
并且我的控制器会删除代码。
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Users extends Controller {
public function action_get_user ()
{
// GET method
}
public function action_post_user ()
{
// POST method
}
public function action_put_user ()
{
// PUT method
}
public function action_delete_user ()
{
// DELETE method
}
public function action_noauth ()
{
// No authentication response
}
}