Intro to Laravel 5

22f21d5c22b930fd35dd98f25dedf6a4?s=47 Ben Edmunds
January 15, 2016

Intro to Laravel 5

Learn why this new framework has been taking the PHP world by storm.

Are you ashamed to admit you're a PHP developer? Have you been using the same old, boring PHP framework for years? Tired of your PHP applications turning into enormous beasts? Maybe you've heard about Laravel but haven't made the effort to dive into it? In this presentation, we'll walk through what makes Laravel an elegant, fun, and exciting framework to make PHP applications that you'll be proud of.

22f21d5c22b930fd35dd98f25dedf6a4?s=128

Ben Edmunds

January 15, 2016
Tweet

Transcript

  1. 7.

    Why use it? Expressive Syntax Route::get(‘user/{id}’, function($id) { $user =

    User::find($id); return view(‘home') ->with('user', $user); });
  2. 11.

    Why use it? Facades = Quick Start $object = new

    class; $object->set('key', 'value'); Class::set('key', 'value');
  3. 18.

    Why use it? PAAS to quickly deploy PHP apps to

    the “cloud” Digital Ocean Linode AWS
  4. 22.

    Beyond MVC Design Easy to get started Consistent/Stable Fast Excellent

    Docs Threw out previous framework ideas and started over Comparison
  5. 26.

    Structure app Commands Handlers Commands Events Http Controllers Middleware Requests

    Providers Services config resources views tests app Commands Console Events Handlers Commands Events Http Controllers Middleware Requests Providers Services bootstrap config database migrations seeds public package resources lang views storage cache logs meta sessions views work tests
  6. 27.
  7. 30.

    Routes Register multiple verbs for a single route: Router::match([‘get’, ‘post’],

    ‘/’, function(){ //this will run for both GET and POST });
  8. 32.
  9. 35.

    Testing /tests/ExampleTest.php Test it! Test it good! class ExampleTest extends

    TestCase { function testBasicExample() { $this->visit('/') ->see(‘Boom!');
  10. 36.

    Testing $ php artisan test Run it! PHPUnit 3.6.10 by

    Sebastian Bergmann. Configuration read from ... Time: 0 seconds, Memory: 6.25Mb OK (1 test, 1 assertion) Output
  11. 37.
  12. 41.

    API /tests/api/UserTest.php class UserTest extends TestCase { public function testGetUser()

    { $this->get(‘/user/1') ->seeJson([ ‘success‘ => true ]);
  13. 45.
  14. 52.

    Middleware class AjaxMiddleware implements Middleware { public function handle($request, Closure

    $next) { if (!$request->ajax()) return response('Unauthorized.', 401); return $next($request); }
  15. 54.
  16. 58.

    Query Builder $id = DB::table('statuses') ->insertGetId([ ‘user_id' => 1, 'email'

    => ‘example@gmail.com', 'text' => ‘Vegas Baby! #zendcon’, ]); Insert & Get ID
  17. 63.

    Eloquent ORM Relationships class Status extends Model { public function

    user() { return $this->belongsTo(‘App\User'); } } App\Status::find(1)->user->email; Retrieve Status Creator Email
  18. 65.

    Eloquent ORM Normal Usage $statuses = App\Status::all(); foreach ($statuses as

    $status) echo $status->user->email; SELECT * FROM "statuses" foreach result { SELECT * FROM "users" WHERE "id" = 1 } 100 statuses = 101 queries
  19. 68.

    Eloquent ORM $statuses = App\Status::with(‘user’)->get(); foreach ($statuses as $status) echo

    $status->user->email; 100 statuses = 2 queries SELECT * FROM "statuses" SELECT * FROM "users" WHERE "id" IN (1, 2, 3, 4, 5, ...) Eager Loading FTW!
  20. 70.
  21. 73.

    Views Sending data to a view with magic methods Route::get('/',

    function(){ $view = view('home.index'); $view->email = ‘email@gmail.com’; return $view;
  22. 78.

    Form Requests Designed just for form handling/validation class CreateStatusRequest extends

    FormRequest { public function rules() { return [ ‘email’ => ‘required|email', ‘text' => ‘required', ]; }
  23. 80.
  24. 83.

    Facades class HomeController extends Controller { public function __construct(Request $request){

    $value = $request->session()->get('key'); } Controller Injection
  25. 85.

    That’s a Wrap Utilizes the best PHP has to offer

    Well designed API with great docs Ready to scale architecture Go make cool things and have fun