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

Getting Started With Laravel 4

Getting Started With Laravel 4

Christopher Pitt

April 04, 2014
Tweet

More Decks by Christopher Pitt

Other Decks in Technology

Transcript

  1. 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
  2. the disclaimers ! ›❯ much of this is my opinion

    ›❯ i have a vested interest in the success of laravel
  3. laravel ! ›❯ laravel is an mvc framework ›❯ laravel

    installs with composer ›❯ laravel uses 24 community packages (11 from symfony)
  4. artisan ! ›❯ artisan is a command-line utility ›❯ artisan

    ships with laravel 4 ›❯ artisan automates complicated and laborious tasks
  5. // terminal ! ›❯ php artisan env Current application environment:

    production ! ›❯ php artisan down Application is now in maintenance mode.
  6. routes ! ›❯ routes link a url to a function

    ›❯ routes are specified by request method
  7. // app/routes.php ! Route::get("products", function() { return "show the products";

    }); ! Route::post("product", function() { return "save the product"; });
  8. // terminal ! ›❯ php artisan serve Laravel development server

    started on http://localhost:8000 ! ›❯ curl -X GET "http://localhost:8000/products" show the products
  9. // 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}"; });
  10. // 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}"; });
  11. // terminal ! ›❯ curl -X GET "http://localhost:8000/bridge/gimly" run, fools

    ! ›❯ curl -X GET "http://localhost:8000/bridge/balrog" YOU SHALL NOT PASS
  12. views ! ›❯ views are where php meets html ›❯

    views can be plain php ›❯ blade is a template engine
  13. // 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")); });
  14. // app/views/products/index.php ! <div class="products"> <?php foreach ($products as $product):

    ?> <div class="product"> <?php echo $product[0]; ?> <span class="price"><?php echo $product[1]; ?></span> </div> <?php endforeach; ?> </div>
  15. // app/views/products/index.blade.php ! <div class="products"> @foreach ($products as $product) <div

    class="product"> {{ $product[0] }} <span class="price">{{ $product[1] }}</span> </div> @endforeach </div>
  16. controllers ! ›❯ controllers connect data to views ›❯ controllers

    redirect requests ›❯ controllers shouldn't be doing much more than that
  17. // 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!
  18. migrations ! ›❯ migrations are scripted database changes ›❯ migrations

    have a special dsl ›❯ migrations need to be run
  19. // 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
  20. // 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
  21. // 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
  22. seeders ! ›❯ seeders are scripted database populators ›❯ seeders

    can insert as much or as little data as required ›❯ seeders need to be run
  23. // app/database/seeds/UsersTableSeeder.php ! DB::table("users")->insert([ [ "username" => "chrispitt", "password" =>

    Hash::make("password"), "email" => "[email protected]", "created_at" => time(), "updated_at" => time() ] ]);
  24. eloquent ! ›❯ eloquent is a programmatic wrapper for database

    tables ›❯ eloquent is best for single instances
  25. authentication ! ›❯ authentication is about blocking the wrong users

    and allowing the right ones ›❯ authentication needs a special eloquent model ›❯ alternatives exist
  26. thanks ! ›❯ follow http://twitter.com/followchrisp ›❯ learn http://tutorials.io ______ <

    moo! > ------ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||