Kohana 3.3 Route - 未找到重定向到控制器默认值的操作

时间:2013-10-07 12:27:37

标签: routes kohana kohana-3.3

目前使用Kohana 3.3默认路由如果我说控制器用户和操作登录并且我调用/ user / login,一切都很好,操作被调用并执行。

但是现在如果我将url更改为/ user / logins,那么系统找不到任何名为logins的操作并返回错误“Kohana_HTTP_Exception [404]:在此服务器上找不到请求的URL用户/登录。”< / p>

我的问题是,是否有办法强制重定向到/ user / index(默认操作),以防有人在控制器中找不到操作?

喝彩!

3 个答案:

答案 0 :(得分:2)

您可以override HTTP_Exception_404 class并手动重定向。

答案 1 :(得分:1)

实际上你必须覆盖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;
    }
}

答案 2 :(得分:1)

修改

正如@Darsstar在评论中建议的那样,最好在控制器中的方法之前更改操作,而不是覆盖执行方法。所以在你的用户控制器中就是这样:

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);
    }
}

因此,如果没有当前操作并且定义了默认操作,则会将当前请求操作更改为默认值。您可以将此代码放入主控制器中,并在子控制器中仅定义$ default_action。


旧回答:

您应该从Controller类重写exe​​cute方法。通常它看起来像这样:

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;
}

将其更改为:

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;
}

现在,如果操作不存在,则检查默认操作并运行它,或者如果不存在则抛出404.