41
LARAVEL 4 TIHOMIR OPACIC PRESENTED BY PACKAGE DEVELOPMENT

Laravel 4 package development

Embed Size (px)

DESCRIPTION

With Composer as an integral part of Laravel 4 PHP framework, PHP programmers finaly have a way to break the complex projects into smaller independent units (Laravel Packages) that can later easily be used in any other project. This brings code reusibilty to a completely new level. Lecture describes the proccess of creating a simple Laravel package with Facade and Artisan CLI support. Detailed walkthorugh is available as a github project as well: https://github.com/orangehill/Laravel-Workbench-Walkthrough

Citation preview

Page 1: Laravel 4 package development

LARAVEL 4

TIHOMIROPACIC

PRESENTEDBY

PACKAGE DEVELOPMENT

Page 2: Laravel 4 package development

THE PHP FRAMEWORK FOR WEB ARTISANS.

ABOUT LARAVEL 4

RESTful Routing

Beautiful Templating

Proven Foundation

Great Community

Command Your Data

Ready For Tomorrow

Composer Powered

Red, Green, Refactor

Page 4: Laravel 4 package development

Got a good PHP class? Is it only on GitHub, or maybe it's just sitting around on your blog?

Stop that. Stop that right now.

Make it into a Composer package and host it on Packagist.

HISTORY

”http://philsturgeon.co.uk/blog/2012/03/packages-the-way-forward-for-php (*Phil Sturgeon: Pyro CMS)

Page 5: Laravel 4 package development

Composer is a tool for dependency management in PHP.

It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

COMPOSER

http://getcomposer.org/

Page 6: Laravel 4 package development

Packagist is the main Composer repository. It aggregates all sorts of PHP packages that are installable with Composer.

PACKAGIST

https://packagist.org/

Page 7: Laravel 4 package development

1 2 3 4

CODE REUSABILITY

MODULAR APPS OPENSOURCELEVEREGE

FRAMEWORK DEVMADE EASIER

BENEFITS

Main benefits while using Composer and Packagist to get or publish PHP Packages.

Page 8: Laravel 4 package development

2012-04 2012-09 2013-02 2013-08

950000020000001000000100000

PACKAGES INSTALLED

Source: https://packagist.org/statistics

Page 9: Laravel 4 package development

1 2 3 4

INTRODUCED IN V.4WERE BUNDLES IN V.3

FRAMEWORKGROWTH BOOST

ENTIRELYMADE OF PACKAGES

WORKBENCHPACKAGE DEV TOOL

LARAVEL 4

Packages in Laravel 4 PHP Framework

Page 10: Laravel 4 package development

INSTALL COMPOSER

Installation instructions: http://getcomposer.org/doc/01-basic-usage.md#installation

Page 11: Laravel 4 package development

COMPOSER.JSON

{ "name": "orangehill/zgphpcon2013", "description": "Laravel 4 workbench package generation walkthrough.", "authors": [ { "name": "Tihomir Opacic", "email": "[email protected]" } ], "require": { "php": ">=5.3.0", "illuminate/support": "4.0.x" }, "autoload": { "psr-0": { "Orangehill\\Zgphpcon2013": "src/" } }, "minimum-stability": "dev"}

VENDOR / NAMEDESCRIPTION

AUTHORS

DEPENDENCY

PSR-0 AUTOLOADING INFO

more: http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ 

Page 12: Laravel 4 package development

Also Available in Serbian:

Razvoj web aplikacija uz pomoć Laravel radnog okvira verzije 4 za početnike

Slaviša Petrović @slawisha75

CODEBRIGHT

Dayle Rees @daylerees

https://leanpub.com/codebright-sr https://leanpub.com/codebright

Page 13: Laravel 4 package development

COMPOSER.JSON

$ php composer.phar install

*phar: PHP Archive - entire PHP applications in a single file

$ composer install

*If you did a global install and do not have the phar in that directory run this instead

Page 14: Laravel 4 package development

LARAVEL WORKBENCH

14 STEP WALKTHROUGH

https://github.com/orangehill/Laravel-Workbench-Walkthrough

Page 15: Laravel 4 package development

SIMPLICITY14 STEP WALKTHROUGH

https://github.com/orangehill/Laravel-Workbench-Walkthrough

IT’S SOEASY!

Page 16: Laravel 4 package development

Edit /app/config/workbench.php and set your name and email. This info is used later to populate the composer.json file.

PACKAGE GENERATION

Laravel Workbench Walkthrough

1 Edit workbench config file

Page 17: Laravel 4 package development

Use Command Line Interface (CLI) to navigate to Laravel 4 root folder, and then run:

Note that orangehill represents a vendor (company name, personal name etc.), and walkthrough represents a package name.

PACKAGE GENERATION

Laravel Workbench Walkthrough

2 Run CLI (Command Line Interface) command

php artisan workbench orangehill/walkthrough --resources

Page 18: Laravel 4 package development

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

PACKAGE GENERATION

Laravel Workbench Walkthrough

3 Navigate to package directory

Page 19: Laravel 4 package development

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

PACKAGE SETUP

Laravel Workbench Walkthrough

4 Add a Service Provider

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

Page 20: Laravel 4 package development

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:

PACKAGE SETUP

Laravel Workbench Walkthrough

5 Create Main Package Class

