2014-02-18 141 views
0

我的问题很小,但我没有找到解决方案。我在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', 
), 

当我按下添加按钮正确添加到数据数据库,但不会重定向到我的博客路线。这是从我得到了一个形象:)

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

toRoute(Zend框架方法2似乎只是设置重定向的信息,但是并不:

enter image description here

+1

您的网络服务器是否正在运行?检查路线是否存在。请学习ZF2文档。你的代码是非常不安全的。 – Sam

+0

是的,我是zend的初学者,代码不安全?正确的地方!是的网络服务器运行良好 – Mohammadov

+1

@Mohammadov ::山姆是正确的,窗体验证在哪里?记住一件事,'永远不要相信用户输入!!' –

回答

-4

试试这个实际上将它们发送到浏览器。

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', 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
); 

另外,你应该confgure你的php.ini的display_errors与= ON方便地调试问题。