Tuesday, February 10, 2015

An alternative to the laravel 5 contact form

Introduction

I was reading the contact form tutorial, and after that I'm trying to get the tutorial working on my pc.

There was a bit of information missing, but that's because the writes wants you to buy his book. And there are a few ways the code can be written different.

Get on your marks, GO!

If you don't know how to install laravel, check the excellent documentation.
To run the development server, run php artisan serve in the command line.

alternative routes

Laravel has a way to prefix the controller methods with the http verbs, they call it implicit controllers.

In the file app/Http/routes.php you add the line Route::controller('contact', 'ContactController');.

To create the controller, run php artisan make:controller ContactController --plain.
The --plain parameter creates the controller without methods.

The controller

You can ignore line 5 and 6 for the moment, they will be created later.

Add the lines 10 to 23 to your controller.

To get the getIndex method working the index.blade.php file needs to exist.

index.blade.php

This file has to be created in the resources/views/contact directory

The missing information in the original article is to make the Form methods work.

  1. Add "illuminate/html": "5.*" to the required section of the composer.json, and run composer update
  2. Add 'Illuminate\Html\HtmlServiceProvider', to the config/app.php file in the providers array.
  3. Add 'Form'=> 'Illuminate\Html\FormFacade', to the file above in the aliases array

intermezzo

If you put the postIndex method of the controller and lines 5 and 6 in comments. And start the development server, you will see the contact form at http://localhost:8000/contact.

ContactFormRequest

run php artisan make:request ContactFormRequest.

On line 14 you change false to true, because you want anonymus users to send the contact form.

In the rules method you add the items from the code above.

ContactFormValid event

The original article lets you add the mail sending code to the store controller method. This gives the controller to much responsibility. I will use an event to trigger the mail sending.

In the app/Providers/EventServiceProvider.php listen array you add following item;
'App\Events\ContactFormValid' => ['App\Handlers\Events\EmailContactForm', ],

As you can see the event itself is in the app/events directory and all the code that needs to be executed is in the app/Handlers/Events directory.

Then you run php artisan event:generate.

ContactFormValid.php

As you can see in the controller on line 18 the ContactFormRequest instance is added to the constructor.
In the event file the all method is called to fill the data variable.

EmailContactForm.php

On line 2รง you see the data variable is added to the Mail send call.

Because it is an array you can use this handler for other events, the only thing you need to check are the data keys and their value.

contact.blade.php

In the handler on line 28 we defined this view file, which is in resources/views/emails directory.

The big finish

Remove all comments from the controller and fill the contact form in your browser.

Now the controllers only function is to display a view. And when the form is posted with valid data it fires an event and redirects the user to the form.