Last active
September 26, 2015 10:45
-
-
Save davidDuymelinck/121bcf80eae665f06729 to your computer and use it in GitHub Desktop.
Laravel 5 contact demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You received a message from test.com: | |
<p> | |
Name: {{ $name }} | |
</p> | |
<p> | |
Email: {{ $email }} | |
</p> | |
<p> | |
{{ $message }} | |
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
use App\Http\Requests\ContactFormRequest; | |
use App\Events\ContactFormValid; | |
class ContactController extends Controller { | |
public function getIndex() | |
{ | |
return view('contact.index'); | |
} | |
public function postIndex(ContactFormRequest $request) | |
{ | |
event(new ContactFormValid($request)); | |
return \Redirect::route('contact') | |
->with('message', 'Thanks for contacting us!'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Requests; | |
use App\Http\Requests\Request; | |
class ContactFormRequest extends Request { | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return true; | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
'name' => 'required', | |
'email' => 'required|email', | |
'message' => 'required', | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Events; | |
use App\Events\Event; | |
use Illuminate\Queue\SerializesModels; | |
class ContactFormValid extends Event { | |
use SerializesModels; | |
/** | |
* Create a new event instance. | |
* | |
* @return void | |
*/ | |
public function __construct($data) | |
{ | |
$this->data = $data->all(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Handlers\Events; | |
use App\Events\ContactFormValid; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Contracts\Queue\ShouldBeQueued; | |
class EmailContactForm { | |
/** | |
* Create the event handler. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Handle the event. | |
* | |
* @param ContactFormValid $event | |
* @return void | |
*/ | |
public function handle(ContactFormValid $event) | |
{ | |
\Mail::send('emails.contact', | |
$event->data | |
function($message) | |
{ | |
$message->from('wj@test.com'); | |
$message->to('dfs@test.com', 'Admin')->subject('Feedback'); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Contact/h1> | |
@if(Session::has('message')) | |
<div> | |
{{ Session::pull('message') }} | |
</div> | |
@endif | |
@if(! $errors->isEmpty()) | |
<ul> | |
@foreach($errors->all() as $error) | |
<li>{{ $error }}</li> | |
@endforeach | |
</ul> | |
@endif | |
{!! Form::open(array('route' => 'contact')) !!} | |
<div class="form-group"> | |
{!! Form::label('Your Name') !!} | |
{!! Form::text('name', null, | |
array('required', | |
'placeholder'=>'Your name')) !!} | |
</div> | |
<div class="form-group"> | |
{!! Form::label('Your E-mail Address') !!} | |
{!! Form::text('email', null, | |
array('required', | |
'placeholder'=>'Your e-mail address')) !!} | |
</div> | |
<div class="form-group"> | |
{!! Form::label('Your Message') !!} | |
{!! Form::textarea('message', null, | |
array('required', | |
'placeholder'=>'Your message')) !!} | |
</div> | |
<div class="form-group"> | |
{!! Form::submit('Contact Us!') !!} | |
</div> | |
{!! Form::close() !!} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment