Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sharing Laravel - Bringing Laravel's Best Asset...

Sharing Laravel - Bringing Laravel's Best Assets to Any Project

I gave a talk at Laracon Eu 2014 on how folks excited about Laravel can take the best from Laravel to their other projects. Note that only a small portion of this is *unique to* Laravel, but rather than we can use our desire to work in Laravel as a motivation to bring the good parts of Laravel everywhere in our coding and our codebases.

This is also a quick introduction to using Illuminate components in non-Laravel applications.

Matt Stauffer

August 29, 2014
Tweet

More Decks by Matt Stauffer

Other Decks in Technology

Transcript

  1. Sharing Laravel @stauffermatt Most Laravel tutorials assume a blank slate.

    greenfield, adj: a project that lacks any constraints imposed by prior work.
  2. Sharing Laravel @stauffermatt The problem: Most of our work is

    on
 existing codebases. FREEDOM Old Code
  3. Sharing Laravel @stauffermatt The solution: Learn how to bring the

    best assets of the Laravel world to non-Laravel projects.
  4. Sharing Laravel @stauffermatt The disclaimers: • Going to cover a

    lot, shallowly, so please ask me questions afterward • Much of this talk isn’t unique to Laravel
  5. Sharing Laravel @stauffermatt What will Laravel add to my project?

    Modern coding practices Rapid development Collection of great community (Symfony, etc.) packages Collection of great unique (Illuminate) packages Thriving and positive community Adoption of advanced patterns & practices Potential for future growth—unique foresight
  6. Sharing Laravel @stauffermatt response to legacy code: trash it. Sharing

    Laravel @stauffermatt or, more likely, “KILL IT WITH FIRE”
  7. Sharing Laravel @stauffermatt Nuking your old project is a Bad

    Idea. 1. It’s going to take longer than you think. 2. It’s going to take longer than you think. 3. There’s more domain knowledge in your old project than you can imagine. 4. Development on the production site will halt during development. 5. It’s going to take longer than you think.
  8. Sharing Laravel @stauffermatt UNDERCOVER LARAVEL • Recognizing that an all-out

    rebuild is not wise or, likely, in line with the budget… • Still wanting to bring the Best Of Laravel to your project… • Become a Laravel secret agent.
 Sneak it in where it’s least expected. • Test here. DI there. Illuminate component there. • Prove the value before you’re asking anyone to pay for it. • On every task or project, look for one way to make your codebase a little more Laravel-y.
  9. Sharing Laravel @stauffermatt LEARN THE BASICS OOP best practices SOLID

    Law of Demeter Loose coupling Not just wrapping procedural code in classes (ahem CodeIgniter ahem) STEP 1
  10. Sharing Laravel @stauffermatt LEARN THE BASICS Design patterns Identifying best-practice,

    commonly-used patterns; giving each a name; and suggesting situations in which each will (and won’t) prove useful STEP 1
  11. Sharing Laravel @stauffermatt LEARN THE BASICS Design patterns Examples: •

    Adapter Pattern • Singleton Pattern • Observer Pattern • Many more… STEP 1
  12. Sharing Laravel @stauffermatt Read the books Code Bright
 Dayle Rees

    From Apprentice to Artisan
 Taylor Otwell Implementing Laravel
 Chris Fidao Other Books I haven’t read yet
 http://wiki.laravel.io/books LEARN FROM LARAVEL STEP 2
  13. Sharing Laravel @stauffermatt LEARN FROM LARAVEL Go read the accepted

    PSRs PSR-0: Old Autoloading PSR-1 & PSR-2: Code styles PSR-3: Logging PSR-4: Autoloading STEP 2
  14. Sharing Laravel @stauffermatt IRC like it’s 1999 #laravel You could

    hang out in #laravel and never have written a line of Laravel code in your life. LEARN FROM LARAVEL STEP 2
  15. Sharing Laravel @stauffermatt MODERNIZE YOUR FOUNDATION Code standards PHP CodeSniffer

    (PHPStorm, Phil Sturgeon blog post on Sublime Text) PHP-CS-Fixer (Automatically fixes your code) PSR2-ish (tabs not spaces, control braces on new lines) Scrutinizer config: 
 https://github.com/laravel/framework/blob/4.2/.scrutinizer.yml STEP 3
  16. Sharing Laravel @stauffermatt Stay Classy Procedural code/functions -> methods on

    classes -> retroactive designed OOP Namespaces & autoloading STEP 3 MODERNIZE YOUR FOUNDATION
  17. Sharing Laravel @stauffermatt Autoloading & PSR-4 Autoload your classes PSR-4

    autoloading Drop as many includes/requires as possible STEP 3 MODERNIZE YOUR FOUNDATION
  18. Sharing Laravel @stauffermatt Bug-driven testing For every bug reported… Write

    a (failing) test. Then fix the bug by writing code that makes the test pass. Repeat. STEP 3 MODERNIZE YOUR FOUNDATION
  19. Sharing Laravel @stauffermatt COMPOSE ALL THE THINGS What is
 Composer?

    Dependency manager for PHP (Like Ruby Gems, Node Package Manager, etc.) STEP 4
  20. Sharing Laravel @stauffermatt ILLUMINATE YOUR PROJECT Illuminate packages Mail Auth

    Queue Config Cache Session Workbench Validation Translation Support Routing Remote Pagination Log Http Html Hashing Events Database Cookie STEP 5 Commonly exported: • Database (Eloquent) • Config • Cache • Session • Support (Collections, array helpers, etc.) • Pagination (Because Phil Sturgeon) • Many more…
  21. Sharing Laravel @stauffermatt <?php ! /* In composer.json require illuminate/support

    */ ! require_once('vendor/autoload.php'); ! // Collection $people = new Illuminate\Support\Collection([ 'Declan', 'Abner', 'Mitzi' ]); ! $people->each(function($person) { echo "Well, howdy $person!<br>"; }); Simple Use Example
  22. Sharing Laravel @stauffermatt <?php require_once('vendor/autoload.php'); $capsule = new Illuminate\Database\Capsule\Manager; $capsule->addConnection([

    'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => 'supersecret!', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '' ]); $capsule->bootEloquent(); ! // Other file class User extends Illuminate\Database\Eloquent\Model {} $user = User::find(1); Using the Capsule
  23. Sharing Laravel @stauffermatt $app = new Illuminate\Container\Container; $app->bind('app', $app); !

    $providers = [ 'Illuminate\Queues\QueueServiceProvider', 'Illuminate\Events\EventServiceProvider' ]; ! foreach ($providers as $provider) { with (new $provider($app))->register(); } ! $aliases = [ 'Queue' => 'Illuminate\Support\Facades\Queue', 'Config' => 'Illuminate\Support\Facades\Config', ]; ! foreach ($aliases as $alias => $class) { class_alias($class, $alias); } ! Illuminate\Support\Facades\Facade::setFacadeApplication($app); Bootstrapping Illuminate
  24. Sharing Laravel @stauffermatt LARAVEL-AS-A-COMPONENT Just a little bit of Laravel

    Laravel-as-a-frontend: Build a modern Laravel app that consumes data from your existing site Laravel-as-a-backend: Build a modern Laravel app that replaces an existing data store STEP 7
  25. Sharing Laravel @stauffermatt Craft API CraftCMS is based on Yii;

    like Laravel’s CMS step-brother Craft API plugin (in development) Example: @mattgreen110 LARAVEL-AS-A-COMPONENT STEP 7
  26. Sharing Laravel @stauffermatt “The Right Toolbox” Anthony Colangelo at HappyCog

    Eloquent objects to describe your Craft database structure https://speakerdeck.com/ acolangelo/the-right-toolbox LARAVEL-AS-A-COMPONENT STEP 7
  27. Sharing Laravel @stauffermatt DEEP/OpenAPI/Export It Same thing, but for ExpressionEngine

    https://github.com/rsanchez/Deep https://github.com/putyourlightson/open-api https://www.mithra62.com/ (Export It) LARAVEL-AS-A-COMPONENT STEP 7
  28. Sharing Laravel @stauffermatt Know why you’re here What is it

    about Laravel that brought you to this conference?
  29. Sharing Laravel @stauffermatt Bring that, incrementally, to legacy apps •

    Undercover Laravel • OOP & Design/Architecture Patterns • Modernize codebase • Composer & Illuminate Packages • Get Saas-y • Laravel as a Component
  30. Sharing Laravel @stauffermatt Additional resources Modernizing Legacy Applications in PHP

    
 Paul Jones ! Working Effectively with Legacy Code
 Michael Feathers ! Refactoring 
 Martin Fowler, Kent Beck, et al. ! Mastering Object Oriented PHP
 Brandon Savage