2017-06-16 70 views
1

我是ZF3的noobie,我们在基于codeigniter的主应用中放置了基于zend的管理面板。像下面的方式我如何在zend框架3中获取基本目录URL?

my_app/zend_admin/ 
| 
| 
--- config 
--- module 
--- public 

我可以使用www.my_app.com/zend_admin/my_zend_controller/my_zend_action访问zend模块。我想访问www.my_app.com/my_ci_controller/my_ci_action

是否有任何zend提供的方法,因为ci提供了base_url(),所以我可以取我的ci控制器?

回答

1

,让你可以使用的serverUrl视图助手(像笨BASE_URL())

$this->serverUrl();  // return http://web.com OR 
$this->serverUrl('/uri'); // return http://web.com/uri 

我不知道你的设置,但尝试的基础网址...

+0

部分它适用于我,在服务器端它的罚款,但在本地主机,其服务器的URL到本地主机不localhost/my_app ..我怎么能理解这一点。所以接受你的答案作为我的解决方案。 –

0

有有几种方法可以使用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知道这是为什么!

+0

感谢您的详细解释。 –