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 controllerpublic 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Getting Started with Zend Framework 2 - ViewBy default, the action and view in the controller are one-to-one correspondence, such as HellowordController::indexActio...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.