如何在 ZendFramework2 中更改控制器中的布局? [英] How to change layout in controller in ZendFramework2?

查看:22
本文介绍了如何在 ZendFramework2 中更改控制器中的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个话题并回答:在 Zend Framework 2.0 的控制器中更改布局 :: 答案

I found this topic and answer: Change layout in the controller of Zend Framework 2.0 :: Answer

我正在尝试这样做:

public function loginAction() {
    if ($this->zfcUserAuthentication()->hasIdentity()) {
        return $this->redirect()->toRoute('zfcadmin');
    }
    $this->layout('layout/login');
    return new ViewModel();
}

但它不起作用.

当然我有文件 MODULE_DIR/view/layout/login.phtml.

我尝试了 var_dump($this->layout()); 在设置布局之前和之后它显示,在 $this->layout('布局/登录'); 行.但事实并非如此.

I tried to var_dump($this->layout()); before setting layout and after it and it shows, that layout is changed after $this->layout('layout/login'); line. But it is not.

如何在控制器中设置不同的布局?

此外,为什么更改布局后我没有收到任何消息?为什么加载标准布局而不是错误?

我想,我必须在某处设置布局(例如我设置路线).可能在配置 ['view_manager']['template_map'] 中添加如下内容:

I think, I have to set up layout somewhere (like I set routes, for example). Possibly in config ['view_manager']['template_map'] by adding something like:

$config = array(
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view'
        ),        
        'template_map' => array(
            'layout/login'           => __DIR__ . '/../view/layout/login.phtml',
        ),
    ),
);

——就像说那里:

当然,您也需要定义这些布局...只需检查应用程序模块 module.config.php 查看如何定义布局.

Of course you need to define those layouts, too... just check Application Modules module.config.php to see how to define a layout.

这对我没有帮助:(

试过这个:

public function loginAction() {
    if ($this->zfcUserAuthentication()->hasIdentity()) {
        return $this->redirect()->toRoute('zfcadmin');
    }
    $layout = $this->layout();
    $layout->setTemplate('layout/login');
    return new ViewModel();
}

按照 @alex 的建议.不起作用 :'(.没有 return new ViewModel(); 行的相同结果.

as @alex suggested. Doesn't work :'(. Same result without return new ViewModel(); line.

您自己查看文件:

  • AdminController.php (loginAction)
  • module.config.php (to be sure I added layout/login correctly

我尝试按照您的建议进行调试.

I tried to debug as you suggest.

我更新了 __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 是空变量.

it displays NULL. So method is called, but $template is null variable.

使用代码,来自帖子,我在帖子开头给出了:

With code, from post, I had given in the start of my post:

$this->layout('layout/login');
return new ViewModel();

显示string(12) "layout/login".

没有任何代码(所以布局 layout/admin 已加载(ZfcAdmin 的默认设置),它显示:string(12) "layout/admin".

Without any code (so layout layout/admin loaded (default for ZfcAdmin), it shows: string(12) "layout/admin".

如果我加载我网站的 /,它的页面会加载标准布局(在这两种情况下,模块配置中有或没有 layout/layout.

If I load / of my site it page is loaded with standart layout (in both cases with or without layout/layout in module config.

我试过了:

$layout = $this->layout();
var_dump($layout->getTemplate());
$layout->setTemplate('layout/login');
var_dump($layout->getTemplate());
die();

在控制器中.它显示:string(13) "layout/layout" string(12) "layout/login".所以布局改变了.但是标准布局 layout/layout 呈现而不是 layout/login.:(

in controller. It shows: string(13) "layout/layout" string(12) "layout/login". So layout is changed. But standart layout layout/layout rendered instead of layout/login. :(

推荐答案

因为您使用的是 ZfcAdmin 并且 启用了 use_admin_layout 选项在该模块中,您尝试设置布局的登录路由是 ZfcAdmin 的子路由,管理布局侦听器正在启动并覆盖您尝试设置的模板您的控制器操作.

Because you're using ZfcAdmin and have the use_admin_layout option enabled in that module and the login route you're attempting to set a layout on is a child route of ZfcAdmin, the admin layout listener is kicking in and over-writing the template you're attempting to set in your controller action.

禁用 zfcadmin 布局、编写自己的侦听器并在那里处理登录布局的特定情况可能是最简单的.您可以使用与 ZfcAdmin 在 Module.php 中使用的基本相同的方法来做到这一点,只需进行一两次调整...

It's perhaps easiest to disable zfcadmin layout, write your own listener and handle the specific case of login layout there. You can do that using essentially the same method that ZfcAdmin uses in Module.php with a tweak or two ...

请务必禁用 ZfcAdmin 布局

Be sure to disable ZfcAdmin layout

'zfcadmin' => array(
    'use_admin_layout' => false,
),

然后,使用您的模块名称作为配置键,设置您自己的相同配置版本...

then, using your module name as a config key, set up your own version of the same config ...

'myzfcadmin' => array(
    'use_admin_layout' => true,
    'admin_layout_template' => 'layout/admin',
    // you could even define a login layout template here
    'login_layout_template' => 'layout/login',
),

接下来在 MyZfcAdmin/Module.php 添加一个监听器,几乎与 ZfcAdmin 中的监听器完全一样,只是让它检查你的 myzfcadmin 配置值......

Next in MyZfcAdmin/Module.php add a listener, almost exactly like the one in ZfcAdmin only have it check your myzfcadmin config values instead ...

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getParam('application');
    $em = $app->getEventManager();

    $em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'selectLayoutBasedOnRoute'));
}

public function selectLayoutBasedOnRoute(MvcEvent $e)
{
    $app = $e->getParam('application');
    $sm = $app->getServiceManager();
    $config = $sm->get('config');

    if (false === $config['myzfcadmin']['use_admin_layout']) {
        return;
    }

    $match = $e->getRouteMatch();
    $controller = $e->getTarget();

    if (!$match instanceof \Zend\Mvc\Router\RouteMatch
        || 0 !== strpos($match->getMatchedRouteName(), 'zfcadmin')
        || $controller->getEvent()->getResult()->terminate()
    ) {
        return;
    }

    if ($controller instanceof \MyZfcAdmin\Controller\AdminController
        && $match->getParam('action') == 'login'
    ) {
        // if you'd rather just set the layout in your controller action just return here
        // return;
        // otherwise, use the configured login layout ..
        $layout = $config['myzfcadmin']['login_layout_template'];
    } else {
        $layout = $config['myzfcadmin']['admin_layout_template'];
    }

    $controller->layout($layout);                
}

如您所见,我添加了代码来检查控制器是否是您特定的 AdminController 实例和登录操作,如果是,请设置备用模板,否则使用默认模板,现在无需在您的控制器中担心.

As you can see, I added code to check the controller is your specific AdminController instance and login action, and if so, set the alternate template otherwise use the default, no need to worry about it in your controller now.

这篇关于如何在 ZendFramework2 中更改控制器中的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