Zend framework2 如何更改ZendFramework2中控制器的布局?
我找到了这个话题并回答::: 我正在尝试这样做:Zend framework2 如何更改ZendFramework2中控制器的布局?,zend-framework2,Zend Framework2,我找到了这个话题并回答::: 我正在尝试这样做: public function loginAction() { if ($this->zfcUserAuthentication()->hasIdentity()) { return $this->redirect()->toRoute('zfcadmin'); } $this->layout('layout/login'); return new ViewModel(
public function loginAction() {
if ($this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcadmin');
}
$this->layout('layout/login');
return new ViewModel();
}
但它不起作用
当然我有文件MODULE\u DIR/view/layout/login.phtml
我试图var_dump($this->layout())代码>在设置布局之前和之后,显示在$this->layout('layout/login')之后更改布局代码>行。但事实并非如此
如何在控制器中设置不同的布局?
另外,如果布局更改,为什么我没有收到任何消息为什么加载标准版式而不是错误
我想,我必须在某个地方设置布局(例如,我设置了路线)。可能在config['view\u manager']['template\u map']
中添加如下内容:
$config = array(
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view'
),
'template_map' => array(
'layout/login' => __DIR__ . '/../view/layout/login.phtml',
),
),
);
-就像我说的:
当然,您也需要定义这些布局。。。检查一下
应用程序模块module.config.php
了解如何定义布局
这对我没有帮助:(
更新1
我试过这个:
public function loginAction() {
if ($this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcadmin');
}
$layout = $this->layout();
$layout->setTemplate('layout/login');
return new ViewModel();
}
$layout = $this->layout();
var_dump($layout->getTemplate());
$layout->setTemplate('layout/login');
var_dump($layout->getTemplate());
die();
正如建议的那样。不起作用:'(.相同的结果,没有返回新的ViewModel();
行
您可以自己查看文件:
- (登录)
- (为了确保我正确添加了
布局/登录
)
更新2
我试着按照你的建议调试
我更新了\u invoke
函数:
public function __invoke($template = null)
{
var_dump($template);
die();
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
有一些情况:
对于代码,您建议:
$layout = $this->layout();
$layout->setTemplate('layout/login');
它显示NULL。因此调用了方法,但$template
是NULL变量
关于代码,从,我在我的帖子开始时给出:
$this->layout('layout/login');
return new ViewModel();
它显示字符串(12)“布局/登录”
没有任何代码(因此加载了布局layout/admin
(默认为ZfcAdmin
),它显示:string(12)“layout/admin”
如果我加载我的站点的/
,它的页面将加载标准版式(在这两种情况下,无论是否在模块配置中加载版式/layout
)
更新3
我试过这个:
public function loginAction() {
if ($this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcadmin');
}
$layout = $this->layout();
$layout->setTemplate('layout/login');
return new ViewModel();
}
$layout = $this->layout();
var_dump($layout->getTemplate());
$layout->setTemplate('layout/login');
var_dump($layout->getTemplate());
die();
在控制器中。它显示:string(13)“layout/layout”string(12)“layout/login”
。因此布局被更改。但是standart layoutlayout/layout
呈现而不是layout/login
:(在module.config.php中的视图管理器的模板映射中添加布局
像这样:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array
'layout/login' => 'path_to_layout_file'
)
)
然后,在控制器中,尝试使用setTemplate()方法如下设置布局:
编辑,以下是来自Zend库的代码:
在内部Zend\Mvc\Controller\Plugin\Layout
注意这个方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
如果不提供模板,它将调用此方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
请注意[template:protected]=>布局/layout
,这就是为什么我说我认为Zend默认为该布局
因此,当您在控制器中使用$this->setTemplate('layout/login')
设置布局时,请进入该文件,在\u invoke
方法中执行echo$template;die;
,并查看它是否在那里被传递。这样您就可以更好地跟踪它
编辑:设置多个布局
这里有一种方法可以为模块设置布局,以减少冲突或被覆盖的可能性
'module_layouts' => array(
'_default' => 'layout/layout',
'admin' => 'layout/admin',
'foo' => 'layout/foo',
'login' => 'layout/login' // etc, etc
),
您的布局配置可以如下所示:
'zfcadmin' => array(
'use_admin_layout' => false,
),
在module.config.php中的视图管理器的模板映射中添加布局
像这样:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array
'layout/login' => 'path_to_layout_file'
)
)
然后,在控制器中,尝试使用setTemplate()方法如下设置布局:
编辑,以下是来自Zend库的代码:
在内部Zend\Mvc\Controller\Plugin\Layout
注意这个方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
如果不提供模板,它将调用此方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
请注意[template:protected]=>布局/layout
,这就是为什么我说我认为Zend默认为该布局
因此,当您在控制器中使用$this->setTemplate('layout/login')
设置布局时,请进入该文件,在\u invoke
方法中执行echo$template;die;
,并查看它是否在那里被传递。这样您就可以更好地跟踪它
编辑:设置多个布局
这里有一种方法可以为模块设置布局,以减少冲突或被覆盖的可能性
'module_layouts' => array(
'_default' => 'layout/layout',
'admin' => 'layout/admin',
'foo' => 'layout/foo',
'login' => 'layout/login' // etc, etc
),
您的布局配置可以如下所示:
'zfcadmin' => array(
'use_admin_layout' => false,
),
在module.config.php中的视图管理器的模板映射中添加布局
像这样:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array
'layout/login' => 'path_to_layout_file'
)
)
然后,在控制器中,尝试使用setTemplate()方法如下设置布局:
编辑,以下是来自Zend库的代码:
在内部Zend\Mvc\Controller\Plugin\Layout
注意这个方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
如果不提供模板,它将调用此方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
请注意[template:protected]=>布局/layout
,这就是为什么我说我认为Zend默认为该布局
因此,当您在控制器中使用$this->setTemplate('layout/login')
设置布局时,请进入该文件,在\u invoke
方法中执行echo$template;die;
,并查看它是否在那里被传递。这样您就可以更好地跟踪它
编辑:设置多个布局
这里有一种方法可以为模块设置布局,以减少冲突或被覆盖的可能性
'module_layouts' => array(
'_default' => 'layout/layout',
'admin' => 'layout/admin',
'foo' => 'layout/foo',
'login' => 'layout/login' // etc, etc
),
您的布局配置可以如下所示:
'zfcadmin' => array(
'use_admin_layout' => false,
),
在module.config.php中的视图管理器的模板映射中添加布局
像这样:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array
'layout/login' => 'path_to_layout_file'
)
)
然后,在控制器中,尝试使用setTemplate()方法如下设置布局:
编辑,以下是来自Zend库的代码:
在内部Zend\Mvc\Controller\Plugin\Layout
注意这个方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
如果不提供模板,它将调用此方法:
/**
* Invoke as a functor
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @param null|string $template
* @return Model|Layout
*/
public function __invoke($template = null)
{
if (null === $template) {
return $this->getViewModel();
}
return $this->setTemplate($template);
}
/**
* Retrieve the root view model from the event
*
* @return Model
* @throws Exception\DomainException
*/
protected function getViewModel()
{
$event = $this->getEvent();
$viewModel = $event->getViewModel();
echo '<pre>' . print_r($viewModel, true) . '</pre>';die;
if (!$viewModel instanceof Model) {
throw new Exception\DomainException('Layout plugin requires that event view model is populated');
}
return $viewModel;
}
注意[template:protected]=>布局/layout
,这就是为什么我说我认为Zend默认为该布局