【问题标题】:Kohana ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()Kohana ErrorException [致命错误]:调用未定义的方法 Request::redirect()
【发布时间】:2012-10-26 14:13:27
【问题描述】:

我使用的是 Kohana 3.3.0,我有一个控制器,它应该将博客文章保存到数据库然后重定向到主页,我的代码如下:-

class Controller_Article extends Controller {

const INDEX_PAGE = 'index.php/article';

public function action_post() {

$article_id = $this->request->param('id');
$article = new Model_Article($article_id);
$article->values($_POST); // populate $article object from $_POST array
$article->save(); // saves article to database

$this->request->redirect(self::INDEX_PAGE);
}

文章保存到数据库但重定向行给出错误:-

ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()

请告诉我如何进行重定向。

谢谢

【问题讨论】:

    标签: kohana-3


    【解决方案1】:

    您收到异常是因为从 Kohana 3.3 开始,Request 不再具有方法 redirect

    您可以通过替换来修复您的示例

    $this->request->redirect(self::INDEX_PAGE);

    HTTP::redirect(self::INDEX_PAGE);

    【讨论】:

      【解决方案2】:

      是的,Request::redirect 不再存在。因此,为了轻松地从 3.2 迁移到 3.3,我扩展了 Kohana_Request 类并添加了重定向方法。只需在 classes 文件夹中创建 Request.php 并写入

      class Request extends Kohana_Request {
      
          /**
           * Kohana Redirect Method
           * @param string $url
           */
          public function redirect($url) {
             HTTP::redirect($url);
          }
      
      }
      

      因此您将能够同时使用 Request::redirect$this->request->redirect

      【讨论】:

        【解决方案3】:

        在你的控制器$this->redirect('page');

        【讨论】:

          【解决方案4】:

          $this->redirect('article/index');

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-05-16
            • 1970-01-01
            • 1970-01-01
            • 2015-01-28
            • 2015-05-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多