Creating a custom artisan command in laravel

Laravel’s artisan command-line tool makes many tasks easy to accomplish. If we want to
make our own tasks and use artisan to run them, the process is quite simple. In this recipe,
we’ll see how to make an artisan task that automatically creates an HTML5 skeleton in our
views directory.

Getting ready

For this recipe, we’ll need a standard Laravel installation.

How to do it…

To complete this recipe, follow these steps:
1. In the command line, run the artisan command to create our needed files:
php artisan command:make SkeletonCommand
2. In the app/commands directory, open the SkeletonCommand.php file and update
the code as follows:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Filesystem\Filesystem as File;
class SkeletonCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = ‘skeleton:make’;
/**
* The console command description.
*
* @var string
*/
protected $description = ‘Creates an HTML5 skeleton
view.’;
/**
* File system instance
*
* @var File
*/
protected $file;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->file = new File();
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$view = $this->argument(‘view’);
$file_name = ‘app/views/’ . $view;
$ext = ($this->option(‘blade’)) ? ‘.blade.php’ :
‘.php’;
$template = ‘<!DOCTYPE html>
<html>
<head>

<meta charset=utf-8 />
<title></title>
<link rel=”stylesheet” type=”text/css”
media=”screen” href=”css/style.css” />
<script type=”text/javascript”
src=”http://ajax.googleapis.com/
ajax/libs/jquery/2.0.3/jquery.min.js”>
</script>
<!–[if IE]>
<script src=”http://html5shiv.
googlecode.com/svn/trunk
/html5.js”></script>
<![endif]–>
</head>
<body>
</body>
</html>’;
if (!$this->file->exists($file_name)) {
$this->info(‘HTML5 skeleton created!’);
return $this->file->put($file_name . $ext,
$template) !== false;
} else {
$this->info(‘HTML5 skeleton created!’);
return $this->file->put($file_name . ‘-‘ .
time() . $ext, $template) !== false;
}
$this->error(‘There was a problem creating your
HTML5 skeleton’);
return false;

}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array(‘view’, InputArgument::REQUIRED,
‘The name of the view.’),
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array(‘blade’, null, InputOption::VALUE_OPTIONAL,
‘Use Blade templating?’, false),
);
}
}
3. In the app/start directory, open the artisan.php file and add the following line:
Artisan::add(new SkeletonCommand);
4. In the command line, test out the new command:
php artisan skeleton:make MyNewView –blade=true

How it works…

Our first step is to use the command:make function of artisan and pass in the name of the
command we want to use. After this runs, we’ll find a new file in our app/commands directory
with the same name as the name we chose.
In our SkeletonCommand file, we start by adding in a name. This will be the command to
which artisan will respond. Next, we set a description, which will display when we list out all
the artisan commands.
For this command, we’ll be accessing the filesystem, so we need to make sure to add
Laravel’s Filesystem class and that we instantiate it in our constructor. Then, we come to
the fire() method. This is where all the code we want to run should go. For our purpose,
we use a single argument to determine what our view file name will be, and if the –blade
parameter is set to true, we’ll make it a blade file. Then, we create a string that holds our
HTML5 skeleton, though we could also make this a separate file and pull in the text.
We then create the new file using the template as our HTML and display a success message
in the console.

Leave a Reply

Your email address will not be published. Required fields are marked *