Friday, October 17, 2014

Zend Framework 2 : Console based application

Hi,

Console based application has significant role for the users rather to use in web based apps.
In ZF2, there is a way to set console based application.

I like to address you guys on setting of console application inside Zend-Framework 2 in step-wise manner:

Step 1: Open your Module.php file for console based settings and place the below code:
1
2
3
use Zend\ModuleManager\Feature\ConsoleBannerProviderInterface;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\Console\Adapter\AdapterInterface as Console;


and implements the below interface on your Module class i.e.
1
class Module implements ConsoleBannerProviderInterface, ConsoleUsageProviderInterface


Place the below function for the console banner and console usage inside Module.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
     * To show the console banner
     * @see \Zend\ModuleManager\Feature\ConsoleBannerProviderInterface::getConsoleBanner()
     */
    public function getConsoleBanner(Console $console){
        return
        "==------------------------------------------------------==\n" .
        "        Welcome to my ZF2 Console-enabled app             \n" .
        "==------------------------------------------------------==\n" .
        "Version 0.0.1\n"
            ;
    }
    
    /**
     * This method is defined in ConsoleUsageProviderInterface
     */
    public function getConsoleUsage(Console $console){
        return array(
            'show logs'             => 'Show application statistics',
            'run cron'               => 'Run automated jobs',
            '(enable|disable) debug' => 'Enable or disable debug mode for the application.',
            'show users'             => 'Show active users',
            'show all users'               => 'show all users',
            'show enable|disable users' => 'show enable|disable users'
        );
    }


Step 2: Now if you run the application from console, then your have to run the below command where your application reside:
1
# php public/index.php



Then will get the below information:

















If you get the same as above screen then this will give you some energy that you are on right track :)

Step 3: Now you have to set the routing for the console i.e. passing route name after index.php file like:
1
# php public/index.php show logs

To get this, we need to set that in module.config.php file of that Application module:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
                'show-logs' => array(
                    'options' => array(
                        'route'    => 'show logs [--verbose|-v]',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'show-logs'
                        )
                    )
                )
            ),
        ),
    ),


Step 4: Now your are on final destination to run the trigger of gun :)
Place the below code in your application controller file i.e. IndexController.php file:
1
2
3
4
5
6
7
8
9
    public function showLogsAction() {
        $request = $this->getRequest();
        // Make sure that we are running in a console and the user has not tricked our
        // application into running this action from a public web server.
        if (!$request instanceof ConsoleRequest) {
            throw new RuntimeException('You can only use this action from a console!');
        }
        return "App Calling from the Console\n";
    }


Now run the below command:







Simple... :)

If you want to learn more on this please follow the below URL:

http://framework.zend.com/manual/2.0/en/modules/zend.console.modules.html


I hope you enjoy to setup console based app... Bye..

No comments:

Post a Comment