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

148 views Asked by At

Id like to see how the base path view helper sets up the base path variable inside the helper class.

This is an internal based questions, as I imagine its being done with a factory behind the scenes.

I needed to replicate it with a custom version but im hardcoding the base path currently: 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;
 }
}

Id rather use a factory but I dont know how to access the base path set logic WHICH SETS UP THE FACTORY BASE PATH FOR THE base path view helper to setup the custom base path correctly

How is it possible for me to see the factory creation of the base path view helper is my base question

2

There are 2 answers

0
Garry On BEST ANSWER

I seem to have found where the basepath is set, here 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;
        };
    }

I hope this helps

0
Alain Pomirol On

Here is a piece of code that I use in my application and that should answer your question (to be adapted according to the schema of your project)

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