在哪里是为zend basePath视图助手设置的basepath变量(也许是工厂)

时间:2017-01-07 19:44:32

标签: zend-framework zend-framework2 zend-view

我想看看base path view helper如何在帮助程序类中设置基本路径变量。

这是一个基于内部的问题,因为我想它是在幕后工厂完成的。

我需要使用自定义版本复制它,但我现在正在硬编码基本路径:You'll see that even though its extending the basepath viewhelper i cannot configure the basepath variable without this current solution of hardcoding it

class PlutoBasePath extends \Zend\View\Helper\BasePath
{

 public function __construct()
 {
    /**
     * @todo
     * @var Ambiguous $basePath
     */
    $this->basePath = Pluto::registry('prepend_location_url');  
 } 

 public function __invoke($file = null)
 {
        if (null === $this->basePath) {
            throw new Exception\RuntimeException('No base path provided');
        }

        if (null !== $file) {           
            \Pluto\Stdlib\FilesystemUtils::sanitizeFilePaths($file);
            \Pluto\Stdlib\FilesystemUtils::trimLeadingPath($file);                  
        }                       
        return $this->basePath.$file;
 }
}

我宁愿使用工厂,但我不知道如何访问基本路径设置逻辑为基本路径视图助手设置工厂基础路径 to setup the custom base path correctly

我怎么可能看到工厂创建基本路径视图助手是我的基本问题

2 个答案:

答案 0 :(得分:1)

我似乎找到了基本路径的设置位置,Zend\Mvc\Service\ViewHelperManagerFactory::createBasePathHelperFactory()

private function createBasePathHelperFactory(ContainerInterface $services)
    {
        return function () use ($services) {
            $config = $services->has('config') ? $services->get('config') : [];
            $helper = new ViewHelper\BasePath;

            if (Console::isConsole()
                && isset($config['view_manager']['base_path_console'])
            ) {
                $helper->setBasePath($config['view_manager']['base_path_console']);
                return $helper;
            }

            if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
                $helper->setBasePath($config['view_manager']['base_path']);
                return $helper;
            }

            $request = $services->get('Request');

            if (is_callable([$request, 'getBasePath'])) {
                $helper->setBasePath($request->getBasePath());
            }

            return $helper;
        };
    }

我希望这会有所帮助

答案 1 :(得分:0)

以下是我在我的应用程序中使用的一段代码,它应该回答您的问题(根据项目的架构进行调整)

    use Zend\View\Renderer\PhpRenderer;
    use Zend\View\Resolver;
    ...
    $stack = new Resolver\TemplatePathStack(
        [
            'script_paths' => [
                __DIR__ . '/../../../view'
            ]
        ]);
    $resolver = new Resolver\AggregateResolver();
    $resolver->attach($stack);
    $renderer = new PhpRenderer();
    $renderer->setResolver($resolver)
        ->plugin('basePath')
        ->setBasePath('/');