eugene php june 2015 - let's talk laravel

27

Upload: anaxamaxan

Post on 10-Aug-2015

178 views

Category:

Internet


0 download

TRANSCRIPT

Let's Talk Laravel

EUG PHP MeetupJune 17th, 2015Presented by Max SchwanekampGraciously hosted by IDX Broker Eugene idxbroker.com

Who is this guy?Max Schwanekamp

Lead Developer, CE Learning SystemsEUG PHP OrganizerTwitter: @[email protected]

Instead, think of yourself as a software writer. Good

code is about clarity.— DHH (David Heinemeier Hansson, creator of Ruby on

Rails)

• What is Laravel? Who's responsible for this madness?

• Who should use it? Who shouldn't?

• Why?

• Some tasty bits

• Can I have fries that?

• Learn more, get help and have fun

What is this thing?• Application framework for PHP 5.5+

• Currently the most popular PHP app framework on Github

• Approaching 5 Million composer installs (packagist)

What is this thing?• A set of components so you can stop reinventing wheels

• A set of guidelines and best practices to make creating your apps simpler and more enjoyable

• A community of friendly, helpful developers worldwide

Who's behind the curtain?

• Created by Taylor Otwell, formerly lead developer for UserScape, now works on Laravel full time.

• Numerous community contributors of course - GrahamCampbell esp prominent

• Jeffrey Way is the Official Laravel Cheerleader (his words, podcast)

Who is Laravel for?• Business application developers

• Mobile developers needing a robust backend with minimal fuss

• Devs looking for a modern, standards-friendly approach to PHP

• Any PHP dev who wants a balance of tools along with flexibility & control

Who it's not for:• If you're only using PHP for Wordpress or other CMS work,

maybe you shouldn't bother with this.

• BUT, maybe you want to add some custom component that your CMS makes difficult.

PHP Frameworks

Ordered "heavy" to "light"; that doesn't necessarily correlate to performance

• Zend & Symfony

• Laravel• Yii, Kohana, CodeIgniter 2, Cake 2

• PHPixie

• Phalcon - good alternative

• Slim

• Lumen

Speaking of Symfony• Laravel is closely tied with Symfony, using a number of core

components, such as Routing, Translation, HttpFoundation, etc.

More third-party components built in

• Flysystem, Swiftmailer, DotEnv, Psysh, Carbon, etc etc

Composer and Packagist make it all possible

Why should I care?

Laravel vs straight PHP• Helper classes and functions: Laravel's syntactic sugar

makes PHP quite bearable, even enjoyable.

• Takes advantage of newer PHP language features, e.g. late static binding, SPL classes, and OMG so many closures.

• Laravel offers so much helpfulness. It's kind of insane to reinvent that many wheels.

So. Much. Helpfulness:Composer Rules It All Routing CachingEvent Handling Service Container Dependency InjectionEloquent ORM Migrations Schema BuilderArtisan commands Queue Workers Task schedulingPSR-7 Middleware - with parameters!Authentication Socialite Elixir wrapper for GulpTranslation and Localization Cloud Filesystem StorageValidation Collections Helpers helpers helpers

Example: Routing<?php //Http/routes.php

Route::pattern('id', '[0-9]+');Route::pattern('trainingRole', '(trainee|faculty|supervisor)');

/**************** API ****************/Route::group(['prefix' => 'api'], function () {

Route::get('clients', [ 'as'=>'api.clientsIndex', 'uses' => 'Api\ClientsController@getIndex' ]); Route::get('clients/{id}', [ 'as'=>'api.clientsShow', 'uses' => 'Api\ClientsController@getShow' ]); Route::post('clients', [ 'as'=>'api.clientsCreate', 'uses' => 'Api\ClientsController@postCreate' ]); Route::put('clients/{id?}', ['as'=>'api.clientsEdit', 'uses' => 'Api\ClientsController@putEdit']); Route::delete('clients/{id?}', ['as'=>'api.clientsDelete', 'uses' => 'Api\ClientsController@deleteIndex']); // etc...

});

Migration examplephp artisan make:migration create_users_tableCreated Migration: 2015_06_18_003309_create_users_table

<?php

use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

public function up() { Schema::create('notifications', function(Blueprint $table) { $table->increments('id'); $table->string('email')->index(); $table->string('name'); $table->timestamps(); }); } //... snip}

php artisan migrate

Collections: My favorite feature of Laravel<?phpuse App\Models\Goal; // ...SNIPclass GoalRepository implements GoalRepositoryContract, OrderableContract, ValidatesContract{ use CommonTrait, OrderingTrait, ValidatesTrait, AttachablesTrait;

public function getAllObjectivesWithComments(Goal $id) { $goal->load(['objectives','objectives.comments']); return $goal->objectives->filter(function($objective){ return $objective->comments->count() > 0; }); }}

Sometimes it's the little things• Get an array value, else return a default value:

array_get($myArray,'key','defaultValue');

• Remove an item from deeply-nested array: array_forget($myDeepArray, 'foo.bar.baz'); php$object = $repo->findObject($id);return response()->json($object, HTTP_SUCCESS);// recursively serializes $object into json

TinkerPowered by Psysh, boots your app into a REPL

$ tinkerPsy Shell v0.4.4 (PHP 5.6.4-1+deb.sury.org~trusty+1 — cli) by Justin Hileman>>> $goalRepo = app('GoalRepository');=> <Sa\Repositories\Eloquent\GoalRepository #00000000339520f4000000005f8515d3>>>> $goalRepo->find(193);=> <Sa\Models\Goal #00000000339520c5000000005f8515d3> { id: 193, contentsection_id: 85, name: "Site Supervisor Preparation", description: "yada yada yada yada", created_at: "2012-10-27 09:39:48", updated_at: "2013-01-14 04:04:47", ordering: 2, role_id: 4, deleted_at: null }

Officialish ServicesForge

• Server management for Digital Ocean, VPS and (soon) AWS

• Laravel-oriented but can be for any server

Server Pilot is a decent alternative

Officialish ServicesEnvoyer

"Zero Downtime" deployments for PHP apps

Dploy.io is a decent hosted alternative

Fabric fabfile.org is an awesome alternative

When is Laravel not the right answer?• API only? Look at Lumen or Slim• Want more CMS features, but still want to use Composer

packages? Take a look at Drupal 8.• Maybe PHP is not the right answer, or is not the whole

answer.

Laravel LTSLaravel 5.1, released last week, is the first LTS release.Laravel 5.1 is the first release of Laravel to receive long term support. Laravel 5.1 will receive bug fixes for 2 years and security fixes for 3 years. This support window is the largest ever provided for Laravel and provides stability and peace of mind for larger, enterprise clients and customers.

People-ish HelpfulnessLaracasts

If your dev team works in PHP, you must get them Laracasts accounts!Start Here: Laravel 5 FundamentalsThen go here: SOLID Principles in PHP

Then go to the Laracasts Discuss forum. Follow interesting threads.

People-ish Helpfulness• Laravel-News.com

• Leanpub is great but with Laravel 5 just released, most of the Laravel books there are somewhat out of date alreadyEasy Laravel 5Easy Ecommerce with Laravel 5

• Laravel.io

• Laravel Slack Channel

Thanks for listening!EUG PHP Meetup - http://www.meetup.com/eugphp/