03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Deployment 77<br />

.<br />

This file should be saved as app/commands/EnvironmentCommand.php.<br />

The command class begins with us setting a useful name and description for the following commands<br />

we will create. The fire() method includes three calls to the line() method; which prints text to the<br />

command line. The getArguments() and getOptions() methods return empty arrays because we do<br />

not expect to handle any arguments or options.<br />

.<br />

You may have noticed the XML notation within the calls to the line() method. Laravel 4’s console<br />

library extends Symphony 2’s console library; and Symphony 2’s console library allows these<br />

definitions in order to alter the meaning and appearance of text rendered to the console.<br />

While the appearance will be changed, by using this notation, it’s not the best thing to be doing<br />

(semantically speaking). The bits in the comment elements aren’t comments any more than the bit<br />

in the info elements are.<br />

We’re simply using it for a refreshing variation in the console text colours. If this sort of jacky<br />

behaviour offends your senses, feel free to omit the XML notation altogether!<br />

Before we can run any commands; we need to register them with Artisan:<br />

1 Artisan::add(new EnvironmentCommand);<br />

.<br />

This was extracted from app/start/artisan.php.<br />

There’s nothing much more to say than; this code registers the command with Artisan, so that it<br />

can be invoked. We should be able to call the command now, and it should show us something like<br />

the following:<br />

1 php artisan environment<br />

2 environment:get gets host and environment.<br />

3 environment:set adds host to environment.<br />

4 environment:remove removes host from environment.<br />

Congratulations! We’ve successfully created and registered our first Artisan command. Let’s go<br />

ahead and make a few more.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!