Zend 覆盖默认视图对象

发布于 2024-11-08 14:49:13 字数 1095 浏览 0 评论 0原文

如何覆盖 zend 框架中的默认视图对象,以便我可以拥有自定义视图对象?

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    function _initViewHelpers() { 
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('HTML4_STRICT');
        $view->setHelperPath(APPLICATION_PATH . '/helpers', '');        
        $view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
                         ->appendName('description', 'Zend Framework');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Zend Custom View');
        $view->setScriptPath(APPLICATION_PATH . '/themes/admin');
        
        return $view;
    }
}

默认视图包含模块的默认脚本路径。我想要所有模块的一条路径,以启用模板系统。 setScriptPath 方法应该覆盖视图对象生成的默认路径,但事实并非如此。

array(2) { [0]=> string(66) "C:/xampp/htdocs/NEOBBS_v6/application/modules/admin/views\scripts/" [1]=> string(51) "C:\xampp\htdocs\NEOBBS_v6\application/themes/admin/" }

它有两个 scriptPath。这可以通过覆盖默认视图对象来完成吗?

How can I overwrite the default view object in zend framework so I could have the custom one?

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    function _initViewHelpers() { 
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('HTML4_STRICT');
        $view->setHelperPath(APPLICATION_PATH . '/helpers', '');        
        $view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
                         ->appendName('description', 'Zend Framework');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Zend Custom View');
        $view->setScriptPath(APPLICATION_PATH . '/themes/admin');
        
        return $view;
    }
}

The default view contains default script path for module. I want one path for all modules, to enable template system. The setScriptPath method should overwrite the default path generated by the view object, but it doesn't.

array(2) { [0]=> string(66) "C:/xampp/htdocs/NEOBBS_v6/application/modules/admin/views\scripts/" [1]=> string(51) "C:\xampp\htdocs\NEOBBS_v6\application/themes/admin/" }

it has two scriptPaths. Can this be done by overwriting the default view object?

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

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

发布评论

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

评论(2

泡沫很甜 2024-11-15 14:49:13

ArneRie 发布的内容是正确的,但是 ViewRenderer 检查是否设置了标准脚本路径,如果没有设置则添加它。由于路径是按后进先出的方式检查的,因此 ViewRenderer 在您的路径之后添加标准路径,然后始终使用该路径。

对我有用的是同时设置标准路径和自定义路径,自定义路径位于最后,类似于:

$view->setScriptPath(array(
    APPLICATION_PATH . '/views/scripts/', // or whatever the standard path is
    APPLICATION_PATH . '/themes/admin'
));

不过可能有更好的解决方案。

What ArneRie posted is correct, however the ViewRenderer checks to see whether the standard script path is set and adds it if not. Since the paths are checked LIFO, what's happening is that the ViewRenderer is adding the standard path after your one and then always using that one.

What worked for me was to set both the standard path and my custom path at the same time, with the custom one being last, something like:

$view->setScriptPath(array(
    APPLICATION_PATH . '/views/scripts/', // or whatever the standard path is
    APPLICATION_PATH . '/themes/admin'
));

there may be a better solution for this though.

人海汹涌 2024-11-15 14:49:13

尝试添加:

        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

Try to add:

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