質問

I'm currently working at a library for Zend 2, that is among others supposed to render view scripts. The rendering is working fine and I'm also able to use any other helper within the view script, but the basePath() helper is throwing an exception with the message:

No base path provided

I've already tried to set the basePath in the config, but it's only changing in the view scripts rendered by a controller. In case it's relevant, here's the code that is rendering the view:

// file: /vendor/mate/library/mate/Group/Functions/GetHtml.php
// $templateDir is either directing to /module/Application/view/group/<name>.phtml
// or to /vendor/mate/library/mate/view/group/<name>.phtml

    $map = new Resolver\TemplateMapResolver(array(
        'group/'.$type => $templateDir,
    ));
    $stack = new Resolver\TemplatePathStack(array(
        'script_paths' => array(
            $viewDir,
        )
    ));

    $resolver->attach($map)    // this will be consulted first
             ->attach($stack);

    $groupModel = new ViewModel(array(
        'elements' => $this->getGroup()->toArray(),
        'groupElement' => $groupElement,
    ));
    $groupModel->setTemplate('group/'.$type);
    $groupHtml = $renderer->render($groupModel);

Has anyone any idea how to get the basePath to work in my view scripts?

役に立ちましたか?

解決

The base path of the helper must explicitly be set. Grab the plugin from the renderer and set the base path first before rendering:

// create your view model

$renderer->plugin('basePath')->setBasePath('/foo');

// render view model now

他のヒント

The problem simply comes because you are creating a new, separate instance of the PhpRenderer rather than using the one which already exists.

If you get the renderer via the service manager instead of creating a new one, you will not need any of this configuration.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top