<?php namespace Orangehill\Walkthrough;

class Walkthrough {

public static function hello(){ return "What's up Zagreb!"; }

}

Page 21: Laravel 4 package development

Edit the Package Service Provider file /workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/WalkthroughServiceProvider.php and make sure that the register method looks like this:

PACKAGE SETUP

Laravel Workbench Walkthrough

6 Register the new class with the Laravel’s IoC Container

public function register(){ $this->app['walkthrough'] = $this->app->share(function($app) { return new Walkthrough; });}

Page 22: Laravel 4 package development

Note: If your service provider cannot be found, run the php artisan dump-autoload command from your application's root directory.

PACKAGE SETUP

Laravel Workbench Walkthrough

6 NOTE!

Page 23: Laravel 4 package development

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

FACADE GENERATION

Laravel Workbench Walkthrough

echo Walkthrough::hello();

Page 24: Laravel 4 package development

Create a folder named Facades under following path /workbench/orangehill/walkthrough/src/Orangehill/Walkthrough/

FACADE GENERATION

Laravel Workbench Walkthrough

7 Create a Facades folder

Page 25: Laravel 4 package development

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

PACKAGE SETUP

Laravel Workbench Walkthrough

8 Create a Facade class

<?php namespace Orangehill\Walkthrough\Facades;

use Illuminate\Support\Facades\Facade;

class Walkthrough extends Facade {

protected static function getFacadeAccessor() { return 'walkthrough'; }

}

Page 26: Laravel 4 package development

Add the following to the register method of your Service Provider file:

This allows the facade to work without the adding it to the Alias array in app/config/app.php

PACKAGE SETUP

Laravel Workbench Walkthrough

9 Edit a register method of your Service Provider file

$this->app->booting(function(){ $loader = \Illuminate\Foundation\AliasLoader::getInstance(); $loader->alias('Walkthrough', 'Orangehill\Walkthrough\Facades\Walkthrough');});

Page 27: Laravel 4 package development

Edit your /app/routes.php file and add a route to test if a package works:

BROWSER TEST

Laravel Workbench Walkthrough

10 Edit a routes file

Route::get('/hello', function(){ echo Walkthrough::hello();});

Page 28: Laravel 4 package development

If all went well you should see the output in your browser after visiting the test URL:

BROWSER TEST

Laravel Workbench Walkthrough

What's up Zagreb!

Page 29: Laravel 4 package development

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:

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

11 Modify a main package class

public static function hello($verb = 'up'){ if (PHP_SAPI == 'cli') echo "What's $verb Zagreb?\n"; return "What's up Zagreb?";}

Page 30: Laravel 4 package development

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

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

12 Create a Command class

<?php namespace Orangehill\Walkthrough;

use Illuminate\Console\Command;use Symfony\Component\Console\Input\InputOption;use Symfony\Component\Console\Input\InputArgument;

class WalkthroughCommand extends Command {

Page 31: Laravel 4 package development

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

12 Create a Command class

/** * The console command name. * * @var string */ protected $name = 'walkthrough';

/** * The console command description. * * @var string */ protected $description = 'Run the Walkthrough Package hello() method from command line.';

Page 32: Laravel 4 package development

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

12 Create a Command class

/** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); }

Page 33: Laravel 4 package development

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

12 Create a Command class

/** * Execute the console command. * * @return void */ public function fire() { app('walkthrough')->hello($this->argument('verb')); }

Page 34: Laravel 4 package development

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

12 Create a Command class

/** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('verb', InputArgument::REQUIRED, 'verb'), ); }

}

Page 35: Laravel 4 package development

Modify Service Provider file register method to include the following code:

ARTISAN CLI SUPPORT

Laravel Workbench Walkthrough

13 Modify Service Provider file register method

$this->app['command.walkthrough'] = $this->app->share(function($app){ return new WalkthroughCommand;});$this->commands('command.walkthrough');

Page 36: Laravel 4 package development

Run the test from CLI in your project root folder:

CLI TEST

Laravel Workbench Walkthrough

14 Run a test from CLI

php artisan walkthrough cooking

Page 37: Laravel 4 package development

If all went well:

CLI TEST

Laravel Workbench Walkthrough

What's cooking Zagreb!

Page 38: Laravel 4 package development

•Time/Date Management Classes•Various API Wrapper Classes•PDF Creation Libraries•Image Manipulation Libraries

PACKAGES

FRAMEWORK AGNOSTIC PACKAGES

Page 39: Laravel 4 package development

PACKAGES

Satis - Package Repository Generator

https://github.com/composer/satis

PRIVATE REPOSITORIES

Page 40: Laravel 4 package development

Orange HillDjordja Stanojevica 9b, 11000 Belgrade, Serbia

MAP

CONTACT US

WWW.ORANGEHILLDEV.COM

[email protected]

+381.64.167.7367

Page 41: Laravel 4 package development

FACEBOOKWWW.FACEBOOK.COM/ORANGEHILLDEV

TWITTERWWW.TWITTER.COM/ORANGEHILLDEV

LINKEDINWWW.LINKEDIN.COM/COMPANY/ORANGE-HILL

BLOGWWW.ORANGEHILLDEV.COM

FOLLOW US

Orange HillDjordja Stanojevica 9b, 11000 Belgrade, Serbia