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!

Christoph Rumpel

November 27, 2015
Tweet

More Decks by Christoph Rumpel

Other Decks in Programming

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

    View Slide

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

    View Slide

  3. A F R A M E W O R K I S
    JUST A TOOL
    could be more!

    View Slide

  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 …

    View Slide

  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

    View Slide

  6. L A R A V E L I S …
    A N E C O S Y S T E M

    View Slide

  7. T H I S I S M E
    or

    View Slide

  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

    View Slide

  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

    View Slide

  10. TAY L O R O T W E L L
    T H I S G U Y

    View Slide

  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.

    View Slide

  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 !

    View Slide

  13. F I R S T S T E P S
    L A R A V E L

    View Slide

  14. R O U T E S
    W H E R E D O Y O U WA N N A G O

    View Slide

  15. Route::get(‘/‘, function() {
    return ‘Hello DrupalCamp’;
    });

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  20. Recipe::all();

    View Slide

  21. Recipe::find(1);

    View Slide

  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

    View Slide

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

    View Slide

  24. $myTopRecipes = Recipe::where(‘user_id', 1)
    ->orderBy(‘likes’, ‘desc’)
    ->take(5)
    ->get();
    foreach ($myTopRecipes as $recipe)
    {
    var_dump($recipe->name);
    }

    View Slide

  25. C O N T R O L L E R S

    View Slide

  26. class RecipeController extends Controller
    {
    public function index()
    {
    $recipes = Recipes::all();
    return view(‘recipes.index’, [‘recipes’ => $recipes]);
    }
    }

    View Slide

  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

    View Slide

  28. B L A D E
    D E F I N E Y O U R V I E W

    View Slide

  29. // …views/layouts/master.blade.php


    My recipes




    @yield('content')

    @include(‘parts.footer')


    View Slide

  30. // …views/recipes/index.blade.php
    @extends('layouts.master')
    @section('content')
    My recipes

    @stop

    View Slide

  31. My favourite recipes

    @foreach ($recipes as $recipe)
    {{ $recipe->name }}
    @endforeach

    View Slide

  32. M I G R AT I O N S
    W E L C O M E

    View Slide

  33. Migrations are like version
    control for your database

    View Slide

  34. 2015_11_27_123134_create_recipes_table.php
    2015_11_27_123322_create_ingredients_table.php

    View Slide

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

    View Slide

  36. public function up() {
    Schema::create('recipes', function(Blueprint $table)

    {
    $table->increments(‘id');
    $table->string(‘name')->unique();

    });
    }

    View Slide

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

    View Slide

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

    View Slide

  39. M I D D L E WA R E
    M A L C O L M I N T H E

    View Slide

  40. Auth
    A P P
    Session
    Request
    Middleware

    View Slide

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

    View Slide

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

    {

    // … redirect to login

    }


    // let the request through
    }

    View Slide

  43. Route::post(‘/recipe’, [
    'middleware' => ‘auth’
    ‘uses' => ‘RecipeController@store’
    ]);

    View Slide

  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

    View Slide

  45. IoC
    Inversion of Control

    View Slide

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

    View Slide

  47. Constructor injection

    View Slide

  48. class RecipeController extends BaseController
    {
    protected $mailer;
    public function __construct(Mailer $mailer)
    {
    $this->mailer = $mailer;
    }
    public function store()
    {
    //… store logic
    $this->mailer->send(…);
    }
    }

    View Slide

  49. Method injection

    View Slide

  50. class RecipeController extends BaseController
    {
    public function index(Mailer $mailer)
    {
    $mailer->send(…);
    }
    }

    View Slide

  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

    View Slide

  52. php artisan list

    View Slide

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

    View Slide

  54. RESTful Resource Controller

    View Slide

  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’);

    View Slide

  56. php artisan make:controller RecipeController

    View Slide

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

    }

    View Slide

  58. Route::resource(‘/recipe’, ‘RecipeController’);

    View Slide

  59. php artisan email:birthday

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  63. class StoreRecipeFormRequest extends Request {
    public function authorize() {…}
    public function rules() {…}
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  68. T E S T I N G
    E N L I G H T E N

    View Slide

  69. class HomeTest extends TestCase {
    public function testHomeSite()
    {
    $this->visit(‘/‘)
    ->assertResponseOk();
    }
    }

    View Slide

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

    View Slide

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

    View Slide

  72. E C O S Y S T E M
    L A R VA L

    View Slide

  73. Homestead
    Laravel Vagrant Box

    View Slide

  74. View Slide

  75. View Slide

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

    View Slide

  77. Laracasts
    The best PHP screencasts on the web

    View Slide

  78. View Slide

  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

    View Slide

  80. Thank YOU

    View Slide

  81. Questions?

    View Slide

  82. Thank YOU AGAIN

    View Slide

  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/

    View Slide