Skip to content

Instantly share code, notes, and snippets.

@paulund
Created November 30, 2017 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulund/f64d12ee69b6d156c98a74503ff14448 to your computer and use it in GitHub Desktop.
Save paulund/f64d12ee69b6d156c98a74503ff14448 to your computer and use it in GitHub Desktop.
Files you need for a Laravel Contact Form Package https://paulund.co.uk/creating-a-laravel-contact-form-package
<?php
/**
* Contact form API
*/
Route::namespace('Paulund\ContactForm\Http\Controllers')->prefix('api')->group(function ($api) {
$api->post('contact', 'ContactController@store');
});
{
"name": "paulund/contact-form",
"description": "Paulund laravel contact form",
"type": "project",
"autoload": {
"psr-4": {
"Paulund\\ContactForm\\": "src/"
}
}
}
<template>
<form v-on:submit.prevent="submitForm">
<div class="control">
<label>Name</label>
<input type="type" v-model="name" required="true">
</div>
<div class="control">
<label>Email</label>
<input type="email" v-model="email" required="true">
</div>
<div class="control">
<label>Message</label>
<textarea v-model="message" required="true"></textarea>
</div>
<div class="control">
<input type="submit" class="button" value="Send">
</div>
</form>
</template>
<script>
import axios from 'axios';
export default {
data: function(){
return {
name: '',
email: '',
message: ''
}
},
methods: {
submitForm(){
let formData = {
name:this.name,
email:this.email,
message:this.message,
};
axios.post('/api/contact', formData).then(data => {
console.log('Message sent');
});
}
}
}
</script>
@component('mail::message')
# Contact Form
<p>From: {{ $contact['name'] }}</p>
<p>Email: {{ $contact['email'] }}</p>
{{ $contact['message'] }}
Thanks,<br>
{{ config('app.name') }}
@endcomponent
<?php
namespace Paulund\ContactForm\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Mail;
use Paulund\ContactForm\Http\Requests\ContactFormRequest;
use Paulund\ContactForm\Mail\ContactEmail;
use Paulund\LaravelCommon\Http\Controllers\Controller;
class ContactController extends Controller
{
public function store( ContactFormRequest $request )
{
Mail::to( config('mail.from.address') )->send( new ContactEmail($request->only([
'name', 'email', 'message'
])) );
return response()->json( ['sent' => true], Response::HTTP_OK);
}
}
<?php
namespace Paulund\ContactForm\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ContactEmail extends Mailable
{
use Queueable, SerializesModels;
public $contact;
/**
* Create a new message instance.
*
*/
public function __construct(array $contact)
{
$this->contact = $contact;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('mail.contact');
}
}
<?php
namespace Paulund\ContactForm\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ContactFormRequest extends FormRequest
{
/**
* 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',
];
}
}
<?php
namespace Paulund\ContactForm;
use Illuminate\Support\ServiceProvider;
class ContactFormServiceProvider extends ServiceProvider
{
public function boot()
{
// Load routes
$this->loadRoutesFrom(__DIR__ . '/Routes/api.php');
// Assets
$this->publishes([
__DIR__.'/Resources/assets' => resource_path('paulund/assets/contactform')
], 'paulund');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment