Découverte de Laravel

Thibaud Dauce

@ThibaudDauce | https://thibaud.dauce.fr
Quantic Telecom

Co-fondateur de Quantic Telecom

https://www.quantic-telecom.net
Formations Laravel

Formations Laravel

https://www.formations-laravel.fr

Laravel par rapport à Symfony

France vs USA

L'ORM de Symfony


                    <?php

                    class User
                    {
                        /**
                         * @ORM\Column(type="integer")
                         * @ORM\Id
                         * @ORM\GeneratedValue(strategy="AUTO")
                         */
                        private $id;

                        /**
                         * @ORM\Column(type="string", length=100)
                         */
                        private $email;

                        public function getId()
                        {
                            return $this->id;
                        }

                        public function getEmail()
                        {
                            return $this->email;
                        }

                        public function setId($id)
                        {
                            $this->id = $id;
                        }

                        public function setEmail($email)
                        {
                            $this->email = $email;
                        }
                    }

                    $em = $this->get('doctrine')->getManager();

                    $product = new User();
                    $product->setEmail('thibaud@dauce.fr');
                    $em->persist($product);
                    $em->flush();

                    $users = $em->getRepository('AppBundle:User')->findAll();
                    $user = $em->getRepository('AppBundle:User')->find(1);

                    var_dump($user->getEmail());
                

L'ORM de Laravel


                    <?php

                    class User extends Model
                    {
                        protected $guarded = [];
                        // protected $fillable = ['email'];
                    }

                    $user = User::create([
                        'email' => 'thibaud@dauce.fr',
                    ]);

                    $users = User::all();
                    $user = User::find(1);

                    var_dump($user->email);
                

L'ORM de Laravel


                    <?php

                    use Illuminate\Support\Facades\Schema;
                    use Illuminate\Database\Schema\Blueprint;
                    use Illuminate\Database\Migrations\Migration;

                    class CreateUsersTable extends Migration
                    {
                        public function up()
                        {
                            Schema::create('users', function (Blueprint $table) {
                                $table->increments('id');
                                $table->string('email')->unique();
                                $table->timestamps();
                            });
                        }

                        public function down() {}
                    }
                

Moteur de template

Twig vs Blade

Twig


                    {% for user in users if user.active %}
                       {{ user.email }}
                    {% endfor %}
                

Blade


                    @foreach($users as $user)
                        @if ($user->isActive())
                            {{ $user->email }}
                        @endif
                    @endforeach
                

Routing et configuration

YAML vs PHP

YAML


                    homepage:
                    path:      /
                    defaults:  { _controller: AppBundle:Home:index }
                    userpage:
                    path:      /users/{username}
                    defaults:  { _controller: AppBundle:User:show }

                    parameters:
                    database_driver:   pdo_mysql
                    database_host:     127.0.0.1
                    database_port:     ~
                    database_name:     symfony
                    database_user:     root
                    database_password: ~
                

PHP


                    Route::get('/', 'HomeController@index')->name('homepage');
                    Route::get('/users/{user}', 'UsersController@show')
                        ->name('userpage');

                    return [
                        'driver' => 'mysql',
                        'host' => env('DB_HOST', '127.0.0.1'),
                        'port' => env('DB_PORT', '3306'),
                        'database' => env('DB_DATABASE', 'forge'),
                        'username' => env('DB_USERNAME', 'forge'),
                        'password' => env('DB_PASSWORD', ''),
                    ];
                

Formulaires

PHP vs HTML

PHP


                    $form = $this->createFormBuilder($task)
                        ->add('email', TextType::class, [
                            'label' => "Email"
                        ])->add('password', PasswordType::class, [
                            'label' => "Mot de passe"
                        ])->add('save', SubmitType::class, [
                            'label' => "S'inscrire"
                        ])->getForm();

                    {{ form_start(form) }}
                    {{ form_widget(form) }}
                    {{ form_end(form) }}
                

HTML


                    
{{ csrf_field() }}

@if ($errors->has('email'))

{{ $errors->first('email') }}

@endif

Framework de tests

Tests unitaires avec PHPUnit

Tests HTTP intégrés à Laravel

Tests HTML intégrés à Symfony

Frameworks de test optionnels chez Laravel

Laravel Browser Kit Testing (surcouche à Symfony)

Laravel Dusk (surcouche à Sélénium)

Injection de dépendances automatique

Configuration en YAML vs PHP

Mails

Swift_Message vs Mailable

Swift_Message


                    $message = new \Swift_Message('Bienvenue !')
                        ->setFrom('thibaud@dauce.fr')
                        ->setTo($user->getEmail())
                        ->setBody(
                            $this->renderView(
                                'Emails/welcome.html.twig',
                                array('user' => $user)
                            ),
                            'text/html'
                        )->addPart(
                            $this->renderView(
                                'Emails/welcome.txt.twig',
                                array('user' => $user)
                            ),
                            'text/plain'
                        );

                    $mailer->send($message);
                

Mailable


                    use App\User;
                    use Illuminate\Bus\Queueable;
                    use Illuminate\Mail\Mailable;
                    use Illuminate\Queue\SerializesModels;

                    class Welcome extends Mailable
                    {
                        use Queueable, SerializesModels;

                        public $user;

                        public function __construct(User $user)
                        {
                            $this->user = $user;
                        }

                        public function build()
                        {
                            return $this->subject('Bienvenue !')
                                ->from('thibaud@dauce.fr')
                                ->markdown('emails.users.welcome');
                        }
                    }

                    Mail::to($user->email)->send(new Welcome($user));
                

Et bien d'autres différences

Travaux pratiques

Création d'un Twitter like

Features

  • Inscription des utilisateurs (username, email, password)
  • Connexion des utilisateurs
  • Publication d'un statut
  • Visualisation des statuts des utilisateurs
  • Possibilité de like un statut
  • Envoi d'un mail lors d'un like
  • Tests ?
  • Utilisation de SASS ?
  • Utilisation de JavaScript ?