Introduction to Laravel

(for WordPress developers)

Slides: bit.ly/2CseAwi

About Me

  • I live in Austin with my wife Emily and our cat Banana
     
  • I'm a web developer at ReviewPush (mainly using Laravel)
  • I previously worked primarily with WordPress - theme & plugin development
     
  • I enjoy travel, hiking, board games, good food, coffee, and craft beer
     
  • Pretty much everywhere as @dabernathy89

Shameless Plug

Longhorn PHP Conference

Austin, TX

April 19th - 21st, 2018

longhornphp.com

Talk Overview

  • Framework vs content management system
  • Comparison of concepts & architecture
  • Sample Laravel application

Framework

  • A collection of utilities, usually unified by a defined architecture
  • Comes with few to zero user-facing features
  • Various methods of extension - packages & service providers in Laravel
  • Virtually unlimited flexibility
  • Built for developers only

CMS

  • A complete application, ready to be used for managing content
  • Often built on top of a framework (sometimes part of the CMS, as with WordPress or Joomla)
  • Various methods of extension - themes & plugins in WordPress
  • Limited flexibility
  • Built for developers and/or end users

Framework

CMS

PHP Examples

Laravel Overview

  • Created by Taylor Otwell in 2011
  • Currently on version 5.5
  • MVC-ish style architecture
  • Contains a variety of features that can be used to build any kind of web application
  • Additional libraries & tools exist in the Laravel ecosystem that aren't bundled with the framework

Laravel

  • Simple apps tend to follow Model-View-Controller
  • No strict architecture enforced
  • Service Container assists with extending framework
  • Allows for event-driven code

WordPress

  • Event-driven: actions/filters are used both within core and to extend core
  • See Tom McFarlin's post on MVC vs event-driven pattern

Architecture

Laravel

  • Simple routing system maps URLs to corresponding logic (controller)
  • After Laravel maps a URL to a controller, everything is up to the developer

WordPress

Lifecycle

Laravel

  • System for mapping Events to Listeners
  • Events hold data; listeners act on Events
  • Events can be `dispatched` from anywhere
  • Events can easily be queued
  • No equivalent of WordPress filters
  • Available but not required

WordPress

  • Hooks: actions and filters
  • Actions trigger custom code and pass data
  • Filters trigger custom code, pass data, and receive data back
  • Synchronous
  • Essential to WordPress development

Events vs Hooks

Laravel

  • Entities typically represented by Eloquent Models
  • Columns / properties entirely up to developer
  • Models can have various relationships with other models
  • Eloquent lets you easily query data, including related entities, and filter/paginate

WordPress

  • Many entities are built-in to WordPress (users, posts, pages, menu items, images, etc)
  • Custom entities tend to be Custom Post Types
  • WP Query is used to easily query data and filter/paginate

Eloquent vs WP Query

Laravel

  • Tool for building and processing simple to complex SQL queries
  • Often used in conjunction with or instead of Eloquent
  • Eloquent extends the Query Builder

WordPress

  • Relatively simple class for running queries directly against database
  • Developers encouraged to use WP Query instead when possible

Query Builder vs WPDB

Laravel

  • Custom templating language similar to Twig, Mustache
  • Data is generally fetched in controller and passed to template
  • Developer determines which view (template) to load
  • Highly modular
  • Only has access to data passed to it
  • Structured outside --> in

WordPress

  • Plain PHP
  • Data is generally fetched inside the template ("The Loop")
  • WordPress determines which template will be loaded; can be altered via hooks
  • Can be modular
  • Global scope
  • Structured top --> bottom

Blade vs Template System

Laravel

WordPress

Other Similar Features

Cache
Artisan Console
User Authentication
Gates / Policies
Mail
Task Scheduling
Localization
Transients API / wp_cache
WP-CLI
User Authentication
Roles & Permissions
wp_mail
WP Cron
I18n / l10n

Some Unique Laravel Features:

  • Middleware
  • Collections
  • Validation
  • Broadcasting (websockets)
  • Queues
  • Filesystem
  • Migrations / Seeding

Some Unique WordPress Features:

  • HTTP API
  • Metadata / Options
  • Settings API
  • Menu System
  • Media Library
  • Widgets
  • Various other CMS features

Sample Application: TodoMVC Back End

The TodoMVC project is a specification for a simple front-end application which can be written using various different JavaScript frameworks.

 

We're going to look at how we could use Laravel to handle persistence of TodoMVC data.

 

The back-end is simple: it needs to be able to create, read, update, and delete Tasks from the database.

Made with Slides.com