Getting Started with Zend Framework 2 - View

1. Call a custom view


By default, the action and view in the controller are one-to-one correspondence, such as HellowordController::indexAction()(path:/module/Application/src/Application/Controller/HellowordController.php) The corresponding view file is index.phtml (path:/module/Application/view/application/helloword/index.phtml).
Views can also be customized in ZF2:

First create your own custom view file my.phtml (of course it does not have to be a phtml file)
(path://module/Application/view/application/myviews/my.phtml)
Then call directly in the controller:
 <?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class HellowordController exntends AbstractActionController
{
    public function indexAction()
    {
        $view = new ViewModel();
        $view->setTemplate('application/myviews/my.phtml');
        return $view;
    }
}
You can also refer to the default 'error/404' method and add your "view_manager"configuration information to the module.config.php file
<?php
return array(

    ...

   'view_manager' => array(
       'template_path_stack' => array(
           __DIR__ . '/../view',
       ),
       'template_map' => array(
            'myviews/my' => __DIR__ . '/../view/application/myviews/my.phtml',
        ),
    ),

    ...
Then you can actually use "myviews/my"in the view
$view = new ViewModel();
$view->setTemplate('myviews/my');
return $view;

2. Specify the layout


The method is similar to the above, first configure the layout parameters in module.config.php
return array(
    'view_manager' => array(
	 'template_map' => array(
	     'myviews/layout'   => __DIR__ . '/../view/layout/layout.phtml',
	 ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);
 : module.config.php view , view_manager , 
Then you can decorate the current layout in the controller
public function indexAction()
{
    $this->layout('myviews/layout');
}

3. Call the corresponding view directly without using layout


public function indexAction()
{
    $view  = new ViewModel();
    $view->setTerminal(true);     // layout
    $view->setVariables(array()); // view 
    return $view;
}

4. Pass parameters in the view to the layout


For example, pass parameters to layout.phtml in index.phtml
index.phtml :
$this->layout()->helloword = 'Hi';
layout.phtml:
echo $this->layout()->helloword; //output: Hi

5. Print out parameters directly without using views


IndexController.php
public function indexAction()
{
     return $this->getResponse()
                 ->setStatusCode(200)
                 ->setContent('foo');
}

//optput foo

6. Pass parameters directly to the layout in the action


Module.php:
class Module
{
    ...
    public function onBootstrap( $e )
    {
	$eventManager = $e->getApplication()->getEventManager();
	$eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 100 );
    }

     public function preDispatch($e) 
    {
    	...
    	$e->getViewModel()->setVariable('names', 'start');
        ...
    }
    ...
}
layout.phtml:
echo $this->layout()->names;
//output start

좋은 웹페이지 즐겨찾기