Zend framework2 在Zf2中的Module.php文件中查找BaseUrl
我需要在module.php文件或服务文件中找到baseUrl。早些时候,我正在使用Zend framework2 在Zf2中的Module.php文件中查找BaseUrl,zend-framework2,base-url,Zend Framework2,Base Url,我需要在module.php文件或服务文件中找到baseUrl。早些时候,我正在使用 $event = $this->getEvent(); $request = $event->getRequest(); $router = $event->getRouter(); $uri = $router->getRequestUri();
$event = $this->getEvent();
$request = $event->getRequest();
$router = $event->getRouter();
$uri = $router->getRequestUri();
$baseUrl = sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $request- >getBaseUrl());
但是,我知道这只会在控制器的动作方法中运行。如何在其他地方获取baseUrl?在
Module.php中尝试此功能-
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e) {
$e->getApplication()
->getEventManager()
->getSharedManager()
->attach('*', 'route', array($this, 'some_function_name'));
}
public function some_function_name(MvcEvent $e) {
$request = $e->getRequest();
$router = $e->getRouter();
$uri = $router->getRequestUri();
$baseUrl = sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $request->getBaseUrl());
//[...some other code ... ]
}
//[...some other code ... ]
}