Zend framework 2 access to the base path in the controller

How can I call the basePath helper in the controller in ZF 2. I need to redirect to a specific URL where I need the base path. return $ this-> redirect () β†’ toUrl ($ basePath. '/ application / rent / search');

+3
source share
3 answers

Here's a simple method to make all help inquiries available from within controllers. Therefore, you should be able to use the following:

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); } 
+6
source

The full base url (http: // ...) can be determined from the controller as follows:

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

try

 class XxxController extends AbstractActionController { ... public function basePath() { $basePath = $this->serviceLocator ->get('viewhelpermanager') ->get('basePath'); return $basePath(); } 

in

OR

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

http://project.com/profile

returns ""

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

returns / ~ limonazzo / public /

0
source

Source: https://habr.com/ru/post/969462/


All Articles