我在Zend Framewor 2中的解決方案很簡單。對於索引我傾向於致電parrent :: indexAction()構造函數bcs我們擴展Zend \ Mvc \ Controller \ AbstractActionController。或者只是在indexAction中返回數組()()。 ZF將自動返回index.pthml,確定必須返回的內容。
返回新ViewManager()相同返回陣列()
<?php
namespace Test\Controller;
use Zend\Mvc\Controller\AbstractActionController,
Zend\View\Model\ViewModel;
// Or if u write Restful web service then use RestfulController
// use Zend\Mvc\Controller\AbstractRestfulController
class TestController extends AbstractActionController
{
/*
* Index action
*
* @return main index.phtml
*/
public function indexAction()
{
parent::indexAction();
// or return new ViewModel();
// or much simple return array();
}
/*
* Add new comment
*
* @return addComment.phtml
*/
public function addAction()
{
$view = new ViewManager();
$view->setTemplate('test/test/addComment.phtml'); // module/Test/view/test/test/
return $view;
}
不要忘記配置在模塊/配置/ module_config
'view_manager' => array(
'template_path_stack' => array(
'Test' => __DIR__ . '/../view',
),
),
HTTP路由和view_manager:// zf2test.akrabat.com/ – Developer
https://github.com/akrabat/ZF2TestApp/blob/master/module/Application/config/module.config.php#L78 – Developer
+1,完美答案!!! – SagarPPanchal