2012-11-30 131 views
3

如何在ZF 2中的控制器中調用basePath helper。我必須重定向到一個特定的url,我需要基路徑。 return $ this-> redirect() - > toUrl($ basePath。'/ application/rent/search');zend框架2在控制器中的基本路徑訪問

+0

在我的測試,這是沒有必要的。只需使用'$這個 - >重定向() - >的toURL(「/應用/租/搜索」)' –

回答

3

完整的基本URL(HTTP:// ...),可以從控制器內確定如下:

$event = $this->getEvent(); 
$request = $event->getRequest(); 
$router = $event->getRouter(); 
$uri = $router->getRequestUri(); 
$baseUrl = sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $request->getBaseUrl()); 
+0

存在的baseUrl和基本路徑 – khunshan

6

下面是一個easy method,使所有視圖助手可以從控制器內。所以,你應該能夠使用以下命令:

public function someAction() 
{ 
    $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); 
    $url = $renderer->basePath('/application/rent/search'); 
    $redirect = $this->plugin('redirect'); 
    return $redirect->toUrl($url); 
} 
+3

注意區別在於重定向器插件也有一個toRoute()方法,這使得利用基本路徑已過時。 – aimfeld

+0

我喜歡這個解決方案更多... –

+0

我看到的最佳解決方案。如果在控制器配置中使用工廠,它非常乾淨。 – albert

0

嘗試

class XxxController extends AbstractActionController 
{ 

... 

public function basePath() 
{ 
    $basePath = $this->serviceLocator 
     ->get('viewhelpermanager') 
     ->get('basePath'); 
    return $basePath(); 
} 

OR

public function algoAction() 
{ 
    echo $this->getRequest()->getBaseUrl(); 
} 

http://project.com/profile

返回 「」

http://localhost/~limonazzo/public/profile

回報/〜limonazzo /公/