return $ this-> redirect() - > toRoute()在zend框架中不起作用

时间:2014-02-18 12:18:18

标签: php doctrine-orm doctrine zend-framework2

我的问题很小但我没有找到解决方案。我在Zend Framework 2中使用了doctrine,当我将数据刷新到我希望重定向到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',
),

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

enter image description here

2 个答案:

答案 0 :(得分: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,以便轻松调试您的问题。

答案 1 :(得分:-3)

试试这个:

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

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