Controllers

https://laracasts.com/series/laravel-from-scratch-2017/episodes/8 https://openclassrooms.com/fr/courses/3613341-decouvrez-le-framework-php-laravel/3616775-les-controleurs

Table des matières

1. Location

Controllers are stored in the /app/Http/Controllers folder

The easy way to create a controller is by running php artisan make:controller NameController (f.i. NotesController) from the command prompt (first go to the application folder).

The make:controller verb will create a file called NameController in the /app/Http/Controllers folder.

The file will look like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NotesController extends Controller
{
   //
}

So just create functions:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NotesController extends Controller
{
  public function index()
  {
    return 'Return all notes';
  }

  public function show($id)
  {
    return 'Return the note having '.$id.' as ID';
  }
}

Once the controller is created, just edit the router, change

Route::get('/notes', function () {
   $notes = DB::table('notes')->get();
   return view('notes.index', compact('notes'));
});

to

Route::get('/notes', 'NotesController@index');

The entry point is immediately specified: function index();

And, for adding a second entry:

The route

Route::get('/notes/{id}', function ($id) {
   $notes = DB::table('notes')->find($id);
   return view('notes.show', compact('notes'));
});

to

Route::get('/notes', 'NotesController@show');

By convention, index() is for showing the list of ... (articles, notes, customers, ...) and show() is for one item.

2. Dependency injection

The concept: the controller should only know what to do (save an image) and not how to do (save on the filesystem, on a could, in a database, ...). To make this possible, the controller will pass the hand to the class that will really do the job.

This will be done thanks the dependency injection concept.

It’s nicely explain here, in French.

The idea explained here below with a controller for saving an image (anwser of a form where the user can upload and send an image to the server)

<?php
public function postForm(ImagesRequest $request)
{
  $image = $request->file('image');

  if($image->isValid())
  {
    $chemin = config('images.path');

    $extension = $image->getClientOriginalExtension();

    do {
      $nom = str_random(10) . '.' . $extension;
    } while(file_exists($chemin . '/' . $nom));

    if($image->move($chemin, $nom)) {
      return view('photo_ok');
    }
  }

  return redirect('photo')
    ->with('error','Sorry your image can\'t be saved on the server!');
}

In fact, the controller just need to receive the image, call a generic function (save here) and return a message success / failure depending on the result of the save function.

<?php
public function postForm(ImagesRequest $request)
{
  $image = $request->file('image');

  if ($class->save($image)) {
    return view('photo_saved');
  }

  return redirect('photo')
    ->with('error','Sorry your image can\'t be saved on the server!');
}

Doing this allow to reuse again and again the controller.

To learn more, continue reading here, in French.