87
Adventures in Laravel 5 https://joind.in/talk/d0f4a Joe Ferguson https://github.com/svpernova09/quickstart-basic Want to follow along?

MidwestPHP 2016 - Adventures in Laravel 5

Embed Size (px)

Citation preview

Page 1: MidwestPHP 2016 - Adventures in Laravel 5

Adventures in Laravel 5

https://joind.in/talk/d0f4a

Joe Ferguson

https://github.com/svpernova09/quickstart-basic

Want to follow along?

Page 2: MidwestPHP 2016 - Adventures in Laravel 5

Who Am I?

Joe Ferguson

PHP Developer

Twitter: @JoePFerguson

Organizer of @MemphisPHP

@NomadPHP Lightning Talks

PHP FIG Secretary

Passionate about Community

Page 3: MidwestPHP 2016 - Adventures in Laravel 5

Getting Started with Laravel

https://github.com/svpernova09/quickstart-basic

Want to follow along?

Page 4: MidwestPHP 2016 - Adventures in Laravel 5

Database, Migrations, Seeding

database/

factories/ - Default Model Attributes

migrations/ - Version control for your DB Schema

seeds/ - Sample / Test Data for your DB

Page 5: MidwestPHP 2016 - Adventures in Laravel 5

Database, Migrations, Seeding

Page 6: MidwestPHP 2016 - Adventures in Laravel 5

Routing

app/

Http/

routes.php

Page 7: MidwestPHP 2016 - Adventures in Laravel 5

Layouts

resources/

views/

layouts/

app.blade.php

Page 8: MidwestPHP 2016 - Adventures in Laravel 5

Views

resources/

views/

tasks.blade.php

Page 9: MidwestPHP 2016 - Adventures in Laravel 5

Open Laravel-5.2-basic-quickstart

Page 10: MidwestPHP 2016 - Adventures in Laravel 5

Basic Quick Start

https://laravel.com/docs/5.2/quickstart

If you already have Vagrant, Run:

`./vendor/bin/homestead make`

`vagrant up`

Run the migration: `php artisan migrate`

Page 11: MidwestPHP 2016 - Adventures in Laravel 5

Viewing the Migration

Laravel-5.2-basic-quickstart/database/

migrations/2015_10_27_141258_create_tasks_table.php

Page 12: MidwestPHP 2016 - Adventures in Laravel 5

Viewing the ModelLaravel-5.2-basic-quickstart/app/Task.php

Page 13: MidwestPHP 2016 - Adventures in Laravel 5

Viewing the RoutesLaravel-5.2-basic-quickstart/app/Http/routes.php

Page 14: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1

Add / edit migration to create tasks table

Add / edit Task model

Add routes (just like task)

Add / edit Views

You can safely disregard any authentication related to users

Page 15: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1 Task Migration

Page 16: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1 Task Model

Page 17: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1 Task Create Route

Page 18: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1 Task Delete Route

Page 19: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1 Task View

Page 20: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 1 Task Create View

Page 21: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 2

Assign users to tasks as you create tasks

When listing tasks, show the user they have assigned them

When listing users, show how many tasks they have assigned to that user

You can safely disregard any authentication related to users

Page 22: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 2 Task Migration

Page 23: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 2 Model Relationships

Task Model

User Model

Page 24: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 2 Show Assigned User

Page 25: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 2 Count User’s Tasks

Page 26: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3

Write Tests for User Functionality

Write Tests for Task Functionality

Page 27: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3 Hints

Use Model Factories to seed test data

Make sure the task is saved to the DB

Make sure the user is saved to the DB

Ensure the user delete request succeeds

Ensure the task delete request succeeds

Ensure the index page loads and contains data

You can safely disregard any authentication related to users

Page 28: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3 ModelFactory

Page 29: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3Make sure the task is saved to the DB

Page 30: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3Make sure the user is saved to the DB

Page 31: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3Ensure the user delete request succeeds

Page 32: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3Ensure the task delete request succeeds

