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

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


  1. Sharing Laravel Laravel’s Best Assets @stauffermatt Bringing To Any Project

  2. Sharing Laravel @stauffermatt Matt Stauffer @stauffermatt Partner, Technical Director Tighten

  3. Sharing Laravel @stauffermatt Gainesville I’m going to say y’all a

    lot ( )
  4. Sharing Laravel @stauffermatt Why is this talk necessary?

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

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

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

    best assets of the Laravel world to non-Laravel projects.
  8. 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
  9. Sharing Laravel @stauffermatt WHAT MAKES LARAVEL SO GREAT?

  10. 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
  11. Sharing Laravel @stauffermatt but Matt, my project is in CodeIgniter/etc.

    … what do I do? You, dear audience member
  12. Sharing Laravel @stauffermatt WHY DON’T WE JUST REWRITE IT IN

  13. Sharing Laravel @stauffermatt WHY DON’T WE JUST REWRITE IT IN

  14. Sharing Laravel @stauffermatt response to legacy code: trash it. Sharing

    Laravel @stauffermatt or, more likely, “KILL IT WITH FIRE”
  15. 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.
  16. Sharing Laravel @stauffermatt The incremental approach

  17. Sharing Laravel @stauffermatt UNDERCOVER LARAVEL

  18. 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.
  19. Sharing Laravel @stauffermatt The 7 steps to “Laravel-izing” your coding

    and your legacy codebase
  20. Sharing Laravel @stauffermatt Learn the basics STEP 1

  21. Sharing Laravel @stauffermatt Crap code
 wrapped in Laravel
 is still

    crap code.
  22. 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
  23. 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
  24. Sharing Laravel @stauffermatt LEARN THE BASICS Design patterns Examples: •

    Adapter Pattern • Singleton Pattern • Observer Pattern • Many more… STEP 1
  25. Sharing Laravel @stauffermatt LEARN THE BASICS Architecture patterns Hexagonal Architecture

    Domain Driven Design STEP 1
  26. Sharing Laravel @stauffermatt Learn from Laravel STEP 2

  27. Sharing Laravel @stauffermatt LEARN FROM LARAVEL Subscribe to Laracasts STEP

  28. 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 LEARN FROM LARAVEL STEP 2
  29. 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
  30. 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
  31. Sharing Laravel @stauffermatt Read the source LEARN FROM LARAVEL STEP

  32. Sharing Laravel @stauffermatt Modernize your foundation STEP 3

  33. 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: STEP 3
  34. Sharing Laravel @stauffermatt Stay Classy Procedural code/functions -> methods on

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

    autoloading Drop as many includes/requires as possible STEP 3 MODERNIZE YOUR FOUNDATION
  36. 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
  37. Sharing Laravel @stauffermatt Learn more STEP 3
 why-modern-php-is-awesome-and-how-you-can-use-it-today MODERNIZE

  38. Sharing Laravel @stauffermatt Compose all the things STEP 4

  39. Sharing Laravel @stauffermatt COMPOSE ALL THE THINGS What is

    Dependency manager for PHP (Like Ruby Gems, Node Package Manager, etc.) STEP 4
  40. Sharing Laravel @stauffermatt Community packages Packagist Symfony PHP League DIY

  41. Sharing Laravel @stauffermatt Illuminate your project STEP 5

  42. 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…
  43. Sharing Laravel @stauffermatt TITLE FOR STEP 1 mattstauffer/ illuminateNonLaravel

  44. 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
  45. 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
  46. 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
  47. Sharing Laravel @stauffermatt Contribute please! ILLUMINATE YOUR PROJECT STEP 5

  48. Sharing Laravel @stauffermatt Get SaaS-y STEP 6

  49. Sharing Laravel @stauffermatt Transactional Email Sendgrid Mailgun Mandrill etc. GET

  50. Sharing Laravel @stauffermatt Logs Papertrail Bugsnag/Sentry/Airbrake/etc. GET SAAS-Y STEP 6

  51. Sharing Laravel @stauffermatt GET SAAS-Y Code quality Scrutinizer Sensio Labs

    Insights CodeClimate STEP 6
  52. Sharing Laravel @stauffermatt Continuous Integration/ Deployment Codeship Jenkins Travis Semaphore

    Scrutinizer GET SAAS-Y STEP 6
  53. Sharing Laravel @stauffermatt Laravel-as-a- component STEP 7

  54. 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
  55. 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
  56. Sharing Laravel @stauffermatt “The Right Toolbox” Anthony Colangelo at HappyCog

    Eloquent objects to describe your Craft database structure acolangelo/the-right-toolbox LARAVEL-AS-A-COMPONENT STEP 7
  57. Sharing Laravel @stauffermatt

  58. Sharing Laravel @stauffermatt DEEP/OpenAPI/Export It Same thing, but for ExpressionEngine (Export It) LARAVEL-AS-A-COMPONENT STEP 7
  59. Sharing Laravel @stauffermatt Conclusie

  60. Sharing Laravel @stauffermatt Know why you’re here What is it

    about Laravel that brought you to this conference?
  61. Sharing Laravel @stauffermatt (other than this mug)

  62. 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
  63. 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
  64. Sharing Laravel @stauffermatt Compelling action slide