How to detect if a Zend Framework 2 application runs in console or HTTP context?

php, zend-framework2

Solution

Thats pretty easy. Just check if `Request` is an instance of `Zend\Http\Request` for Http and `Zend\Console\Request` for Console request. For example:

namespace Application;

use Zend\Mvc\MvcEvent;
use Zend\Http\Request as HttpRequest ;
use Zend\Console\Request as ConsoleRequest ;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        if ($e->getRequest() instanceof HttpRequest) {
            // do something important for Http
        } elseif($e->getRequest() instanceof ConsoleRequest ) {
            // do something important for Console
        }
    }  
}

Problem

I'm writing a module to perform some tasks based on if the application is running in the console or HTTP context. Is there a way to detect this when the module is loaded? For example, I try doing this with Module.php class. ``` namespace MyModule; use ... class Module { public function init(ModuleManager $mm) { if (Console context) { // do something } else { // do something with HTTP } } } ``` Thanks!

Original source