0

I tried to find a way to get base url of mixed (http + CLI) application. CLI is used to do some regular notifications, where app url is used.

I tried this, but it returns me only "http://"

print_r($this->getEvent()->getApplication()->getServiceManager()->get('ViewHelperManager')->get('ServerUrl')->__invoke());
Wilt
  • 41,477
  • 12
  • 152
  • 203
bksi
  • 1,606
  • 1
  • 23
  • 45

1 Answers1

1

This does not work for console requests. The serverUrl is extracted from the http request object (Zend\Http\Request), since a console request is wrapped in a console request object (Zend\Console\Request) this information won't be available for such requests.

You can define (hardcode) an url in a global config or a constant and use this instead.

For example add a host.config.local file to your application/config/autoload folder:

<?php

return array(
    'hostname' => 'http://www.example.com'
);

Now you can get your hostname from your service manager:

$config = $serviceManager->get('config');
$hostName = $config['hostname'];
Wilt
  • 41,477
  • 12
  • 152
  • 203
  • No, i run this code in the console controller. It returns me "http//" – bksi Jun 28 '16 at 11:53
  • @bksi A console request is not an http request. So when you do a console request there is no such thing as a `serverUrl`. – Wilt Jun 28 '16 at 12:35
  • Indeed. So back to my question: How i can get app url from console controller? – bksi Jun 28 '16 at 12:37
  • @bksi There is no such thing as a server url when you do a console request. What url do you expect to get? Can you give an example? – Wilt Jun 28 '16 at 13:13
  • As i said in my question i have an application that can be accessed by url. I use console to do some notifications by crontab. I need to get the application url, to use it in the generated notification emails. – bksi Jun 28 '16 at 15:06
  • As i said in my answer you cannot get the url since a console request doesn't have such thing as a server url in the request object. You can define (hardcode) an url in a global config or a constant and use this instead. – Wilt Jun 28 '16 at 15:09