Advertisement

All Aboard for Laravel 5.1

Web and iOS Application Developer
Nov. 18, 2015
Advertisement

More Related Content

Advertisement
Advertisement

All Aboard for Laravel 5.1

  1. All Aboard for Laravel 5.1 Jason McCreary "JMac" @gonedark
  2. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 A short rant about frameworks Choose wisely and code carefully 2
  3. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Talk the Talk 1. What’s New in Laravel 5.0 2. Upgrading from Laravel 4.2 3. What’s New in Laravel 5.1 4. What’s coming in Laravel 5.2 3
  4. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 The Goal “To get you familiar with the new features in Laravel so you are comfortable upgrading your projects to 5.1” 4
  5. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Why Upgrade? “Laravel 5.1 is the first release of Laravel to receive long term support. Laravel 5.1 will receive bug fixes for 2 years and security fixes for 3 years.This support window is the largest ever provided for Laravel and provides stability and peace of mind for larger, enterprise clients and customers.” 5
  6. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 What’s New… In Laravel 5.0 6
  7. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 New Folder Structure • Follows PSR-4 naming conventions • App namespacing • Models live in the default namespace • Everything related to HTTP lives under Http/ (controllers, middleware, requests) • Views live outside the App namespace within resources/. 7
  8. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Folder Structure 8 4.2 5.0
  9. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 App Folder 9 4.2 5.0
  10. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Namespacing <?php namespace App;
 
 // ... 
 class User extends Model {
 // ...
 } 10
  11. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Namespacing <?php namespace AppHttpControllers;
 
 class HomeController extends Controller { 
 public function index()
 {
 return view('home');
 }
 } 11
  12. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Namespacing "autoload": {
 "classmap": [
 "app/commands",
 "app/controllers",
 "app/models",
 "app/database/migrations",
 "app/database/seeds",
 "app/tests/TestCase.php"
 ]
 }, 12 "autoload": { "classmap": [ "database" ], "psr-4": { "App": "app/" } }, 4.2 5.0
  13. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 New Objects • Events are now objects, not strings! • Command objects allow simpler job processing • Requests are now objects • Middleware objects to replace Filters 13
  14. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Middleware “HTTP middleware provide a convenient mechanism for filtering HTTP requests entering your application.” 14
  15. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Middleware <?php namespace AppHttpControllers;
 
 class HomeController extends Controller {
 public function __construct()
 {
 $this->middleware('auth');
 } // ...
 } 15
  16. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Middleware <?php namespace AppHttpMiddleware; use Closure; class TimeoutMiddleware { public function handle($request, Closure $next) { if (abs(time() - $request->input(‘ttl’)) > 300) { return redirect('timeout'); } return $next($request); } } 16
  17. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Form Request Objects “A simple way to customize request validation automatically.” 17
  18. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Form Request Objects <?php namespace AppHttpRequests; class RegisterRequest extends FormRequest { public function rules() { return [ 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:8' ]; } } 18
  19. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Controller Method Injection “In addition to constructor injection, you may now type-hint dependencies on controller methods. These objects will be resolved and available along with any route parameters.” 19
  20. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Controller Method Injection public function register( RegisterRequest $request, RegistrationRepository $registration) { // ... } 20
  21. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 ControllerValidation "If FormRequests are a little too much, the Laravel 5 base controller now includes a ValidatesRequests trait.This trait provides a simple validate() method to validate incoming requests." 21
  22. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 ControllerValidation public function register(Request $request) { $this->validate($request, [ 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:8' ]); } 22
  23. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Environment Configuration “Laravel 5 uses the DotEnv library to condense all configuration values into a single .env file. These values get loaded into $_ENV and available through the env() helper method.” 23
  24. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Environment Configuration APP_ENV=local
 APP_DEBUG=true
 APP_KEY=SomeRandomString
 
 DB_HOST=localhost
 DB_DATABASE=homestead
 DB_USERNAME=homestead
 DB_PASSWORD=secret
 
 CACHE_DRIVER=file
 SESSION_DRIVER=file 24 <?php return array( 'APP_KEY' => 'secretkey', ); 4.2 5.0
  25. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Environment Configuration 25 <?php
 
 return [ 
 'debug' => env('APP_DEBUG'),
 // ... 
 'key' => env('APP_KEY', 'SomeRandomString'),
 
 // ...
 ];
  26. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Artisan Updates • Now includes several make commands to generate common objects • tinker is now backed by Psysh for a more powerful REPL • route commands for listing and caching routes 26
  27. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Authentication “User registration, authentication, and password reset controllers are now included out of the box, as well as simple corresponding views.” 27
  28. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 User Model (4.2) <?php
 
 use IlluminateAuthUserTrait;
 use IlluminateAuthUserInterface;
 use IlluminateAuthRemindersRemindableTrait;
 use IlluminateAuthRemindersRemindableInterface;
 
 class User extends Eloquent implements UserInterface, RemindableInterface {
 
 use UserTrait, RemindableTrait;
 
 protected $table = 'users';
 protected $hidden = array('password', 'remember_token');
 } 28
  29. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 User Model (5.0) <?php namespace App;
 
 use IlluminateAuthAuthenticatable;
 use IlluminateDatabaseEloquentModel;
 use IlluminateAuthPasswordsCanResetPassword;
 use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
 use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
 
 class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
 
 use Authenticatable, CanResetPassword;
 
 protected $table = 'users';
 protected $fillable = ['name', 'email', 'password'];
 protected $hidden = ['password', 'remember_token'];
 } 29
  30. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Blade “By default, Laravel 5.0 escapes all output from both the {{ }} and {{{ }}} Blade directives. A new {!! !!} directive has been introduced to display raw, unescaped output.” 30
  31. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Libraries and Packages • Laravel Elixir - asset management • Laravel Socialite - authentication with Oauth services • Flysystem - filesystem abstraction library • Laravel Scheduler - command manager 31
  32. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Upgrading Laravel From 4.2 to 5.0 32
  33. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Two Approaches • “New move-in” • “Update in-place” 33
  34. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 What to do? “As always, it depends. However, the documentation recommends migrating your 4.2 app to a new Laravel 5 app. So, new move-in it is.” 34
  35. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Basic Steps 1. Create a new Laravel 5 app 2. Migrate configuration 3. Move app files 4. Add namespacing 5. Review Bindings 6. Miscellaneous changes 7. Blade Tag changes 8. Update dependencies 9. Cross fingers 35
  36. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Do not pass 5.0! “Going directly to 5.1 will make the migration more complicated as there are significant changes between 5.0 and 5.1. Do not pass 5.0. Otherwise you will not collect $200 and you will go to jail.” 36
  37. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Create new Laravel 5 app composer create-project laravel/laravel --prefer-dist 37
  38. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Create new Laravel 5 app composer global require “laravel/installer=~1.1” laravel new project-name 38
  39. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Migrate Configuration 1. Copy configuration from your 4.2 app’s env.php to .env 2. Compare configurations from your 4.2 app’s config/ 3. Recreate environment configurations from your 4.2 app’s config/env/ to.env 4. Update environment configurations to use env() 39
  40. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Migrate Configuration “Be sure to leave .env.example file in your project. It should contain placeholder values that will make it easy for other developers to copy and configure for their environment.” 40
  41. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Migrate Configuration “When managing multiple environments, you may find it easiest to create several .env files and symlink the .env within each environment.” 41
  42. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Move Files 42 4.2 5.0
  43. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Move Files • routes.php to app/Http/routes.php • app/views to resources/views • app/database to database/ 43
  44. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Add namespace “You do not need to do this.You can keep everything in the global namespace and add the paths to the classmap just as in Laravel 4.2. However, not doing so carries over technical debt as your project will not truly follow Laravel 5.0’s configuration.” 44
  45. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Add namespace <?php namespace AppHttpControllers;
 
 class YourController extends Controller { // ...
 }
 45
  46. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Add namespace <?php namespace App;
 
 class YourModel extends Eloquent { // ...
 }
 46
  47. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Review Bindings "Move any service container bindings from start/ global.php to the register method of the app/ Providers/AppServiceProvider.php file." 47
  48. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Miscellaneous Changes •SoftDeletingTrait is now SoftDeletes •User Authentication changes •Pagination method changes •Oh yeah, and the Form and Html Facades are gone. 48
  49. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Blade Changes 1. Cry 2. Bargain 3. Face reality 4. Start changing every {{ }} to {!! !!} 5. Remove all blade comment tags {{-- --}} (you should have known better) 6. Smile 49
  50. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Update your dependencies "Review your dependencies then run composer update" 50
  51. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Cross fingers 51
  52. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 201552
  53. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Questions… Ask now! Cause we’re moving to 5.1 53
  54. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 What’s New… In Laravel 5.1 54
  55. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Project Updates • Long Term Support • Requires PHP > 5.5.9 • Follows PSR-2 • Updated Documentation • Uses openssl for encryption, instead of mcrypt 55
  56. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 App Folder 56 5.0 5.1
  57. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Event Broadcasting "makes it easy to broadcast your events over a websocket connection." 57
  58. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Middleware Parameters <?php namespace AppHttpMiddleware; use Closure; class RoleMiddleware { public function handle($request, Closure $next, $role) { if (!$request->user()->hasRole($role)) { // Redirect... } return $next($request); } } 58
  59. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Middleware Parameters Route::put( ‘post/{id}', ['middleware' => 'role:editor', function ($id) { // ... }] ); 59
  60. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Focus on Testing • Stronger integration testing • Model factories 60
  61. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 What’s Coming… In Laravel 5.2 61
  62. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Laravel 5.2 • Coming December 2015 • Route filters have been deprecated in preference of middleware. • *_fetch methods deprecated in favor of *_pluck 62
  63. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Resources • What’s new in Laravel 5 Laracasts • Laravel Official Upgrade Guide • Laravel In-Place Upgrade Guide • What’s new in Laravel 5.1 Laracasts 63
  64. All Aboard for Laravel 5.1 - Jason McCreary - php[world] 2015 Questions… @gonedark on Twitter 64
Advertisement