更改 Bootstrap 中的布局

发布于 2024-11-03 04:28:44 字数 409 浏览 2 评论 0原文

我是 zend 新手,在更改引导程序中的布局时遇到问题。我想在用户登录时更改我的布局。

我在引导程序中更改布局的函数如下:

protected function _initAuthState()
{
$layout = new Zend_Layout;
$layout->setLayoutPath('/layouts/scripts');

if (Zend_Auth::getInstance()->hasIdentity()):
// Logged in.
$layout->setLayout(layout2);

else:
// Not Logged in.
$layout->setLayout(‘layout’);
endif;
}

此代码不起作用,布局始终相同......救命!

I am new with zend and I have a problem changing my layout in the bootstrap. I want to change my layout when the user is logged in.

My function to change the layout in the bootstrap is like this:

protected function _initAuthState()
{
$layout = new Zend_Layout;
$layout->setLayoutPath('/layouts/scripts');

if (Zend_Auth::getInstance()->hasIdentity()):
// Logged in.
$layout->setLayout(layout2);

else:
// Not Logged in.
$layout->setLayout(‘layout’);
endif;
}

This code doesn't work, the layout is always the same ... help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-11-10 04:28:44

您正在修改布局实例,而不是系统正在使用的实例。

我假设您在 application.ini 中指定布局参数。所以你需要:

$this->bootstrap('layout');
$layout = $this->getResource('layout');

然后对此布局实例执行检查/修改。

顺便说一句,更改布局通常是使用前端控制器插件来完成的。仍然足够早地运行来完成这项工作,但通常更可配置和可重用。请参阅此处这里举两个例子。

You are modifying a new layout instance, not the instance that is being used by the system.

I assume you are specifying your layout params in application.ini. So you need:

$this->bootstrap('layout');
$layout = $this->getResource('layout');

Then perform your check/modification on this layout instance.

BTW, changing layout is often done using a front-controller plugin. Still runs early enough to do the job, but is often more configurable and re-usable. See here and here for two examples.

哭泣的笑容 2024-11-10 04:28:44

我找到了答案!!

这是我的最终结果,并且正在工作!

Bootstrap.php:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initLoader(){
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => '../application/',
        'namespace' => 'My',
        ));

        $resourceLoader->addResourceTypes(array(
            'plugin' => array(
                'path'      => 'plugins/',
                'namespace' => 'Plugin',
            )
        ));
    }

    public function _initPlugins()
    {
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new My_Plugin_Layout());
    }
}

应用程序/插件/Layout.php:

<?php
class My_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        $user = Zend_Auth::getInstance();
        $role = $user->getIdentity()->role;
        $layout = Zend_Layout::getMvcInstance();

        switch ($role) {
            case 'admin':
                $layout->setLayout('layout2');
                break;

            case 'normal':
                $layout->setLayout('layout');
                break;

            default:
                $layout->setLayout('layout');
                break;
        }
    }
}
?>

I found the answer!!

this is my final result, and is working!!

Bootstrap.php:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initLoader(){
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => '../application/',
        'namespace' => 'My',
        ));

        $resourceLoader->addResourceTypes(array(
            'plugin' => array(
                'path'      => 'plugins/',
                'namespace' => 'Plugin',
            )
        ));
    }

    public function _initPlugins()
    {
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new My_Plugin_Layout());
    }
}

application/plugins/Layout.php:

<?php
class My_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        $user = Zend_Auth::getInstance();
        $role = $user->getIdentity()->role;
        $layout = Zend_Layout::getMvcInstance();

        switch ($role) {
            case 'admin':
                $layout->setLayout('layout2');
                break;

            case 'normal':
                $layout->setLayout('layout');
                break;

            default:
                $layout->setLayout('layout');
                break;
        }
    }
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文