SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
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
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
1.
LARAVEL 4
TIHOMIR
OPACIC
PRESENTED
BY
PACKAGE DEVELOPMENT
2.
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
4.
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)
5.
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/
6.
Packagist is the main
Composer repository. It
aggregates all sorts of
PHP packages that are
installable with Composer.
PACKAGIST
https://packagist.org/
7.
1 2 3 4
CODE REUSABILITY MODULAR APPS OPENSOURCE
LEVEREGE
FRAMEWORK DEV
MADE EASIER
BENEFITS
Main benefits while using Composer and Packagist to get or
publish PHP Packages.
9.
1 2 3 4
INTRODUCED IN V.4
WERE BUNDLES IN V.3
FRAMEWORK
GROWTH BOOST
ENTIRELY
MADE OF PACKAGES
WORKBENCH
PACKAGE DEV TOOL
LARAVEL 4
Packages in Laravel 4 PHP Framework
12.
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
13.
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
15.
SIMPLICITY
14 STEP WALKTHROUGH
https://github.com/orangehill/Laravel-Workbench-Walkthrough
IT’S SO
EASY!
16.
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
17.
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
18.
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
19.
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(
// --
'OrangehillWalkthroughWalkthroughServiceProvider',
),
20.
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 OrangehillWalkthrough;
class Walkthrough {
public static function hello(){
return "What's up Zagreb!";
}
}
21.
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;
});
}
22.
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!
23.
Although generating a facade is not necessary, Facade allows you to
do something like this:
FACADE GENERATION
Laravel Workbench Walkthrough
echo Walkthrough::hello();
24.
Create a folder named Facades under following path /workbench/
orangehill/walkthrough/src/Orangehill/Walkthrough/
FACADE GENERATION
Laravel Workbench Walkthrough
7 Create a Facades folder
25.
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 OrangehillWalkthroughFacades;
use IlluminateSupportFacadesFacade;
class Walkthrough extends Facade {
protected static function getFacadeAccessor() { return
'walkthrough'; }
}
26.
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 = IlluminateFoundation
AliasLoader::getInstance();
$loader->alias('Walkthrough', 'OrangehillWalkthrough
FacadesWalkthrough');
});
27.
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();
});
28.
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!
29.
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?";
}
30.
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 OrangehillWalkthrough;
use IlluminateConsoleCommand;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleInputInputArgument;
class WalkthroughCommand extends Command {
31.
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.';
32.
ARTISAN CLI SUPPORT
Laravel Workbench Walkthrough
12 Create a Command class
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
33.
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'));
}
34.
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'),
);
}
}
35.
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');
36.
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
37.
If all went well:
CLI TEST
Laravel Workbench Walkthrough
What's cooking Zagreb!
39.
PACKAGES
Satis - Package Repository Generator
https://github.com/composer/satis
PRIVATE REPOSITORIES
40.
Orange Hill
Djordja Stanojevica 9b, 11000 Belgrade, Serbia
MAP
CONTACT US
WWW.ORANGEHILLDEV.COM
OFFICE@ORANGEHILLDEV.COM
+381.64.167.7367
41.
FACEBOOK
WWW.FACEBOOK.COM/ORANGEHILLDEV
TWITTER
WWW.TWITTER.COM/ORANGEHILLDEV
LINKEDIN
WWW.LINKEDIN.COM/COMPANY/ORANGE-HILL
BLOG
WWW.ORANGEHILLDEV.COM
FOLLOW US
Orange Hill
Djordja Stanojevica 9b, 11000 Belgrade, Serbia