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

Why Laravel?

Why Laravel?

Introduction to Laravel

Jonathan Goode

November 04, 2016
Tweet

More Decks by Jonathan Goode

Other Decks in Technology

Transcript

  1. 2 . 1 HISTORY BEHIND THE VISION , a .NET

    developer in Arkansas (USA), was using an early version of CodeIgniter when the idea for Laravel came to him. “I couldn't add all the features I wanted to”, he says, “without mangling the internal code of the framework”. He wanted something leaner, simpler, and more flexible. Taylor Otwell
  2. 2 . 2 HISTORY BEHIND THE VISION Those desires, coupled

    with Taylor's .NET background, spawned the framework that would become Laravel. He used the ideas of the .NET infrastructure which Microso had built and spent hundreds of millions of dollars of research on. With Laravel, Taylor sought to create a framework that would be known for its simplicity. He added to that simplicity by including an expressive syntax, clear structure, and incredibly thorough documentation. With that, Laravel was born.
  3. 3 . 1 THE EVOLUTION OF LARAVEL Taylor started with

    a simple routing layer and a really simple controller-type interface (MVC) v1 and v2 were released in June 2011 and September 2011 respectively, just months apart In February 2012, Laravel 3 was released just over a year later and this is when Laravel's user base and popularity really began to grow.
  4. 3 . 2 THE EVOLUTION OF LARAVEL In May 2013,

    Laravel 4 was released as a complete rewrite of the framework and incorporated a package manager called . Composer is an application-level package manager for PHP that allowed people to collaborate instead of compete. Before Composer, there was no way to take two separate packages and use different pieces of those packages together to create a single solution. Laravel is built on top of several packages most notably . Composer Symfony
  5. 3 . 3 LARAVEL TODAY Laravel now stands at version

    5.3 with 5.4 currently in development Support for PHP 7 has recently been added to Laravel 4.2.20 and for the first version ever, long term support has been guaranteed for 5.1
  6. 4 . 1 ARCHITECTURE Model Everything database related - Eloquent

    ORM (Object Relational Mapper) View HTML structure - Blade templating engine Controller Processing requests and generating output + Facades, Dependency Injection, Repositories, etc...
  7. 4 . 2 FIRSTLY, WHY OBJECT ORIENTED PHP? Logic is

    split into classes where each class has specific attributes and behaviours. This leads to code that is more maintainable, more predictable and simpler to test.
  8. 5 . 1 ROUTES Routes (/routes) hold the application URLs

    web.php for web routes api.php for stateless requests console.php for Artisan commands // Visiting '/' will return 'Hello World!' Route::get('/', function() { return 'Hello World!'; });
  9. 5 . 2 Route::post('/tasks/add', 'TasksController@store'); Handles posting for data when

    creating a form No need to check for isset($_POST['input'])
  10. 5 . 3 6 . 1 CONTROLLERS Handle the logic

    required for processing requests namespace App\Http\Controllers\Admin; class TasksController extends Controller { public function view($id) { return "This is task " . $id; } }
  11. 6 . 2 ENHANCED CONTROLLER namespace App\Http\Controllers\Admin; class TasksController extends

    Controller { public function view($id) { // Finds the task with 'id' = $id in the tasks table $task = Task::find($id); // Returns a view with the task data return View::make('tasks.view')­>with(compact('task')); } }
  12. 7 . 1 MODELS Classes that are used to access

    the database A Task model would be tied to the tasks table in MySQL namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Task extends Model { use SoftDeletes; protected $fillable = ['title', 'done',]; protected $dates = ['deleted_at']; }
  13. 7 . 2 QUERIES // Retrieves the tasks that have

    a high priority Task::where('priority', '=', 'high')­>get(); Task::wherePriority('high')­>get(); // *Magic*! // Retrieves all the tasks in the database Task::all(); // Retrieves a paginated view for tasks, 20 tasks per page Task::paginate(20); // Inserts a new task into the database Task::create(['description' => 'Buy milk']); // (Soft) deletes the task with an id of 5 Task::find(5)­>delete(); // Permanently deletes all soft deleted tasks Task::onlyTrashed()­>forceDelete();
  14. 8 . 1 VIEWS Blade is a simple, yet powerful

    templating engine provided with Laravel // This view uses the master layout @extends('layouts.master') @section('content') <p>This is my body content.</p> @stop @section('sidebar') <p>This is appended to the master sidebar.</p> @stop
  15. 8 . 2 @foreach($tasks as $task) <p>{{ $task­>title }}</p> @endforeach

    = <?php foreach ($tasks as $task) { <p><?php echo $task­>title ?></p> <?php endforeach ?>
  16. 8 . 3 <p>Tasks:</p> <table> <thead> <tr> <th>ID</th> <th>Title</th> </tr>

    </thead> <tbody> @foreach($tasks as $task) <tr> <td>{{ $task­>id }}</td> <td>{{ $task­>title }}</td> </tr> @endforeach </tbody> </table>
  17. 8 . 4 9 . 1 ARTISAN Artisan is the

    command-line interface included with Laravel It provides a number of helpful commands that can assist you while you build your application $ php artisan list $ php artisan help migrate
  18. 9 . 2 DATA MIGRATION AND SEEDING $ php artisan

    make:migration create_users_table Migration created successfully! $ php artisan migrate ­­seed Migrated: 2016_01_12_000000_create_users_table Migrated: 2016_01_12_100000_create_password_resets_table Migrated: 2016_01_13_162500_create_projects_table Migrated: 2016_01_13_162508_create_servers_table
  19. 10 . 1 KEY CHANGES IN LARAVEL 5 DIRECTORY STRUCTURE

    Laravel 5 implements the autoloading standard which means all your classes are namespaced The default namespace for your web application is app You can change this using php artisan app:name <your‐app‐name> assets, views, and lang now live in a resources folder config, storage, database, and tests directories have been moved to the root of the project bootstrap, public, and vendor retain their place PSR-4
  20. 10 . 2 KEY CHANGES IN LARAVEL 5 ENVIRONMENT DETECTION

    Instead of all the complex nested configuration directories, you have a .env file at the root of your project to take care of the application environment and all the environment variables. It is useful to create a .env.example file to provide a template for other team members to follow (dotEnv)
  21. 10 . 3 KEY CHANGES IN LARAVEL 5 ROUTE MIDDLEWARE

    Middleware can be used to add extra layers to your HTTP routes. If you want code to execute before every route or before specific routes in your application, then such a piece of code is a good fit for a middleware class. For example, let's say you want to block out a certain range of IPs from your application to make your application unavailable in that region. In such a case, you will need to check the client IP before every request and allow/disallow them entry to your application. $ php artisan make:middleware "RegionMiddleware"
  22. 10 . 4 KEY CHANGES IN LARAVEL 5 METHOD INJECTION

    Until version 4.2, you had to request the Inversion of Control (IoC) container to provide a class instance or create it in your controller's constructor to make it accessible under the class scope. Now, you can declare the type hinted class instance in the controller method's signature and the IoC container will take care of it, even if there are multiple parameters in your controller function's signature.
  23. 10 . 5 EXAMPLE OF METHOD INJECTION // TaskController.php namespace

    App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Http\Requests\Admin\TaskUpdateRequest; use App\Models\Task; class TaskController extends Controller { public function store(TaskUpdateRequest $request) { $task = Task::create($request­>except('_token')); return redirect(route('admin.task.index'))­>with('success', trans('general.created', ['what' => 'Task'])); } } /**/ class TaskUpdateRequest extends FormRequest { public function rules() { return ['title' => 'required|min:3|unique:tasks,title',]; } }
  24. 10 . 6 KEY CHANGES IN LARAVEL 5 CONTRACTS Contracts

    are actually interface classes in disguise. Interfaces are a tried and tested method for removing class dependency and developing loosely coupled so ware components. . Most of the major core components make use of these contracts to keep the framework loosely coupled. Laravel buys into the same philosophy ⇲
  25. 10 . 7 KEY CHANGES IN LARAVEL 5 AUTHENTICATION Authentication

    is part of almost all the web applications you develop, and a lot of time is spent writing the authentication boilerplate. This is not the case any more with Laravel 5. The database migrations, models, controllers, and views just need to be configured to make it all work. Laravel 5 has a Registrar service, which is a crucial part of this out of the box, ready to use authentication system.
  26. 10 . 8 OTHER KEY CHANGES IN LARAVEL 5 Queue

    and Task Scheduling Crons taken care of Multiple File Systems Local or remote (cloud based) Route Caching Events Commands
  27. 11 . 1 LARAVEL ELIXIR provides a fluent, expressive interface

    to compiling and concatenating your assets. If you've ever been intimidated by learning Gulp or Grunt, fear no more. Elixir makes it a cinch to get started using Gulp to compile Sass and other assets. It can even be configured to run your tests Laravel Elixir (Assumes sass is located at resources/assets/sass, etc.)
  28. // gulpfile.js const elixir = require('laravel­elixir'); elixir.config.publicPath = 'public_html/assets'; require('laravel­elixir­vue­2');

    elixir(mix => { mix.sass('app.scss') .webpack('app.js') .copy('node_modules/bootstrap­sass/assets/fonts/bootstrap/', 'public_html/assets/fonts/bootstrap'); });
  29. 11 . 2 // Run all tasks and build your

    assets $ gulp // Run all tasks and minify all CSS and JavaScript $ gulp ­­production // Watch your assets for any changes ­ re­build when required $ gulp watch
  30. 11 . 3 12 LARAVEL STARTER ⇲ Created specifically to

    speed up development at UofA Improved version of Laravel 4.1 with backported enhancements from 4.2 This was due to the version of PHP (5.3.8) we previously had to use Has code specific for the way we have to work with web accounts Configured to use public_html rather than public LDAP integration complete with configuration for abdn.ac.uk Enhanced String and Array helpers UofA logos etc. Currently in the process of updating for Laravel 5.3
  31. 13 . 1 LARAVEL HAS MORE UP ITS SLEEVE STANDALONE

    SERVICES Provision and deploy unlimited PHP applications on DigitalOcean, Linode, and AWS A package that provides scaffolding for subscription billing, invoices, etc. A deployment service focused on the deployment side of applications and includes things like zero downtime deploys, health checks, cron job monitoring, and more Forge Spark Envoyer
  32. 13 . 2 LARAVEL HAS MORE UP ITS SLEEVE BAKED

    INTO LARAVEL 5 Event broadcasting, evolved. Bring the power of WebSockets to your application without the complexity. API authentication without the headache. Passport is an OAuth2 server that's ready in minutes. Driver based full-text search for Eloquent, complete with pagination and automatic indexing. Laravel Echo Laravel Passport Laravel Scout
  33. 13 . 3 LARAVEL HAS MORE UP ITS SLEEVE OTHER

    DEVELOPMENT TOOLS Powered by Vagrant, Homestead gets your entire team on the same page with the latest PHP, MySQL, Postgres, Redis, and more. Cachet is the best way to inform customers of downtime. This is your status page. If all you need is an API and lightning fast speed, try Lumen. It's Laravel smaller-light. Need a CMS that runs on Laravel and is built for developers and clients? Look no further. Homestead Cachet Lumen Statamic
  34. 14 MORE RESOURCES Over 900 videos covering all aspects of

    using Laravel https://laravel.com/ https://laracasts.com/