Kontaktformular mit Laravel

Posted in Laravel, 25.01.2017 08:01

Generate the following:


php artisan make:controller AboutController
php artisan generate:view contact
php artisan make:request ContactFormRequest
php artisan generate:view emails.contact

Details:

AboutController

<?php

namespace blog\Http\Controllers;

use Illuminate\Http\Request;
use blog\Http\Requests\ContactFormRequest;

class AboutController extends Controller
{

    public function create()
    {
        return view('contact');
    }

    public function store(ContactFormRequest $request)
    {
        \Mail::send('emails.contact', [
                    'name' => htmlentities($request->get('name')),
                    'email' => htmlentities($request->get('email')),
                    'user_message' => htmlentities($request->get('message'))
                ], function($message) use ($request) {
                       $message->from('info@lbm-services.de')
                                ->replyTo($request->get('email'))
                                ->to('info@lbm-services.de', 'Admin')
                                ->subject('Blog Kontakt: ' . $request->get('subject') );
                  });
        return redirect('/')->with('message', 'Danke für die Nachricht.');
    }

}

ContactFormRequest contains Rules

<?php

namespace blog\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|alpha_dash|between:3,100',
        'email' => 'required|email|between:3,100',
        'subject' => 'required|between:3,150',    
        'message' => 'required',
            ];
    }

}
 

contact.blade.php

@extends('layouts.frontend')
@section('content')

Kontakt
@if (count($errors) > 0)

    @foreach($errors->all() as $error)
    {{ $error }}
    @endforeach

@endif {!! Form::open(array('route' => 'contact_store', 'class' => 'form')) !!}
{!! Form::label('Name') !!} {!! Form::text('name', null, array('required', 'class'=>'form-control', 'placeholder'=>'Name')) !!}
{!! Form::label('E-Mail') !!} {!! Form::text('email', null, array('required', 'class'=>'form-control', 'placeholder'=>'E-Mail')) !!}
{!! Form::label('Betreff') !!} {!! Form::text('subject', null, array('required', 'class'=>'form-control', 'placeholder'=>'Betreff')) !!}
{!! Form::label('Nachricht') !!} {!! Form::textarea('message', null, array('required', 'class'=>'form-control', 'placeholder'=>'Nachricht')) !!}
{!! Form::submit('Senden', array('class'=>'btn btn-primary')) !!}
{!! Form::close() !!}
@endsection

email/contact.blade.php is email-template

Name: {{ $name }}

{{ $email }}

{{ $user_message }}