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

Intro to Laravel 5

Ben Edmunds
September 30, 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.

Ben Edmunds

September 30, 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. Routes Create routes at: routes/web.php routes/api.php Web = web middleware

    API = api middleware App\Providers\RouteServiceProvider to add new route files
  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 App\Providers\RouteServiceProvider protected function mapApiRoutes() { Route::group([ 'middleware' => ['api',

    'auth:api'], 'namespace' => $this->namespace, 'prefix' => 'api', ], function ($router) { require base_path('routes/api.php'); });
  10. API App\Providers\RouteServiceProvider protected function mapApiRoutes() { Route::group([ 'middleware' => ['api',

    'auth:api'], 'namespace' => $this->namespace, 'prefix' => 'api', ], function ($router) { require base_path('routes/api.php'); });
  11. API /tests/api/UserTest.php class UserTest extends TestCase { public function testGetUser()

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

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

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

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

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

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