Slide 1

Slide 1 text

getting started with laravel 4

Slide 2

Slide 2 text

about me ! ›❯ i live in cape town ›❯ i work for http://n.joepublic.co.za ›❯ i love programming ›❯ i have written a book on mvc framework development ›❯ i am finishing a book on laravel applications

Slide 3

Slide 3 text

the disclaimers ! ›❯ much of this is my opinion ›❯ i have a vested interest in the success of laravel

Slide 4

Slide 4 text

laravel ! ›❯ laravel is an mvc framework ›❯ laravel installs with composer ›❯ laravel uses 24 community packages (11 from symfony)

Slide 5

Slide 5 text

artisan ! ›❯ artisan is a command-line utility ›❯ artisan ships with laravel 4 ›❯ artisan automates complicated and laborious tasks

Slide 6

Slide 6 text

// terminal ! ›❯ php artisan env Current application environment: production ! ›❯ php artisan down Application is now in maintenance mode.

Slide 7

Slide 7 text

// continued... ! ›❯ php artisan Laravel Framework version 4.1.24 ! Usage: [options] command [arguments] ...

Slide 8

Slide 8 text

routes ! ›❯ routes link a url to a function ›❯ routes are specified by request method

Slide 9

Slide 9 text

// app/routes.php ! Route::get("products", function() { return "show the products"; }); ! Route::post("product", function() { return "save the product"; });

Slide 10

Slide 10 text

// terminal ! ›❯ php artisan serve Laravel development server started on http://localhost:8000 ! ›❯ curl -X GET "http://localhost:8000/products" show the products

Slide 11

Slide 11 text

// app/routes.php ! Route::get("product/{id}", function($id) { return "show the product {$id}"; }); ! Route::get("category/{id?}", function($id = 1) { return "save the category {$id}"; });

Slide 12

Slide 12 text

// continued... ! Route::get("user/{id}", function($id) { return "show the user {$id}"; })->where("id", "[a-z]+");

Slide 13

Slide 13 text

// continued... ! Route::pattern("product_id", "[0-9]+"); ! Route::get("product/{product_id}", function($product_id) { return "show the product {$product_id}"; }); ! Route::get("product/{product_id}/images", function($product_id) { return "show the product images for {$product_id}"; });

Slide 14

Slide 14 text

// terminal ! ›❯ curl -X GET "http://localhost:8000/bridge/gimly" run, fools ! ›❯ curl -X GET "http://localhost:8000/bridge/balrog" YOU SHALL NOT PASS

Slide 15

Slide 15 text

views ! ›❯ views are where php meets html ›❯ views can be plain php ›❯ blade is a template engine

Slide 16

Slide 16 text

// app/routes.php ! Route::get("/", function() { return View::make("hello"); });

Slide 17

Slide 17 text

// app/views/hello.php ! Laravel PHP Framework ...

Slide 18

Slide 18 text

// app/routes.php ! Route::get("products", function() { return View::make("products/index"); });

Slide 19

Slide 19 text

// app/views/products/index.php !
show the products

Slide 20

Slide 20 text

// app/routes.php ! Route::get("products", function() { $products = [ ["the red one", 12.99], ["the blue one", 13.99], ["the black one", 11.99] ]; ! return View::make("products/index", compact("products")); });

Slide 21

Slide 21 text

// app/views/products/index.php !

Slide 22

Slide 22 text

// app/views/products/index.blade.php !
@foreach ($products as $product)
{{ $product[0] }} {{ $product[1] }}
@endforeach

Slide 23

Slide 23 text

// continued... ! @extends("layout") ... ! @section("content") ... @stop

Slide 24

Slide 24 text

// app/views/layout.blade.php ! @yield("content", "default") ... ! @include("includes/header") ...

Slide 25

Slide 25 text

controllers ! ›❯ controllers connect data to views ›❯ controllers redirect requests ›❯ controllers shouldn't be doing much more than that

Slide 26

Slide 26 text

// terminal ! ›❯ php artisan controller:make UsersController Controller created successfully! ! ›❯ php artisan controller:make PostsController Controller created successfully! ! ›❯ php artisan controller:make TagsController Controller created successfully!

Slide 27

Slide 27 text

// app/controllers/UsersController.php ! class UsersController extends \BaseController { ... }

Slide 28

Slide 28 text

// app/routes.php ! Route::get("users", [ "as" => "users.index", "uses" => "UsersController@index" ]);

Slide 29

Slide 29 text

migrations ! ›❯ migrations are scripted database changes ›❯ migrations have a special dsl ›❯ migrations need to be run

Slide 30

Slide 30 text

// terminal ! ›❯ php artisan migrate:make create_posts_table Created Migration: 2014_04_01_025625_create_posts_table Generating optimized class loader ! ›❯ php artisan migrate:make create_users_table Created Migration: 2014_04_01_025631_create_users_table Generating optimized class loader

Slide 31

Slide 31 text

// continued... ! ›❯ php artisan migrate:make create_tags_table Created Migration: 2014_04_01_025656_create_tags_table Generating optimized class loader ! ›❯ php artisan migrate:make create_posts_tags_table Created Migration: 2014_04_01_025701_create_posts_tags_table Generating optimized class loader

Slide 32

Slide 32 text

// app/database/migrations/[timestamp]_create_users_table.php ! use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; ! class CreatePostsTable extends Migration { ... }

Slide 33

Slide 33 text

// continued... ! Schema::create("users", function(Blueprint $table) { $table->increments("id"); $table->string("username"); $table->string("password"); $table->string("email"); $table->timestamps(); });

Slide 34

Slide 34 text

// terminal ! ›❯ php artisan migrate Migration table created successfully. Migrated: 2014_04_01_025625_create_posts_table Migrated: 2014_04_01_025631_create_users_table Migrated: 2014_04_01_025656_create_tags_table Migrated: 2014_04_01_025701_create_users_tags_table

Slide 35

Slide 35 text

seeders ! ›❯ seeders are scripted database populators ›❯ seeders can insert as much or as little data as required ›❯ seeders need to be run

Slide 36

Slide 36 text

// app/database/seeds/DatabaseSeeder.php ! class DatabaseSeeder extends Seeder { ... }

Slide 37

Slide 37 text

// continued... ! public function run() { $this->call("UsersTableSeeder"); }

Slide 38

Slide 38 text

// app/database/seeds/UsersTableSeeder.php ! DB::table("users")->insert([ [ "username" => "chrispitt", "password" => Hash::make("password"), "email" => "[email protected]", "created_at" => time(), "updated_at" => time() ] ]);

Slide 39

Slide 39 text

// terminal ! ›❯ php artisan db:seed Seeded: UsersTableSeeder

Slide 40

Slide 40 text

query builder ! ›❯ query builder builds sql queries ›❯ query builder has its own dsl

Slide 41

Slide 41 text

eloquent ! ›❯ eloquent is a programmatic wrapper for database tables ›❯ eloquent is best for single instances

Slide 42

Slide 42 text

authentication ! ›❯ authentication is about blocking the wrong users and allowing the right ones ›❯ authentication needs a special eloquent model ›❯ alternatives exist

Slide 43

Slide 43 text

thanks ! ›❯ follow http://twitter.com/followchrisp ›❯ learn http://tutorials.io ______ < moo! > ------ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||