目前使用 Kohana 3.3 默认路由,如果我说控制器用户和操作登录并且我调用 /user/login,一切都很好,操作被调用并执行。
但现在如果我将 url 更改为 /user/logins,则系统找不到任何名为 logins 的操作并返回错误“Kohana_HTTP_Exception [404]:在此服务器上找不到请求的 URL 用户/登录名”。
我的问题是,如果在控制器中找不到调用的动作,是否有办法强制重定向到 /user/index (默认动作)?
干杯!
目前使用 Kohana 3.3 默认路由,如果我说控制器用户和操作登录并且我调用 /user/login,一切都很好,操作被调用并执行。
但现在如果我将 url 更改为 /user/logins,则系统找不到任何名为 logins 的操作并返回错误“Kohana_HTTP_Exception [404]:在此服务器上找不到请求的 URL 用户/登录名”。
我的问题是,如果在控制器中找不到调用的动作,是否有办法强制重定向到 /user/index (默认动作)?
干杯!
您可以覆盖 HTTP_Exception_404 类并手动进行重定向。
EDIT:
So as @Darsstar suggested in comment, it's better to change action in before method in controller than to override execute method. So it goes like this, in your User controller:
protected $default_action = 'index';
public function before()
{
$action = 'action_'.$this->request->action();
if (!empty($this->default_action) && !method_exists($this, $action))
{
$this->request->action($this->default_action);
}
}
So if there is no current action and default action is defined it changes current request action to default. You can put this code into main Controller and define only $default_action in subcontrollers.
Old answer:
You should override execute method from Controller class. Normally it looks like this:
public function execute()
{
// Execute the "before action" method
$this->before();
// Determine the action to use
$action = 'action_'.$this->request->action();
// If the action doesn't exist, it's a 404
if ( ! method_exists($this, $action))
{
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $this->request->uri())
)->request($this->request);
}
// Execute the action itself
$this->{$action}();
// Execute the "after action" method
$this->after();
// Return the response
return $this->response;
}
Change it to something like this:
public function execute()
{
// Execute the "before action" method
$this->before();
// Determine the action to use
$action = 'action_'.$this->request->action();
// If the action doesn't exist, check default action
if ( ! method_exists($this, $action))
{
//Can be hardcoded action_index or $this->default_action set in controller
$action = 'action_index';
// If the action doesn't exist, it's a 404
if ( ! method_exists($this, $action))
{
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $this->request->uri())
)->request($this->request);
}
}
// Execute the action itself
$this->{$action}();
// Execute the "after action" method
$this->after();
// Return the response
return $this->response;
}
Now if action doesn't exist it checks default action and runs it, or if doesn't exist throws 404.
实际上你必须重写 HTTP_Exception_404 类。
您可以在那里创建一个 404 NotFound 页面。但是,如果您想重定向到某个页面 - 您需要执行以下操作:
class HTTP_Exception_401 extends Kohana_HTTP_Exception_401 {
/**
* Generate a Response for the 401 Exception.
*
* The user should be redirect to a login page.
*
* @return Response
*/
public function get_response()
{
$response = Response::factory()
->status(401)
->headers('Location', URL::site('user/index'));
return $response;
}
}