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

Hello World, I'M Laravel

Hello World, I'M Laravel

So there is this thing called Laravel. You may have heard of it already, but you’re not sure what it is actually about? Or you do, but want to know more about it? Great, cause this talks is especially for you! Laravel is at the same time one of the youngest and most popular PHP frameworks out there. So how does this work together? Let us take a closer look at why it is that popular and how it could be of use for you too. We will go through the main functionalities and talk about all the brand new features in version 5. So be there!

14d39e65f615fd6dcb9dd44ea7f7995b?s=128

Christoph Rumpel

November 27, 2015
Tweet

Transcript

  1. H E L L O W O R L D

    , I ’ M L A R AV E L C H R I S T O P H R U M P E L
  2. A F R A M E W O R K

    I S JUST A TOOL
  3. A F R A M E W O R K

    I S JUST A TOOL could be more!
  4. G R E AT F O R B E G

    I N N E R S A N D R A P I D D E V E L O P M E N T L A R A V E L I S …
  5. L A R A V E L I S …

    M A D E F O R A L L K I N D O F P R O J E C T S I Z E S
  6. L A R A V E L I S …

    A N E C O S Y S T E M
  7. T H I S I S M E or

  8. W O R K I N G F O R

    L I E C H T E N E C K E R . AT
  9. F I N D M E C H R I

    S T O P H - R U M P E L . C O M @ C H R I S T O P H R U M P E L
  10. TAY L O R O T W E L L

    T H I S G U Y
  11. G E T T I N G R E A

    L S TA R T S W I T H T H E I N T E R FA C E , T H E R E A L S C R E E N S T H A T P E O P L E A R E G O I N G T O U S E … . T H I S L E T S Y O U G E T T H E I N T E R FA C E R I G H T B E F O R E Y O U G E T T H E S O F T WA R E W R O N G . “ “ G E T T I N G R E A L B Y 3 7 S I G N A L S , C R E A T O R S O F B A S E C A M P.
  12. C O D E E X A M P L

    E S A R E S I M P L I F I E D !
  13. F I R S T S T E P S

    L A R A V E L
  14. R O U T E S W H E R

    E D O Y O U WA N N A G O
  15. Route::get(‘/‘, function() { return ‘Hello DrupalCamp’; });

  16. Route::post(‘/recipe’, ‘RecipeController@store’); Route::get(‘/recipe’, ‘RecipeController@index’);

  17. Route::get(‘/api/recipe/{id}’, ‘RecipeController@show’); Route::get(‘/api/recipe’, ‘RecipeController@index’); Route::post(‘/api/recipe’, ‘RecipeController@store’);

  18. Route::group(['prefix' => ‘api’], function() { }); Route::get(‘/recipe/{id}’, ‘RecipeController@show’); Route::get(‘/recipe’, ‘RecipeController@index’);

    Route::post(‘/recipe’, ‘RecipeController@store’);
  19. E L O Q U E N T N O

    T H I N G I S M O R E E L O Q U E N T T H A N
  20. Recipe::all();

  21. Recipe::find(1);

  22. $recipe = Recipe::where('name', ‘Lasagne’)->get(); $recipe = new Recipe; $recipe->name =

    ‘Lasagne’; $recipe->save(); $recipe->update([‘name’ => ‘Sushi’]); $recipe->delete(); C R D U
  23. $recipe = Recipe::where('name', ‘Lasagne’)->get(); $recipe = Recipe::where(‘name', ‘=‘, ‘Lasagne’)->get(); $recipe

    = Recipe::whereName(‘Lasagne’)->get();
  24. $myTopRecipes = Recipe::where(‘user_id', 1) ->orderBy(‘likes’, ‘desc’) ->take(5) ->get(); foreach ($myTopRecipes

    as $recipe) { var_dump($recipe->name); }
  25. C O N T R O L L E R

    S
  26. class RecipeController extends Controller { public function index() { $recipes

    = Recipes::all(); return view(‘recipes.index’, [‘recipes’ => $recipes]); } }
  27. public function store() { $validator = Validator::make(Input::all(), [ 'name' =>

    'required|unique:recipes|max:255', ]); } if ($validator->fails()) { //… redirect with error message } Recipe::create(Input::all()); // …success message
  28. B L A D E D E F I N

    E Y O U R V I E W
  29. // …views/layouts/master.blade.php <html> <head> <title>My recipes</title> … </head> <body> <div

    class="container"> @yield('content') </div> @include(‘parts.footer') </body> </html>
  30. // …views/recipes/index.blade.php @extends('layouts.master') @section('content') <h1>My recipes</h1> … @stop

  31. <h1>My favourite recipes</h1> <ul> @foreach ($recipes as $recipe) <li>{{ $recipe->name

    }}</li> @endforeach </ul>
  32. M I G R AT I O N S W

    E L C O M E
  33. Migrations are like version control for your database

  34. 2015_11_27_123134_create_recipes_table.php 2015_11_27_123322_create_ingredients_table.php

  35. class CreateUsersTable extends Migration { public function up() {…} public

    function down() {…} }
  36. public function up() { Schema::create('recipes', function(Blueprint $table)
 { $table->increments(‘id'); $table->string(‘name')->unique();


    }); }
  37. public function down() { Schema::drop(‘recipes’); }

  38. N E X T L E V E L L

    A R A V E L
  39. M I D D L E WA R E M

    A L C O L M I N T H E
  40. Auth A P P Session Request Middleware

  41. // app/Http/Middleware class Authenticate { public function handle() {…} }

  42. public function handle() { if ($this->auth->guest())
 {
 // … redirect

    to login
 }
 
 // let the request through }
  43. Route::post(‘/recipe’, [ 'middleware' => ‘auth’ ‘uses' => ‘RecipeController@store’ ]);

  44. D E P E N D E N C Y

    I N J E C T I O N D O N ’ T B E A F R A I D O F
  45. IoC Inversion of Control

  46. public function store() { // …store logic Mail::send(‘email.stored', $data, $callback);

    }
  47. Constructor injection

  48. class RecipeController extends BaseController { protected $mailer; public function __construct(Mailer

    $mailer) { $this->mailer = $mailer; } public function store() { //… store logic $this->mailer->send(…); } }
  49. Method injection

  50. class RecipeController extends BaseController { public function index(Mailer $mailer) {

    $mailer->send(…); } }
  51. A R T I S A N C O M

    M A N D - L I N E I N T E R FA C E
  52. php artisan list

  53. php artisan make:migration create_users_table php artisan make:controller RecipeController php artisan

    route:cache
  54. RESTful Resource Controller

  55. Route::get(‘/recipe’, ‘RecipeController@index’); Route::get(‘/recipe/create’, ‘RecipeController@create’); Route::post(‘/recipe’, ‘RecipeController@store’); Route::get(‘/recipe/{recipe}’, ‘RecipeController@show’); Route::put(‘/recipe/{recipe}’, ‘RecipeController@update’);

  56. php artisan make:controller RecipeController

  57. class RecipeController extends BaseController { public function index() {…} public

    function create() {…} public function store() {…} public function show() {…} … }
  58. Route::resource(‘/recipe’, ‘RecipeController’);

  59. php artisan email:birthday

  60. public function schedule(Schedule $schedule) { $schedule->command(‘email:birthday') ->dailyAt(’09:00’); }

  61. F O R M R E Q U E S

    T B E T T E R VA L I D A T I O N
  62. public function store() { $validator = Validator::make(Input::all(), [ 'name' =>

    'required|unique:recipes|max:255', ]); } if ($validator->fails()) { //… redirect with error message } Recipe::create(Input::all()); // …success message
  63. class StoreRecipeFormRequest extends Request { public function authorize() {…} public

    function rules() {…} }
  64. public function authorize() { return true; }

  65. public function authorize() { return Auth::user()->isAdmin(); }

  66. public function rules() { return [ 'name' => ‘required|unique:recipes|max:255', ];

    }
  67. public function store(StoreRecipeFormRequest $formRequest, Recipe $recipe) { } $recipe->create(Input::all()); //

    …success message
  68. T E S T I N G E N L

    I G H T E N
  69. class HomeTest extends TestCase { public function testHomeSite() { $this->visit(‘/‘)

    ->assertResponseOk(); } }
  70. public function testHomeSite() { $this->visit(‘/‘) ->see(‘My recipes’); }

  71. public function testStoreRecipe() { $this->post(‘/recipe’, [‘name’ => ‘Lasagne’]) ->seeJson([‘created’ =>

    true]); }
  72. E C O S Y S T E M L

    A R VA L
  73. Homestead Laravel Vagrant Box

  74. None
  75. None
  76. Laravel Spark (alpha) Building business oriented SaaS applications

  77. Laracasts The best PHP screencasts on the web

  78. None
  79. L A R AV E L I S M O

    R E T H A N J U S T A T O O L
  80. Thank YOU

  81. Questions?

  82. Thank YOU AGAIN

  83. Resources • http://laravel.com/ • https://laracasts.com/ • https://forge.laravel.com/ • https://envoyer.io/ •

    http://lumen.laravel.com/ • http://www.laravelpodcast.com/ • http://bossfight.co/man-photographer/ • https://unsplash.com/