/Laravel-Workbench-Walkthrough

Laravel 4 workbench package generation walkthrough

Primary LanguagePHP

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.

Laravel 4 Workbench Package Generation

  1. Open: /app/config/workbench.php

and set your name and email. This info is used later to populate the composer.json file.

  1. 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.

  1. Use your CLI to navigate to /workbench/orangehill/walkthrough and verify that the package structure has been created.

Package Setup

  1. Open /app/config/app.php to add a Service Provider to the end of the providers array:

    'providers' => array( // -- 'Orangehill\Walkthrough\WalkthroughServiceProvider', ),

  2. 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:

  3. 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.

Package Facade Generation

Although generating a facade is not necessary, Facade allows you to do something like this:

echo Walkthrough::hello();
  1. Create a folder named Facades under following path /workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/

  2. Inside the Facades folder create a file named Walkthrough.php with the following content:

  3. 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

Browser Test

  1. 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.

Adding an Artisan CLI Support

Following steps will allow yout to run your method from CLI.

  1. 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!"; }

  2. Create a file WalkthroughCommand.php inside /workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/ folder with following content (code is pretty much self-explanatory):

    hello($this->argument('verb')); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('verb', InputArgument::REQUIRED, 'verb'), ); } }
  3. 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');

  4. Run php artisan walkthrough cooking from CLI in your project root folder and you should see an output What's cooking Zagreb?.

About Us

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