mtymek/blast-base-url

no issue - just an idea

Opened this issue · 3 comments

(...this is in some way related to the discussion about the helper factory)

Since you are using zend-view (which includes its own helper), we could just use the zend-view BasePath view helper and just call setBasePath on it without having a custom view helper which basically does the same.

i tried it and it works just fine (zend-servicemanager3), see following code fragments:

//...
use Zend\View\HelperPluginManager;
use Zend\View\Helper\BasePath;
//...
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        //...
        if ($this->container->has(HelperPluginManager::class)) {
            $manager = $this->container->get(HelperPluginManager::class);
            if ($manager->has(BasePath::class)) {
                $helper = $manager->get(BasePath::class);
                $helper->setBasePath($this->basePath . '/');
            }
        }
        //...
        return $next($request, $response);
    }

you can close the issue of course since it's not an issue :-)
kind regards

self-comment b4 i forget
the approach used above works but will instantiate the plugin manager and the zend-view base-path helper. Maybe a better solution is to just define the base-path plugin factory to a custom factory extendend from zend-view (which is simply the InvokableFactory) and find a way to inject the detected base url/path in there. Since a factory has no knowledge of the request and therefore of its attributes, maybe we can alter the application configuration via the container so that the detected value (if not empty) can be reflected into a generic 'base_path' or 'base_url' key.

@pine3ree Do you mean to include the code fragment you gave into each action? Or do you insert it somewhere as middleware?

@gbonnema
Hello. I meant it as a early middleware as the __invoke signature is suggesting.
kind regards