4

我如何在 ZF 2 的控制器中调用 basePath 助手。我必须重定向到我需要基本路径的特定 url。return $this->redirect()->toUrl($basePath.'/application/rent/search');

4

3 回答 3

7

这是使所有视图助手在控制器中可用的简单方法。因此,您应该能够使用以下内容:

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);
}
于 2012-11-30T18:17:35.230 回答
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());
于 2012-11-30T17:53:53.090 回答
1

尝试

class XxxController extends AbstractActionController
{

...

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

或者

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

http://project.com/profile

返回“”

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

返回 /~limonazzo/public/

于 2016-08-17T23:04:42.293 回答