【问题标题】:return $this->redirect()->toRoute() doesn't work in zend frameworkreturn $this->redirect()->toRoute() 在 zend 框架中不起作用
【发布时间】:2014-02-18 12:18:18
【问题描述】:

我的问题很小,但我没有找到解决方案。我将教义与 Zend Framework 2 一起使用,当我将数据刷新到要重定向到 blog 路由的数据库时,就会出现问题。它不工作。 这是我的行动:

public function addAction()
{
    if ($this->request->isPost()) 
    {
        $article = new Article();
        $article->setTitle($this->getRequest()->getPost('title'));
        $article->setDate(new \DateTime());
        $article->setContent($this->getRequest()->getPost('content'));
        $article->setPublication($this->getRequest()->getPost('publication'));

        $this->getObjectManager()->persist($article);
        $this->getObjectManager()->flush();
        $newId = $article->getId();

        return $this->redirect()->toRoute('blog');
    }
    return new ViewModel();
}

这是我的观点:

<form class="contact_form" method="post" >
    <ul>
        <li>
            <h2>Add Article</h2>
            <span class="required_notification">* Required Field</span>
        </li>
        <li>
            <label>Publication:</label>
            <input type="checkbox" name="publication" required />
        </li>
        <li>
            <label>Title:</label>
            <input type="text" name="title" required />
        </li>
        <li>
            <label>Date:</label>
            <input type="date" name="date" name="date" required />
        </li>
        <li>
            <label>Content:</label>
            <textarea name="content" cols="40" rows="6" required ></textarea>
        </li>
        <li>
            <button class="submit" type="submit">Add</button>
        </li>
    </ul>
</form>

最后这是我的路线:

'add' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route'    => '/blog/article/add',
        'defaults' => array(
            'controller' => 'Application\Controller\Blog',
            'action'     => 'add',
        ),
    ),
),
'defaults' => array(
    '__NAMESPACE__' => 'Application\Controller',
    'controller'    => 'Blog',
    'action'        => 'add',
),
'template_map' => array(
    'layout/layout'        => __DIR__ . '/../view/layout/layout.phtml',
    'application/blog/add' => __DIR__ . '/../view/application/blog/add.phtml',
    'error/404'            => __DIR__ . '/../view/error/404.phtml',
    'error/index'          => __DIR__ . '/../view/error/index.phtml',
),

当我按下添加按钮时,数据会正确添加到数据库中,但不会重定向到我的博客路由。这是我得到的图像:

【问题讨论】:

  • 你的网络服务器还在运行吗?检查路由是否存在。请参考 ZF2 文档。您的代码非常不安全。
  • 是的,我是 zend 的初学者,代码不安全?到底在哪里!!是的,网络服务器运行良好
  • @Mohammadov:: Sam 是对的,表单验证在哪里?记住一件事,Never trust User Input!!

标签: php doctrine-orm doctrine zend-framework2


【解决方案1】:

您的配置中没有名为 blog 的路由。

您必须指定一个名为 blog 的路由,如本例所示:

<?php
return array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),
        'blog' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/blog',
                'defaults' => array(
                    'controller' => 'Application\Controller\Blog',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'add' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route' => '/article/add',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Blog',
                            'action' => 'add',
                        ),
                    ),
                ),
            ),
        ),
    ),
);

另外,您应该将您的 php.ini 配置为 display_errors = ON 以轻松调试您的问题。

【讨论】:

    【解决方案2】:

    试试这个:

     $this->redirect()->toRoute('blog');
     $this->getResponse()->sendHeaders();
     exit();
    

    Zend Framework 2 中的 toRoute() 方法似乎只是设置了重定向标头,但实际上并没有将它们发送到浏览器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多