$30 off During Our Annual Pro Sale. View Details »

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
Tweet

More Decks by Matt Stauffer

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  5. Sharing Laravel @stauffermatt
    Most Laravel tutorials
    assume a blank slate.
    greenfield, adj: a project that
    lacks any constraints imposed by
    prior work.

    View Slide

  6. Sharing Laravel @stauffermatt
    The problem:
    Most of our work is on

    existing codebases.
    FREEDOM
    Old Code

    View Slide

  7. Sharing Laravel @stauffermatt
    The solution:
    Learn how to bring the best
    assets of the Laravel world to
    non-Laravel projects.

    View Slide

  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

    View Slide

  9. Sharing Laravel @stauffermatt
    WHAT MAKES LARAVEL
    SO GREAT?

    View Slide

  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

    View Slide

  11. Sharing Laravel @stauffermatt
    but Matt,
    my project is in
    CodeIgniter/etc. …
    what do I do?
    You, dear audience member

    View Slide

  12. Sharing Laravel @stauffermatt
    WHY DON’T WE JUST
    REWRITE IT IN LARAVEL?

    View Slide

  13. Sharing Laravel @stauffermatt
    WHY DON’T WE JUST
    REWRITE IT IN LARAVEL?
    TERRIBLE IDEA

    View Slide

  14. Sharing Laravel @stauffermatt
    response to
    legacy code:
    trash it.
    Sharing Laravel @stauffermatt
    or, more likely,
    “KILL IT WITH FIRE”

    View Slide

  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.

    View Slide

  16. Sharing Laravel @stauffermatt
    The incremental
    approach

    View Slide

  17. Sharing Laravel @stauffermatt
    UNDERCOVER
    LARAVEL

    View Slide

  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.

    View Slide

  19. Sharing Laravel @stauffermatt
    The 7 steps to
    “Laravel-izing” your
    coding and your
    legacy codebase

    View Slide

  20. Sharing Laravel @stauffermatt
    Learn the basics
    STEP 1

    View Slide

  21. Sharing Laravel @stauffermatt
    Crap code

    wrapped in Laravel

    is still crap code.

    View Slide

  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

    View Slide

  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

    View Slide

  24. Sharing Laravel @stauffermatt
    LEARN THE BASICS
    Design patterns
    Examples:
    • Adapter Pattern
    • Singleton Pattern
    • Observer Pattern
    • Many more…
    STEP 1

    View Slide

  25. Sharing Laravel @stauffermatt
    LEARN THE BASICS
    Architecture patterns
    Hexagonal Architecture
    Domain Driven Design
    STEP 1

    View Slide

  26. Sharing Laravel @stauffermatt
    Learn from
    Laravel
    STEP 2

    View Slide

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

    View Slide

  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

    http://wiki.laravel.io/books
    LEARN FROM LARAVEL STEP 2

    View Slide

  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

    View Slide

  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

    View Slide

  31. Sharing Laravel @stauffermatt
    Read the source
    LEARN FROM LARAVEL STEP 2

    View Slide

  32. Sharing Laravel @stauffermatt
    Modernize your
    foundation
    STEP 3

    View Slide

  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: 

    https://github.com/laravel/framework/blob/4.2/.scrutinizer.yml
    STEP 3

    View Slide

  34. Sharing Laravel @stauffermatt
    Stay Classy
    Procedural code/functions -> methods
    on classes -> retroactive designed OOP
    Namespaces & autoloading
    STEP 3
    MODERNIZE YOUR FOUNDATION

    View Slide

  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

    View Slide

  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

    View Slide

  37. Sharing Laravel @stauffermatt
    Learn more
    STEP 3
    https://speakerdeck.com/mattstauffer/

    why-modern-php-is-awesome-and-how-you-can-use-it-today
    MODERNIZE YOUR FOUNDATION

    View Slide

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

    View Slide

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

    Composer?
    Dependency manager
    for PHP
    (Like Ruby Gems, Node
    Package Manager, etc.)
    STEP 4

    View Slide

  40. Sharing Laravel @stauffermatt
    Community packages
    Packagist
    Symfony
    PHP League
    DIY
    COMPOSE ALL THE THINGS STEP 4

    View Slide

  41. Sharing Laravel @stauffermatt
    Illuminate your
    project
    STEP 5

    View Slide

  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…

    View Slide

  43. Sharing Laravel @stauffermatt
    TITLE FOR STEP 1
    github.com/
    mattstauffer/
    illuminateNonLaravel
    ILLUMINATE YOUR PROJECT STEP 5

    View Slide

  44. Sharing Laravel @stauffermatt
    !
    /* 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!
    ";
    });
    Simple Use
    Example

    View Slide

  45. Sharing Laravel @stauffermatt
    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

    View Slide

  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

    View Slide

  47. Sharing Laravel @stauffermatt
    Contribute
    please!
    ILLUMINATE YOUR PROJECT STEP 5

    View Slide

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

    View Slide

  49. Sharing Laravel @stauffermatt
    Transactional Email
    Sendgrid
    Mailgun
    Mandrill
    etc.
    GET SAAS-Y STEP 6

    View Slide

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

    View Slide

  51. Sharing Laravel @stauffermatt
    GET SAAS-Y
    Code quality
    Scrutinizer
    Sensio Labs Insights
    CodeClimate
    STEP 6

    View Slide

  52. Sharing Laravel @stauffermatt
    Continuous Integration/
    Deployment
    Codeship
    Jenkins
    Travis
    Semaphore
    Scrutinizer
    GET SAAS-Y STEP 6

    View Slide

  53. Sharing Laravel @stauffermatt
    Laravel-as-a-
    component
    STEP 7

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  57. Sharing Laravel @stauffermatt
    https://speakerdeck.com/acolangelo/the-right-toolbox

    View Slide

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

    View Slide

  59. Sharing Laravel @stauffermatt
    Conclusie

    View Slide

  60. Sharing Laravel @stauffermatt
    Know why you’re here
    What is it about Laravel that
    brought you to this conference?

    View Slide

  61. Sharing Laravel @stauffermatt
    (other than this mug)

    View Slide

  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

    View Slide

  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

    View Slide

  64. Sharing Laravel @stauffermatt
    Compelling
    action slide

    View Slide