2014-02-18 140 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方便地調試問題。