Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend Framework-从viewscript中清除自定义占位符_Php_Zend Framework_Placeholder_Zend View_Zend Layout - Fatal编程技术网

Php Zend Framework-从viewscript中清除自定义占位符

Php Zend Framework-从viewscript中清除自定义占位符,php,zend-framework,placeholder,zend-view,zend-layout,Php,Zend Framework,Placeholder,Zend View,Zend Layout,我正在尝试从viewscript中清除自定义占位符,假设我有一个创建侧栏的控制器插件: $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); $view = $bootstrap->getResource('view'); $partial = $view->render('partials/_sidebar.phtml'); $view->pla

我正在尝试从viewscript中清除自定义占位符,假设我有一个创建侧栏的控制器插件:

    $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    $view = $bootstrap->getResource('view');
    $partial = $view->render('partials/_sidebar.phtml');
    $view->placeholder('sidebar')->append($partial);
My partial包含我的子菜单(通过Zend_导航视图帮助器呈现)

为了渲染该侧边栏,我在布局中有以下内容:

<?= $this->placeholder('sidebar'); ?>
你知道怎么做这样的事吗

谢谢

编辑: 我的问题其实很简单,因为我的插件是在postDispatch()方法中注册和执行的,然后我的viewscript在插件之前执行,布局在插件之后执行

从现在开始,我有什么选择?我不能在preDispatch方法中声明我的侧栏,因为没有任何脚本目录集,因此我无法确定在这一步执行哪个视图脚本

我也可以使用
action()
helper,你觉得怎么样?已经提出了一个问题。我仍然觉得这不是正确的方法,对我来说,这听起来太过分了

另外,另一个想法是将我的插件移动到控制器的preDispatch()方法中,但这会导致我在侧边栏的每个控制器上复制/粘贴,或者创建一个baseController,但我仍然不喜欢这个想法,我觉得我做得不对


有什么想法吗?

事实上你很接近。在本例中,您只需添加占位符的名称,
侧栏

$this->占位符('sidebar')->exchangeArray(数组())应该可以工作

编辑:

奇怪的是,上面的这些对你不起作用。我在自己的ZF网站上对此进行了测试,效果良好。可能是不同的情况。我不知道

这是另一种选择,尽管涉及面更广

//Get the placeholder object directly
$placeholderObj = $this->getHelper('placeholder')

//This may be why you were getting null in your example above. Since your were using the magic method in the View class to call the placeholder view helper method directly.

//For example you could call the placeholder like so:
$placeholderObj->placeholder('sidebar')->set("Some value");

//Now the alternative way to remove the placeholder value is as follows.
$registry = $placeholderObj->getRegistry()
$registry->deleteContainer('sidebar');
EDIT3

它应该使用preDispatch钩子作为Zend_控制器_插件。我认为您需要确保的唯一一点是,在设置视图脚本/助手路径和布局路径之后,您可以订购该插件。这可以在同一个插件或另一个插件中完成

下面是一个基于我在网站上使用的东西的代码示例。相关部分只是preDispatch中的内容,我在其中设置了视图脚本路径等

class RT_Theme extends Zend_Controller_Plugin_Abstract
{
    private $_sitename;
    private $_assets;
    private $_appPath;
    private $_appLibrary;

    /**
     * Constructor
     * 
     * @param string $sitename
     * @param SM_Assets $assets
     * @param string $appPath
     * @param string $appLibrary
     */
    public function __construct($sitename, $assets, $appPath, $appLibrary)
    {
        $this->_sitename = $sitename;
        $this->_assets = $assets;
        $this->_appPath = $appPath;
        $this->_appLibrary = $appLibrary;
    }

    /**
     * Sets up theme
     * 
     * Sets up theme template directory and assets locations (js, css, images)
     * 
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();
        $controller = $request->getControllerName();
        $action = $request->getActionName();

        $layout = Zend_Layout::getMvcInstance();
        $view = $layout->getView();

        $view->sitename = $this->_sitename;
        $view->headTitle()->setSeparator(' - ')->headTitle($view->sitename);

        $layout->setLayoutPath($this->_appPath . "/html/$module/layout/");

        $view->setScriptPath($this->_appPath . "/html/$module/view/");
        $view->addScriptPath($this->_appPath . "/html/$module/view/_partials/");

        $view->addScriptPath($this->_appLibrary . '/RT/View/Partials/');
        $view->addHelperPath('RT/View/Helper', 'RT_View_Helper');

        $view->addHelperPath($this->_appPath . '/html/view/helpers','My_View_Helper');

        $viewRenderer =
            Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->setView($view); 


        //Ignore this line. Not relevant to the question                 
        $this->_assets->loadFor($controller, $action);

        //Example: Now I can set the placeholder.
        $view->placeholder('header_title')->set($view->sitename);
    }
}

一个想法是设置一个标志,如
$view->isSidebarRenderable
,并在呈现侧边栏之前在布局中检查它。不雅观,但应该有效。我想过这个,但正如你所说,它似乎不雅观。谢谢你分享你的想法:)真的。我很想看看是否有人能想出更干净的东西。干杯找到我的问题,我编辑了我的问题。但我仍然在寻找最好的解决方案。我编辑了我的文章,以正确地撰写它(我不是第一次复制/粘贴)。注意,如果我忘记了参数,我会得到一个警告。而且,如果你阅读了我的整个问题,你会看到
Zend_Debug::dump($placeholder)输出为空对象。谢谢你的回答。嗯,我很惊讶这对你不起作用。这在我的一个网站上对我起了作用。我添加了我的原始答案,这有点罗嗦,但我想可以封装。这也可以解释为什么你得到了一个空的物体。是的,我在想这可能是问题所在。无论如何,我用一个示例编辑了我的问题,说明如何预先设置视图路径,以便可以使用preDispatch设置占位符。我几乎是直接复制的,所以您需要根据设置进行一些调整。
class RT_Theme extends Zend_Controller_Plugin_Abstract
{
    private $_sitename;
    private $_assets;
    private $_appPath;
    private $_appLibrary;

    /**
     * Constructor
     * 
     * @param string $sitename
     * @param SM_Assets $assets
     * @param string $appPath
     * @param string $appLibrary
     */
    public function __construct($sitename, $assets, $appPath, $appLibrary)
    {
        $this->_sitename = $sitename;
        $this->_assets = $assets;
        $this->_appPath = $appPath;
        $this->_appLibrary = $appLibrary;
    }

    /**
     * Sets up theme
     * 
     * Sets up theme template directory and assets locations (js, css, images)
     * 
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();
        $controller = $request->getControllerName();
        $action = $request->getActionName();

        $layout = Zend_Layout::getMvcInstance();
        $view = $layout->getView();

        $view->sitename = $this->_sitename;
        $view->headTitle()->setSeparator(' - ')->headTitle($view->sitename);

        $layout->setLayoutPath($this->_appPath . "/html/$module/layout/");

        $view->setScriptPath($this->_appPath . "/html/$module/view/");
        $view->addScriptPath($this->_appPath . "/html/$module/view/_partials/");

        $view->addScriptPath($this->_appLibrary . '/RT/View/Partials/');
        $view->addHelperPath('RT/View/Helper', 'RT_View_Helper');

        $view->addHelperPath($this->_appPath . '/html/view/helpers','My_View_Helper');

        $viewRenderer =
            Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->setView($view); 


        //Ignore this line. Not relevant to the question                 
        $this->_assets->loadFor($controller, $action);

        //Example: Now I can set the placeholder.
        $view->placeholder('header_title')->set($view->sitename);
    }
}