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

Intro to Laravel

Intro to Laravel

Intro the Laravel to be presented by Ben Edmunds at ATLPHP on June 7th, 2012.

Ben Edmunds

May 09, 2012
Tweet

More Decks by Ben Edmunds

Other Decks in Technology

Transcript

  1. Who Am I Ben Edmunds @benedmunds http://benedmunds.com Build and lead

    development teams >10 years of experience in the industry Specialize in PHP development
  2. Who Am I Ben Edmunds @benedmunds http://benedmunds.com Ion Auth http://github.com/benedmunds/CodeIgniter-Ion-Auth

    General http://github.com/benedmunds Open Source Contributions http://appstucco.com
  3. Who Am I Personal: From Phenix City, AL Married for

    6 years Work for Kforce Government Solutions Co-Founder of AppStucco Tech Focus: PHP (CodeIgniter, Laravel) Javascript (Node.js, Express, Phonegap) Databases (MySQL, PostGreSQL, MongoDB) Ben Edmunds @benedmunds http://benedmunds.com
  4. Why use it? Built on PHP 5.3 Sinatra-style Routing Bundles

    http://bundles.laravel.com/ Artisan CLI Tool Wave your PHP flag proudly that’s right, I use PHP sucka Expressive Syntax Eloquent ORM Well Documented
  5. Why use it? Expressive Syntax Utilizes the latest PHP features

    Route::get('user/(:num)', function($id) { $user = User::find($id); return View::make('home')->with('user', $user); }); <?php namespace Eloquent; use Laravel\Database as DB; abstract class Model {
  6. Why use it? Bundles http://bundles.laravel.com/ $ php artisan bundle:install twilio

    Artisan CLI Tool $ php artisan bundle:install twilio Well Documented http://laravel.com/docs
  7. Why use it? Sinatra-style Routing Eloquent ORM Route::get('/', function() {

    //do neat shit }); $user = new User; $user->id = 1; $user->save(); $user = User::find(1);
  8. Structure MV[CR] Design Model View Controller || Route || Both

    Bundles Self contained modules Has it’s own config, models, routes, views, etc...
  9. Structure application - config - controllers - models - views

    - routes.php - bundles.php - etc... laravel public - css - img - js - etc...
  10. Unit Test application/tests/routes.test.php Test that biotch! class TestRoutes extends PHPUnit_Framework_TestCase

    { public function testBase() { $response = Route::forward('GET', '/'); $this->assertEquals('Boom!', $response); }
  11. Unit Test $ php artisan test Run it! PHPUnit 3.6.10

    by Sebastian Bergmann. Configuration read from ...www/laravel/develop/phpunit.xml Time: 0 seconds, Memory: 6.25Mb OK (1 test, 1 assertion) Output
  12. API application/routes.php Route::put('api/user', function() { $user = User::create(array( 'email' =>

    Input::get(‘email’) )); return json_encode(array( ‘user_id‘ => $user->id, ‘success‘ => true ); });
  13. Routes Register multiple verbs for a single route: Router::register(array('GET', 'POST'),

    $uri, function(){ //this will run for both get and post });
  14. Route Filters Run before or after a given route. Register

    a filter: Route::filter('auth', function() { if (Auth::guest()) return Redirect::to('login'); });
  15. Fluent Query Builder Similar to CI DB Class Simple and

    Easy to Understand Used extensively by Eloquent ORM
  16. Fluent Query Builder $user = DB::table('users')->where_email('[email protected]') ->first(); Query Row by

    Email $id = DB::table('users')->insert_get_id(array( 'email' => '[email protected]' )); Insert & Get ID
  17. Eloquent ORM class User extends Eloquent { } Define an

    Eloquent model class Post extends Eloquent { }
  18. Eloquent ORM Relationships class Post extends Eloquent { public function

    user() { return $this->belongs_to('User'); } } Post::find(1)->user->email; Retrieve Post Creator Email
  19. Eloquent ORM Normal Usage $posts = Post::all(); foreach ($posts as

    $post) { echo $post->user->email; } 100 posts = 101 queries SELECT * FROM "posts" foreach result { SELECT * FROM "users" WHERE "id" = 1 }
  20. Eloquent ORM Eager Loading $posts = Post::with(‘user’)->get(); foreach ($posts as

    $post) { echo $post->user->email; } 100 posts = 2 queries SELECT * FROM "posts" SELECT * FROM "users" WHERE "id" IN (1, 2, 3, 4, 5, ...) Eager Loading FTW!
  21. Eloquent ORM Normal Usage $posts = Post::all(); foreach ($posts as

    $post) { echo $post->user->email; } 100 posts = 101 queries SELECT * FROM "posts" foreach result { SELECT * FROM "users" WHERE "id" = 1 } Eager Loading $posts = Post::with(‘user’)->get(); foreach ($posts as $post) { echo $post->user->email; } 100 posts = 2 queries SELECT * FROM "posts" SELECT * FROM "users" WHERE "id" IN (1, 2, 3, 4, 5, ...)
  22. Views Route::get('/', function() { return View::make('home.index')->with('email', '[email protected]'); Sending data to

    a view with magic methods Route::get('/', function() { $view = View::make('home.index'); $view->email = ‘[email protected]’; return $view; Sending data to a view
  23. That’s a Wrap Utilizes the best PHP has to offer

    Lightweight and crazy fast Well designed API with great docs Go make cool shit