Laravel Workbench Walkthrough is a Laravel 4 workbench package generation walkthrough, used to describe package generation process in ZgPHP Mini Conference 2013 that happened on September 9th 2013.
This README.md file contains detailed instructions on how to create the same Workbench package yourself.
- Open:
/app/config/workbench.php
and set your name and email. This info is used later to populate the composer.json file.
-
Use Command Line Interface (CLI) to navigate to Laravel 4 root folder, and then run:
php artisan workbench orangehill/walkthrough --resources
Note that orangehill
represents a vendor (company name, personal name etc.), and walkthrough
represents a package name.
- Use your CLI to navigate to
/workbench/orangehill/walkthrough
and verify that the package structure has been created.
-
Open
/app/config/app.php
to add a Service Provider to the end of the providers array:'providers' => array( // -- 'Orangehill\Walkthrough\WalkthroughServiceProvider', ),
-
To create a main package class generate the file named
Walkthrough.php
inside a path/workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/
with the following code inside: -
Register the new class with the Laravel’s IoC Container by editing Package Service Provider file
/workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/WalkthroughServiceProvider.php
and make sure that the register method looks like this:public function register() { $this->app['walkthrough'] = $this->app->share(function($app) { return new Walkthrough; }); }
Note: If your service provider cannot be found, run the php artisan dump-autoload command from your application's root directory.
Although generating a facade is not necessary, Facade allows you to do something like this:
echo Walkthrough::hello();
-
Create a folder named
Facades
under following path/workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/
-
Inside the
Facades
folder create a file namedWalkthrough.php
with the following content: -
Add the following to the register method of your Service Provider file:
$this->app->booting(function() { $loader = \Illuminate\Foundation\AliasLoader::getInstance(); $loader->alias('Walkthrough', 'Orangehill\Walkthrough\Facades\Walkthrough'); });
This allows the facade to work without the adding it to the Alias array in app/config/app.php
-
Edit your
/app/routes.php
file and add a route to test if a package works:Route::get('/hello', function(){ echo Walkthrough::hello(); });
If all went well you should see the What's up Zagreb!
output in your browser after visiting the test URL.
Following steps will allow yout to run your method from CLI.
-
First, let's modify the
/workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/Walkthrough.php
file to accept an optional parameter and echo out a message that we can observe in our CLI:public static function hello($verb = 'up'){ if (PHP_SAPI == 'cli') echo "What's $verb Zagreb!\n"; return "What's up Zagreb!"; }
-
Create a file
hello($this->argument('verb')); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('verb', InputArgument::REQUIRED, 'verb'), ); } }WalkthroughCommand.php
inside/workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/
folder with following content (code is pretty much self-explanatory): -
Modify Service Provider file register method to include the following code:
$this->app['command.walkthrough'] = $this->app->share(function($app) { return new WalkthroughCommand; }); $this->commands('command.walkthrough');
-
Run
php artisan walkthrough cooking
from CLI in your project root folder and you should see an outputWhat's cooking Zagreb?
.
Make sure to stay tuned to our Orange Hill Blog for more usefull information like this.
Follow us on Facebook: https://www.facebook.com/orangehilldev
Follow us on Twitter: https://twitter.com/orangehilldev
Follow us on Linked In: http://www.linkedin.com/company/orange-hill