有有几种方法可以使用ZF微型工具完成这项工作。
ZF中有一些类似的视图助手像CodeIgniter那样。您可以将它们用于视图脚本和布局模板中。
让我们开始使用您的模块的module.config.php
。您可以按如下
'view_manager' => [
'base_path' => 'http://www.yoursite.com/',
]
下view_manager
键设置base_path
键现在,如果您使用以下视图助手
echo $this->basePath();
// Outputs http://www.yoursite.com/
如果您使用以下一个
echo $this->basePath('css/style.css');
// Outputs http://www.yoursite.com/css/style.css
但是,如果你不使用上述配置
echo $this->basePath('css/style.css');
// Outputs css/style.css
由于@tasmaniski说约$this->serverUrl();
你也可以在视图脚本中使用它。这个好东西不需要像$this->basePath()
任何配置,如果你在ZF的控制器动作需要该。做到这一点的控制器动作最简单的方法是
public function indexAction()
{
$uri = $this->getRequest()->getUri();
$baseUrl = sprintf('%s://%s/', $uri->getScheme(), $uri->getHost());
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/
}
否则,你可以得到它下面的方式,但这个工程一样$this->basePath()
public function indexAction()
{
// This is for zf2
$renderer = $this->getServiceLocator->get('Zend\View\Renderer\RendererInterface');
// This is for zf3
// Assuming $this->serviceManager is an instance of ServiceManager
$renderer = $this->serviceManager->get('Zend\View\Renderer\RendererInterface');
$baseUrl = $renderer->basePath('/uri');
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/uri
}
此外,还有两个功能,可以是在控制器操作的不同条件下使用。如果使用重写规则,那些将返回空字符串。这些是
$this->getRequest()->getBaseUrl();
$this->getRequest()->getBasePath();
这些都不像你所期望的那样工作。必须参考issue知道这是为什么!
部分它适用于我,在服务器端它的罚款,但在本地主机,其服务器的URL到本地主机不localhost/my_app ..我怎么能理解这一点。所以接受你的答案作为我的解决方案。 –