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

Upgrading a Laravel 4 Application to Laravel 5

Upgrading a Laravel 4 Application to Laravel 5

Given as a Nomad PHP Lightning Talk - 2015-03-19

From this blog post:


Matt Stauffer

March 19, 2015

More Decks by Matt Stauffer

Other Decks in Technology


  1. Upgrading a Laravel 4 application to Laravel 5

  2. Wherefore? Major framework version upgrades: Thoughtbot, Rails 2->3 Large app,

    2 devs, 1 month Laravel 4->5 is a few hours to a day, depending on the app. But, it’s not drag-and-drop
  3. Today’s Guinea Pig: SaveMyProposals

  4. (Check it out on Github later) github.com/mattstauffer/savemyproposals

  5. Caveat Do the minimum viable upgrade.

  6. Git Caveat If you want to maintain file consistency, either

    don't commit along the way, or squash at the end
  7. Step 1 Branch your app and nuke the files

  8. cd Sites/savemyproposals-original-site git checkout -b laravel-5-upgrade rm -rf * rm

    .gitattributes rm .gitignore rm .travis.yml/etc.
  9. Step 2 Install Laravel 5 fresh into the new branch

  10. git remote add laravel https://github.com/laravel/laravel.git git fetch laravel git merge

    laravel/master --squash git add . git commit -am "Bring in Laravel 5 base." composer install
  11. Step 3 Clone old site parallel

  12. git clone git:giturl.git ../savemyproposals-temp-clone So we have two folders parallel:

    ls .. Total 123 drwxr-xr-x 1 usr staff 748 Jan 1 2015 savemyproposals-temp-clone drwxr-xr-x 1 usr staff 748 Jan 1 2012 savemyproposals-original-site
  13. Step 4 Set your domain namespace (optionally)

  14. Running this: php artisan app:name SaveMyProposals Takes us from: <?php

    namespace App\Http\Controllers; to: <?php namespace SaveMyProposals\Http\Controllers;
  15. Step 5 Migrate your Composer dependencies

  16. Easiest option: copy your dependencies over to the new composer.json.

    Then composer update.
  17. Step 6 Move your primary domain folder

  18. cp -r ../savemyproposals-temp-clone/src ./app Or, in a GUI, copy all

    the files from your old domain folder (e.g. /src or /app/YourProjectName) into the app folder (/app). /app Console/Commands/etc. Http/Controllers/etc. Your/Domain/Folders/etc.
  19. Step 7 Move your framework-specific code

  20. Step 7.a Move commands from app/commands to app/Console/Commands

  21. Step 7.b Move controllers from app/controllers to app/Http/Controllers

  22. Step 7.c Move database from app/database to database

  23. Step 7.d Move filters from app/filters.php to app/Providers/[email protected]()

  24. Step 7.e Move language files from app/lang to resources/lang

  25. Step 7.f If you're still using app/models, add it to

    composer.json: "autoload": { "classmap": [ "database", "app/models" ] }
  26. Step 7.g Move routes from app/routes.php to app/Http/routes.php *Note: Adjust

    built-in filter calls (e.g. ['before' => 'auth']) to be middleware calls (e.g. ['middleware' => 'auth'])
  27. Step 7.h Move any bindings you registered in start/global.php to

    app/Providers/[email protected]()
  28. Step 7.i Move tests from app/tests to tests

  29. Step 8 Migrate your configuration

  30. 4 Shift your mentality (no environment folders) 4 Move over

    your keys into .env.example 4 Copy .env.example to .env and move over your values 4 Learn APP_ENV, APP_DEBUG, APP_KEY, DB_*, *_DRIVER
  31. Step 9 Move any loose files-- readme.md, .travis.yml, etc.-- into

    the new branch
  32. Step 10 Update auth (User) model The fastest way is

    to use the new User model. If not, read the instructions.
  33. Step 11 Update your Blade echo syntax

  34. Change {{ and }} to {!! and !!} anywhere you

    need un-escaped echo OR // In [email protected](): \Blade::setRawTags('{{', '}}'); \Blade::setContentTags('{{{', '}}}'); \Blade::setEscapedContentTags('{{{', '}}}');
  35. Step 12 Configure Laravel

  36. Inform Laravel that your controllers have no namespace: // app/providers/RouteServiceProvider

    protected $namespace = null; Add the controllers and commands directories to the composer.json classmate autoload if you're not namespacing them.
  37. Step 13 Move public files

  38. Delete every file out of the new public directory except

    index.php, and move your old files in.
  39. Step 14 Update application code with API changes

  40. Just a few API changes: 4 SoftDeletingTrait on models is

    now SoftDeletes 4 No more Eloquent remember()--cache manually 4 Replace $paginator->links() with $paginator- >render() 4 Update Composer dependency pda/pheanstalk~2.1 to pda/pheanstalk~3.0
  41. Step 15 CSRF Routing Disable middleware in app/Http/Kernel or set

    form field _token to value csrf_token()
  42. Step 16 HTML & Forms (optionally) github.com/laravelcollective

  43. Step 17 Bringing Whoops Back (optionally) mattstauffer.co/blog/bringing-whoops-back-to-laravel-5

  44. Step 18 Check the upgrade guide: http://laravel.com/docs/5.0/upgrade

  45. Thanks! https://joind.in/14291 @stauffermatt mattstauffer.co