Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\App;
7 
8 class View implements ViewInterface
9 {
13  protected $_layout;
14 
18  protected $_configScope;
19 
23  protected $_eventManager;
24 
28  protected $page;
29 
33  protected $_actionFlag;
34 
38  protected $_response;
39 
43  protected $_request;
44 
48  protected $_isLayoutLoaded = false;
49 
59  public function __construct(
60  \Magento\Framework\View\LayoutInterface $layout,
63  \Magento\Framework\Config\ScopeInterface $configScope,
64  \Magento\Framework\Event\ManagerInterface $eventManager,
65  \Magento\Framework\View\Result\PageFactory $pageFactory,
66  ActionFlag $actionFlag
67  ) {
68  $this->_layout = $layout;
69  $this->_request = $request;
70  $this->_response = $response;
71  $this->_configScope = $configScope;
72  $this->_eventManager = $eventManager;
73  $this->_actionFlag = $actionFlag;
74  $this->page = $pageFactory->create(true);
75  }
76 
82  public function getPage()
83  {
84  return $this->page;
85  }
86 
92  public function getLayout()
93  {
94  return $this->page->getLayout();
95  }
96 
100  public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true, $addActionHandles = true)
101  {
102  if ($this->_isLayoutLoaded) {
103  throw new \RuntimeException('Layout must be loaded only once.');
104  }
105  // if handles were specified in arguments load them first
106  if (!empty($handles)) {
107  $this->getLayout()->getUpdate()->addHandle($handles);
108  }
109 
110  if ($addActionHandles) {
111  // add default layout handles for this action
112  $this->page->initLayout();
113  }
114  $this->loadLayoutUpdates();
115 
116  if (!$generateXml) {
117  return $this;
118  }
119  $this->generateLayoutXml();
120 
121  if (!$generateBlocks) {
122  return $this;
123  }
124  $this->generateLayoutBlocks();
125  $this->_isLayoutLoaded = true;
126 
127  return $this;
128  }
129 
135  public function getDefaultLayoutHandle()
136  {
137  return $this->page->getDefaultLayoutHandle();
138  }
139 
145  public function addActionLayoutHandles()
146  {
147  $this->getLayout()->getUpdate()->addHandle($this->getDefaultLayoutHandle());
148  return $this;
149  }
150 
158  public function addPageLayoutHandles(array $parameters = [], $defaultHandle = null)
159  {
160  return $this->page->addPageLayoutHandles($parameters, $defaultHandle);
161  }
162 
168  public function loadLayoutUpdates()
169  {
170  $this->page->getConfig()->publicBuild();
171  return $this;
172  }
173 
179  public function generateLayoutXml()
180  {
181  $this->page->getConfig()->publicBuild();
182  return $this;
183  }
184 
190  public function generateLayoutBlocks()
191  {
192  $this->page->getConfig()->publicBuild();
193  return $this;
194  }
195 
202  public function renderLayout($output = '')
203  {
204  if ($this->_actionFlag->get('', 'no-renderLayout')) {
205  return $this;
206  }
207 
208  \Magento\Framework\Profiler::start('LAYOUT');
209 
210  \Magento\Framework\Profiler::start('layout_render');
211 
212  if ('' !== $output) {
213  $this->getLayout()->addOutputElement($output);
214  }
215 
216  $this->_eventManager->dispatch('controller_action_layout_render_before');
217  $this->_eventManager->dispatch(
218  'controller_action_layout_render_before_' . $this->_request->getFullActionName()
219  );
220 
221  $this->page->renderResult($this->_response);
222  \Magento\Framework\Profiler::stop('layout_render');
223 
224  \Magento\Framework\Profiler::stop('LAYOUT');
225  return $this;
226  }
227 
234  public function setIsLayoutLoaded($value)
235  {
236  $this->_isLayoutLoaded = $value;
237  }
238 
244  public function isLayoutLoaded()
245  {
246  return $this->_isLayoutLoaded;
247  }
248 }
renderLayout($output='')
Definition: View.php:202
$response
Definition: 404.php:11
__construct(\Magento\Framework\View\LayoutInterface $layout, RequestInterface $request, ResponseInterface $response, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\View\Result\PageFactory $pageFactory, ActionFlag $actionFlag)
Definition: View.php:59
$value
Definition: gender.phtml:16
addPageLayoutHandles(array $parameters=[], $defaultHandle=null)
Definition: View.php:158
loadLayout($handles=null, $generateBlocks=true, $generateXml=true, $addActionHandles=true)
Definition: View.php:100