Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MohammadAbusaleh's avatar

Can't Get Request to work

So I downloaded a fresh install of Laravel, and I'm trying to get a simple contact form to work, the contact form is at the footer, so it I included it in the master layout. Here's what I did: First: because I will need users I ran this command:

    php artisan make:auth

Second: I edit the master page and included my form there.

Then I implemented the contact form logic, then whenever I click on send It just reloads the same page if the route group is set as [api], and if it's set as [web] it shows:

        TokenMismatchException in VerifyCsrfToken.php line 67:

My Routes File:

Route::group(['middleware' => ['api']], function () {
    Route::post('/', 'FormController@handleFormPost')->name('contact');
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::get('/home', 'HomeController@index');
});

My FormController:

class FormController extends Controller
{
        public function handleFormPost(ContactFormRequest $request)
        {
            $request->all();
            Mail::send('emails.contactemail',
                ['name' => $request->get('name'),
                    'email' => $request->get('email'),
                    'msg' => $request->get('msg')], function($message)
                {
                    $message->from('example@example.com');
                    $message->to('example@example.com', 'Admin')->subject('Customer Feedback');
                });

            return Redirect::route('/')->with('message', 'Thank you for contacting us :)');
        }
}

My ContactFormRequest:

class ContactFormRequest extends Request
{
    public function authorize()
        {
            return true;
        }

        public function rules()
        {
        return [
                'name' => 'required|max:120',
                'email' => 'required|email|max:50',
                'msg' => 'required'
            ];
        }
}

Thank you all for your help, If I need to provide more details please inform me.

0 likes
3 replies
bobbybouwmann's avatar

Oke first of all you shouldn't be mixing your routes in the web or API middleware group! Stick with one group. In your case the web group seems to be the best choice.

Now for positing stuff you always need a csrf_token. This error you get has something to do with this. You can fix that by adding this to your form

<form action="your-action">
    {!! csrf_field() !!}

    // Your form stuff here
</form>
MohammadAbusaleh's avatar

@bobbybouwmann I tried that but it didn't work, here's my form code

     {!! csrf_token() !!}
        <div class="form-group">
        {!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Full Name', 'required']) !!}
        </div>
        <div class="form-group">
        {!! Form::text('email', null, ['class' => 'form-control', 'placeholder' => 'Email', 'required']) !!}
        </div>
        <div class="form-group">
        {!! Form::textarea('msg', null, ['class' => 'form-control', 'placeholder' => 'Your Message', 'required']) !!}
        </div>
        <div class="form-group">
        {!! Form::submit('Send', ['class' => 'btn btn-primary form-control', 'required']) !!}
        </div>

and this is the part in the master page

        <div class="col-lg-6 col-md-6 col-sm-12 text-center">
            <h2>Contact Us</h2>
            <hr>
            @include('errors.list')

            {!! Form::open([route('contact')]) !!}

            @include('forms._contactform')

            {!! Form::close() !!}

            @if(Session::has('message'))
                <div class="alert alert-info">
                    {{Session::get('message')}}
                </div>
            @endif

        </div>

Please or to participate in this conversation.