How to create artisan command in Laravel 4.2


There will be times when you need to create your own artisan command, and it is totally possible with laravel, you can create your own artisan command, in this tutorial i will show you how to create artisan command on laravel 4.2.

On laravel 4.2, the artisan command is stored under app/commands folder, the artisan command is just a regular php file. You don't need to create the php file manually, there is a dedicated artisan command for generating the php file under app/commands folder.

For this tutorial, let's create a simple artisan command for displaying text, the class name for this artisan command is called 'hello', and the command will be 'php artisan say:hello', with parameter input --name.

Creating the php file
To generate php file under app/commands, you can use 'php artisan command:make', so here's what we do:
php artisan command:make hello

If you check app/commands folder, you can see there is a new file called hello.php

Editing the php file
Next we need to edit the php file, in this case hello.php (app/commands/hello.php), make sure exactly like this (copy-paste the code if you have to):

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class hello extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'say:hello';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'This command is just learning purpose.';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
		$name = $this->option('name');
		$greeting = 'Hello';
		$question = ' how are you today?';
		if (!empty($name)) {
			$name = ' ' . $name . ',';
		}
		$display = $greeting . $name . $question;
		$this->info($display);
	}

	/**
	 * Get the console command arguments.
	 *
	 * @return array
	 */
	protected function getArguments()
	{
		return array(
			array('example', InputArgument::OPTIONAL, 'An example argument.'),
		);
	}

	/**
	 * Get the console command options.
	 *
	 * @return array
	 */
	protected function getOptions()
	{
		return array(
			array('name', null, InputOption::VALUE_OPTIONAL, 'Enter your name here.', null),
		);
	}

}



Registering the artisan command
We are not done yet, we still need to register the artisan command that we just created, open app/start/artisan.php file and add the following code:

<?php

/*
|--------------------------------------------------------------------------
| Register The Artisan Commands
|--------------------------------------------------------------------------
|
| Each available Artisan command must be registered with the console so
| that it is available to be called. We'll register every command so
| the console gets access to each of the command object instances.
|
*/

// Artisan command for saying hello
Artisan::add(new hello);

Okay, now we can test the artisan command, see if it's working or not, the command would be:
php artisan say:hello
php artisan say:hello --name=jessica



EmoticonEmoticon