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

where is the basepath variable being set for the zend basePath view helper (maybe a factory)

我想看看 base path view helper 如何在助手 class 中设置基本路径变量。

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

我需要用自定义版本复制它,但我目前对基本路径进行了硬编码: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

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

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

    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('/');

我好像找到了设置basepath的地方,这里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;
        };
    }

希望对您有所帮助