In this article you’ll learn how to use Laravel’s Artisan command line tool, and how to create a customized command. Note that you need to be familiar with the Laravel framework to get the most of this article.
What are we building
In this tutorial we’re going to build a command to minify our css assets, which will be used like this:
cssmin 'output_path' 'file1'...'fileN' --comments --concat
output_path
: (required) path to save the minified files, (style.css
->style.min.css
).file1 ... fileN
: (required) list of files to minify.--comments
: (optional) add this option to keep comments.--concat
: (optional) concatenate the minified files into one file calledall.min.css
.
What is a Laravel Command
Artisan
is the name of the command line utility in Laravel. It comes with a set of predefined commands, which you can list with php artisan list
. If you want to show the help for a specific command, you can use php artisan help command
.
Creating the Css Minifier Command
To create an artisan command, you can use the command:make
command. This command accepts one argument:
name
: the class name for the command.
and three options:
--command
: the name that should be typed to run the command.--path
: by default the commands are stored within theapp/commands
folder, however, you can change that with this option.--namespace
: you can use this option to namespace your set of commands, e.g. in the commandcommand:make
, themake
command is under thecommand
namespace.
Now, to create our command we will use php artisan command:make CssMinCommand --command=cssmin
which will create a CssMinCommand.php
file within our app/commands
directory.
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CssminCommand extends Command{
protected $name = 'cssmin';
protected $description = 'Command description.';
public function __construct(){
parent::__construct();
}
public function fire(){
//
}
protected function getArguments(){
return array(
array('example',
Truncated by Planet PHP, read more at the original (another 2092 bytes)
more
{ 0 comments... » How to Create a Laravel CSS-Minify Command read them below or add one }
Post a Comment