Page 33: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3Ensure the index page loads

Page 34: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 3Ensure the index page contains data

Page 35: MidwestPHP 2016 - Adventures in Laravel 5

Easy Stuff Right?

Page 36: MidwestPHP 2016 - Adventures in Laravel 5

Let’s clean up a bit

Page 37: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 4

Move “Create” Forms to own views

Move “Current” html to own views

Move Create/Delete User Functionality to Controller method

Move Create/Delete Task Functionality to Controller method

Page 38: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 4Move “Create” Forms to own views

Page 39: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 4Move “Current” html to own views

Page 40: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 4Move Create/Delete User Functionality to Controller method

Page 41: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 4Move Create/Delete Task Functionality to Controller method

Page 42: MidwestPHP 2016 - Adventures in Laravel 5

Using Laravel Facades

Using Static Methods:

`User::all()`

`User::find(1);

`User::where(‘user_id`, 1)->get()

`User::where(‘email’, ‘[email protected]’)->get();

Page 43: MidwestPHP 2016 - Adventures in Laravel 5

Using Dependency Injection

Using Injected Object

`$this->user->all()`

`$this->user->find(1);

`$this->user->where(‘user_id`, 1)->get()

`$this->user->where(‘email’, ‘[email protected]’)->get();

Page 44: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 5

Update your controllers to inject your models instead of using static methods (Facades)

Page 45: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 5Using Static

Dependency Injection

Page 46: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 6

Implement index view for tasks

Implement index view for users

Implement edit functionality for tasks

Add tests for your new routes & methods

Page 47: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 6Implement index view for users

Route Controller

Page 48: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 6Implement index view for tasks

Route Controller

Page 49: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 6Implement edit functionality for tasks

Route Controller

Page 50: MidwestPHP 2016 - Adventures in Laravel 5

Exercise 6Handle Edit Form

Route Controller

Page 51: MidwestPHP 2016 - Adventures in Laravel 5

What’s “TaskStoreRequest”?

Page 52: MidwestPHP 2016 - Adventures in Laravel 5

Laravel Form Request

Page 53: MidwestPHP 2016 - Adventures in Laravel 5

Whirlwind tour on Laravel

Page 54: MidwestPHP 2016 - Adventures in Laravel 5

New(ish*) Toys in Laravel

*Since 4.2

Page 55: MidwestPHP 2016 - Adventures in Laravel 5

New Directory Structure

Page 56: MidwestPHP 2016 - Adventures in Laravel 5

Laravel 5 DirectoryApp directory now "Your Application" / "Entry point to app”

Laravel 4 Artisan Commands -> Console in App folder

Web stuff in Http

Controllers all have name space

filters.php -> Broken out into separate classes/files.

More focus on Service Providers -> Filter Service Providers

No more global.php -> Use service providers

Removed models directory. Can be just in app folder. PSR-4 default

Don't like the app namespace? artisan app:name MyApplication

Page 57: MidwestPHP 2016 - Adventures in Laravel 5

Blade Changes

Laravel 4 uses {{ to echo and {{{ To echo escaped

Laravel 5 {{ and {{{ will echo escaped and {!! is used to echo raw

Biggest impact is likely form helpers: {!! Form::open() !!}

Page 58: MidwestPHP 2016 - Adventures in Laravel 5

Commands

Commands (app/Commands) - Message containing only info needed to do something

Command Handler (app/Handlers/Commands) - Class that does something in response to a command

Command Bus - Allows dispatch of commands, matches commands to handlers

Self handling commands just need a handle() method on the command & implements SelfHandling

Page 59: MidwestPHP 2016 - Adventures in Laravel 5

Events

Events (app/Events)

Events have handlers (similar to Commands)

Bind Events via app\Providers\EventServiceProvider.php

Events inform the system something happened VS demanding action from the system

Page 60: MidwestPHP 2016 - Adventures in Laravel 5

Form RequestsSpecial class for validating and authorizing form submissions

Each class has rules() (returns array) and authorize() (returns boolean) methods

Benefit of rules & authorize being methods is you can perform logic

Type Hinting your forms to the Form Request will automatically validate your forms

If validation fails, errors will be available to the view and redirected back.

This happens because the FormRequestServiceProvider listens for anything being resolved is an instance of FormRequest and calls the validate method.

Page 61: MidwestPHP 2016 - Adventures in Laravel 5

Helpersview() - Get a View instance for the given view path

action() - Generate a URL for a given controller action

app_path() - Get the fully qualified path to the app directory

asset() - Generate a URL for an asset.

Routing – get(), delete(), put()

Page 62: MidwestPHP 2016 - Adventures in Laravel 5

Route Caching

artisan route:cache

Serializes your routes.php

Benefits: Faster routing

Drawback: Must run artisan route:clear on every routes.php change

Page 63: MidwestPHP 2016 - Adventures in Laravel 5

MiddlewareImplements decorator pattern. request -> does work -> returns object to next layer

Laravel uses middleware for Encrypting/Decrypting cookies, Reading/Writing Sessions

artisan make:middleware MyMiddleware (app/Http/Middleware)

Middleware registered in app/Http/Kernel.php

Can run before or after the request has been processed.

Easiest example would be auth

Page 64: MidwestPHP 2016 - Adventures in Laravel 5

Controller Method Injection

Can inject dependencies into methods, no longer via constructor

Purpose is to help developers write cleaner code

Page 65: MidwestPHP 2016 - Adventures in Laravel 5

Changes to Illuminate Packages

Form or HTML helpers no longer in Core, must be pulled in via composer.

add "laravelcollective/html": "~5.0" to composer

update config/app.php

Page 66: MidwestPHP 2016 - Adventures in Laravel 5

Elixir

API for defining basic Gulp tasks for your app.

Requires nodejs

Put your sass/less files in resources/assets/sass|less

Can trigger sass/less/phpunit/phpspec, combine stylesheets

Page 67: MidwestPHP 2016 - Adventures in Laravel 5

Socialite

Easy to use social logins with oauth

Supports Facebook, Twitter, Google, Github, and Bitbucket

Page 68: MidwestPHP 2016 - Adventures in Laravel 5

Contracts

Set of interfaces that define the core services provided by the framework

Depend on abstractions, not concrete dependencies.

Write code that doesn't have to be aware of the laravel framework

Page 69: MidwestPHP 2016 - Adventures in Laravel 5

Implicit Route Model Binding

Laravel will automatically resolve type-hinted Eloquent model's defined in routes or controller actions whose variable names match a route segment name.

Laravel 5.2https://laravel.com/docs/5.2/routing#route-model-binding

Page 70: MidwestPHP 2016 - Adventures in Laravel 5

API Rate LimitingLimit the amount of times a user can access your API with the Throttle middleware.

Laravel 5.2https://laracasts.com/series/whats-new-in-laravel-5-2/episodes/2

Will return HTTP Code 429 (Too many requests)

Page 71: MidwestPHP 2016 - Adventures in Laravel 5

Auth & Password ResetsNew artisan command make:auth generates scaffolding for you.

Laravel 5.2https://laracasts.com/series/whats-new-in-laravel-5-2/episodes/3

Page 72: MidwestPHP 2016 - Adventures in Laravel 5

Validating Arrays

Laravel 5.2https://laracasts.com/series/whats-new-in-laravel-5-2/episodes/4

Page 73: MidwestPHP 2016 - Adventures in Laravel 5

Authentication Drivers

Laravel 5.2

Easily have different authentication drivers for multiple authenticatable models or user tables.

Useful to allow users from an admins table be able to log in as well as users from a users table.

Page 74: MidwestPHP 2016 - Adventures in Laravel 5

Functional Testing Is Easy!

Page 75: MidwestPHP 2016 - Adventures in Laravel 5

Upgrade from 4.2 to 5.xFresh install Laravel 5

Update Dependencies /Packages

Namespace (somewhat optional for now)

Migrate environment variables

Move routes to app/Http/routes.php

Move controllers to app/Http/Controllers (add this to classmap)

Copy route bindings to boot() in app/Providers/RouteServiceProvider.php

Add route facade to RouteServiceProvider.php to continue using Route facade

CSRF is now global. Use middleware to disable if needed

Move models to app/Models. Add app/Models to classmap in composer.json

Update your user auth to use Laravel 5’s auth system

Move commands to app/Console/Commands. Add app/Console/Commands to classmap in composer.json

Move migrations to database/migrations, Database Seeds to database/seeds

… and more!

Page 76: MidwestPHP 2016 - Adventures in Laravel 5

Upgrade from 4.2 to 5.x

https://la

ravel.com

/docs/5.1

/upgrade

Fresh install Laravel 5

Update Dependencies /Packages

Namespace (somewhat optional for now)

Migrate environment variables

Move routes to app/Http/routes.php

Move controllers to app/Http/Controllers (add this to classmap)

Copy route bindings to boot() in app/Providers/RouteServiceProvider.php

Add route facade to RouteServiceProvider.php to continue using Route facade

CSRF is now global. Use middleware to disable if needed

Move models to app/Models. Add app/Models to classmap in composer.json

Update your user auth to use Laravel 5’s auth system

Move commands to app/Console/Commands. Add app/Console/Commands to classmap in composer.json

Move migrations to database/migrations, Database Seeds to database/seeds

… and more!

Page 77: MidwestPHP 2016 - Adventures in Laravel 5

Quick note on versions

5.1 LTS bug fixes for 2 years, security fixes for 3 years

Non LTS: bug fixes for 6 months, security fixes for 1 year

5.1 is currently the only LTS version

Page 78: MidwestPHP 2016 - Adventures in Laravel 5

Which version should you use?

5.2 for my own projects

5.1 for client projects

Page 79: MidwestPHP 2016 - Adventures in Laravel 5

Homestead“Laravel Homestead is an official, pre-packaged

Vagrant "box" that provides you a wonderful development environment without requiring you

to install PHP, HHVM, a web server, and any other server software on your local machine. No more worrying about messing up your operating

system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-

create the box in minutes!”

Page 80: MidwestPHP 2016 - Adventures in Laravel 5

What’s in the box:• Ubuntu 14.04• PHP 7.0• HHVM• Nginx• MySQL• Postgres• Redis

• NodeJS• Bower• Grunt• Gulp• Beanstalkd• Memcached• Laravel Envoy

Fabric + HipChat Extension + more!

Page 81: MidwestPHP 2016 - Adventures in Laravel 5

Getting Homestead

Install the box:vagrant box add laravel/homestead

Page 82: MidwestPHP 2016 - Adventures in Laravel 5

Getting Homestead

If you have PHP installed locally:composer global require "laravel/homestead=~2.0"

Make sure to place the ~/.composer/vendor/bin directory in your PATH so the homestead executable is found when you run the

homestead command in your terminal.

Page 83: MidwestPHP 2016 - Adventures in Laravel 5

Homestead 2.x & 3.x

Significant change over previous 1.x versions

Uses homestead from your home folder

Less vagrant stuff in your projects (if you don’t like that sort of thing)

3.x will give you PHP 7

Page 84: MidwestPHP 2016 - Adventures in Laravel 5

How I use Homestead

Page 85: MidwestPHP 2016 - Adventures in Laravel 5

Install Homestead

http://laravel.com/docs/5.1/homestead#per-project-installation

Page 86: MidwestPHP 2016 - Adventures in Laravel 5

Go forth and develop!

Page 87: MidwestPHP 2016 - Adventures in Laravel 5

Feedback!

https://joind.in/talk/f8132

Joe FergusonTwitter: @JoePFergusonEmail: [email protected]: joepferguson

Contact Info: