【问题标题】:Kohana 3.3 Route - action not found redirected to default of the controllerKohana 3.3 Route - 找不到重定向到控制器默认值的操作
【发布时间】:2013-10-07 12:27:36
【问题描述】:

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

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

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

干杯!

【问题讨论】:

    标签: routes kohana kohana-3.3


    【解决方案1】:

    您可以override HTTP_Exception_404 class 并在那里手动进行重定向。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        编辑:

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

        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 类的执行方法。通常它看起来像这样:

        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。

        【讨论】:

        • 在 before() 方法中更改操作会更好。它就是为这样的东西而设计的。
        猜你喜欢
        • 2017-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 2016-11-30
        相关资源
        最近更新 更多