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

Intro to Laravel 5

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.

Ben Edmunds

January 15, 2016
Tweet

More Decks by Ben Edmunds

Other Decks in Technology

Transcript

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

    User::find($id); return view(‘home') ->with('user', $user); });
  2. Why use it? Facades = Quick Start $object = new

    class; $object->set('key', 'value'); Class::set('key', 'value');
  3. Why use it? PAAS to quickly deploy PHP apps to

    the “cloud” Digital Ocean Linode AWS
  4. Beyond MVC Design Easy to get started Consistent/Stable Fast Excellent

    Docs Threw out previous framework ideas and started over Comparison
  5. 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. Routes Register multiple verbs for a single route: Router::match([‘get’, ‘post’],

    ‘/’, function(){ //this will run for both GET and POST });
  7. Testing /tests/ExampleTest.php Test it! Test it good! class ExampleTest extends

    TestCase { function testBasicExample() { $this->visit('/') ->see(‘Boom!');
  8. 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
  9. API /tests/api/UserTest.php class UserTest extends TestCase { public function testGetUser()

    { $this->get(‘/user/1') ->seeJson([ ‘success‘ => true ]);
  10. Middleware class AjaxMiddleware implements Middleware { public function handle($request, Closure

    $next) { if (!$request->ajax()) return response('Unauthorized.', 401); return $next($request); }
  11. Query Builder $id = DB::table('statuses') ->insertGetId([ ‘user_id' => 1, 'email'

    => ‘[email protected]', 'text' => ‘Vegas Baby! #zendcon’, ]); Insert & Get ID
  12. 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
  13. 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
  14. 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!
  15. Views Sending data to a view with magic methods Route::get('/',

    function(){ $view = view('home.index'); $view->email = ‘[email protected]’; return $view;
  16. Form Requests Designed just for form handling/validation class CreateStatusRequest extends

    FormRequest { public function rules() { return [ ‘email’ => ‘required|email', ‘text' => ‘required', ]; }
  17. Facades class HomeController extends Controller { public function __construct(Request $request){

    $value = $request->session()->get('key'); } Controller Injection
  18. 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