2015-04-01 16 views
1

你好朋友我是zf2中的新朋友。我堅持在一個地方。在我的項目中,我想對許多操作調用一個視圖。 我的網址是「baseurl/g/any-thing-from-from-database」 我想從另一個模塊或同一個模塊調用「來自數據庫的任何事情」操作的視圖。如何打電話查看zf2中的dyanmic動作

我的G卡有GController.php

  namespace G\Controller; 

    use Zend\Mvc\Controller\AbstractActionController; 
    use Zend\View\Model\ViewModel; 
    use Zend\Stdlib\RequestInterface as Request; 
    use Zend\Stdlib\ResponseInterface as Response; 
    use Zend\View\Renderer\PhpRenderer; 
    use Students\Form\StudentsForm; 
    use Students\Model\Students; 


    class GController extends AbstractActionController 
      { 
       public function dispatch(Request $request, Response $response = null) 
         { 
          $controller = $this->params('controller'); 
          $nicname = $this->params('action'); 
          if($nicname !== false){ 

           $hosts = $this->getServiceLocator()->get('Manager\Model\HostsTable'); 

           if(($data = $hosts->findByNicname($nicname)) !== null){ 


            $captchaService = $this->getServiceLocator()->get('SanCaptcha'); 

            $form = new StudentsForm($captchaService); 

            return array('from'=>$form); 
           } 
          } 

           return $this->redirect()->toRoute('home',array('controller'=>'application','action'=>'index')); 
         } 


        public function gAction() 
        { 
         return new ViewModel(); 
        } 

      } 

上module.config.php

return array(
'controllers' => array(
    'invokables' => array(
     'G\Controller\G' => 'G\Controller\GController', 
    ), 
), 


'router' => array(
    'routes' => array(
     'g' => array(
      'type' => 'segment', 
      'options' => array(
       'route' => '/g[/:action][/:id]', 
       'constraints' => array(
        'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'id'  => '[0-9]+', 
       ), 
       'defaults' => array(
        'controller' => 'G\Controller\G', 
        'action'  => 'g', 
       ), 
      ), 
     ), 
    ), 
), 

'view_manager' => array(

    'template_path_stack' => array(
     'g' => __DIR__ . '/../view', 
    ), 
), 
); 

這個代碼從我用的是正常工作調度功能的URL不同的動作。當我從數據庫中得到這個動作時,我想用來自不同模塊的一些名爲學生或G的內容顯示錶單。但是,此代碼只顯示頁眉和頁腳,沒有任何其他錯誤。請幫助我。提前感謝。

回答

0

我覺得覆蓋派遣方法不能很好。

我相信你沒有看到任何東西,因爲你沒有渲染ViewModel,所以沒有數據。這是因爲您禁用了默認分派行爲並覆蓋了路由中的動作參數。因此,如果您打開http://my.website/g/foobar,那麼foobarAction()將被調用 - 如果您的調度將正常工作。所以你可以做的就是簡單地將你的動作參數重命名爲(例如)foo,把你的邏輯改爲gAction(),並且做任何你必須處理的事情。$ this-> param('foo')

+0

感謝您的回覆,但如何啓用默認調度行爲 – user3666505 2015-04-02 07:36:53

+0

我的意思是不覆蓋調度方法。 – danopz 2015-04-03 14:05:07

+0

你明白了嗎? – danopz 2015-08-20 12:13:54