Introducing Laravel!

Ashok Modi - DrupalCampLA 2017

About presenter

  • Engineer - CARD.com
  • Drupal - 11 years
  • PHP - 15+ years
  • Go to Drupal meetups
    to talk about anything
    but Drupal

What is Laravel?

  • PHP Framework for web apps
  • Like Ruby on Rails?
    • Imagine that in PHP

Why Laravel?

  • Lots of great features/add-ons
    • Authentication/Authorization
    • E-commerce
    • artisan CLI
      • Similar to Symfony console
  • Large community
  • Very well documented
    • Laracasts
    • Documentation Website

Documented local dev

  • Homestead if you use virtualbox
  • Valet if you want to mostly DIY
  • Forge to help you set up in the cloud

Easy to understand

  • Simple Model-View-Controller (MVC) system
  • Few(er) folders
    • App stores your application code
    • Routes stores your routes
    • Database stores your schema
    • Config stores your site configuration
    • Public stores your flat files
    • Resources stores your theme files

Demo

  • All day trainings site
  • Register to add trainings
  • Trainings Model
    • Title
    • Date
    • Location
    • Info
    • Link
  • https://github.com/btmash/laravel-training

Install homestead

Download Laravel

  • composer create-project --prefer-dist laravel/laravel blog

  • php artisan migrate:install

Install Users

  • php artisan make:auth
  • php artisan migrate

Use Artisan (CLI) to create the model

  • php artisan make:model -mc Training

Update schema

  • database/migrations/*_create_trainings_table.php
  • Add our table fields
  • php artisan migrate
  • Similarly, create new migrate files to do migrations
    • ​php artisan make:migration <name_of_migration>

Create our model

  • app/Training.php
  • $fillable_fields are user input fields
  • public functions would be attributes / relationships you wish to establish
  • public function authors() {
      return $this->belongsTo('User', 'user_id');
    }
  • public function trainings() {
      return $this->hasMany('Training');
    }

None of this seems to do anything...

Create our route

  • routes/web.php
    • Similar for api.php, cli.php, etc
  • Add middleware (like auth/permissions) to limit route access
  • Group routes to satisfy criteria
  • Separate between GET and POST requests
  • Now you can link to them in templates

Create controller

  • Controllers are where routes point
  • Gather info from wherever necessary (like models)
  • Show them using the view
    • return view('template-minus-php', $args);
    • return ['status' => 'yay', 'content' => 'json-here']
    • Hence M-V-C!
  • app/Http/Controllers/TrainingController.php

Create view

  • Uses Blade templating engine by default
    • Relatively simple to understand
  • Can switch to use Twig if you want!
  • @extends('layout.layoutName')
  • @section('sectionName')
    • @foreach($var_array as $var)
    • @endForEach
  • @endSection

Features not covered

  • Validation
    • Validate your models easily
  • Seeding the database
    • No need for db dumps!
  • Testing
  • Docs go over everything

How does this relate to Drupal?

Write entities specific to your needs

  • Everything does not need to be a node
  • Should not be a node
  • Create new entities using Drupal Console
  • Learn how to model your content
    • Will look great in your APIs as well :)

Not everything needs to be Drupal

  • Not everything needs annotations
  • Not everything needs to be so abstracted
  • Not everything needs 100+ modules
    to maintain