Skip to content
Advertisement

A ViewModel for a Contact Form in Laravel

I am not new to MVC, but I am just getting started with Laravel. In ASP.NET MVC we have ViewModels which is basically a data object that we can pass to the view. The view can then use the ViewModel in whatever way it wishes.

Is there such a thing in Laravel? I want to create a ViewModel that I can pass to my contact view and have it create a form scaffold around it!

What I currently have is:

ROUTE

Route::get('/contact', array('as' => 'contact', 'uses' => 'HomeController@contact'));

Route::post('/contact', array('as' => 'contact', 'uses' => 'HomeController@postContact'));

CONTROLLER

<?php

class HomeController extends BaseController {

    public function contact()
    {
        return View::make('contact');
    }

    public function postContact()
    {
        $formData = Input::all();

        return View::make('contact');
    }

}

Also, do you foresee anything wrong with my routes and controller code? In ASP.NET MVC we can have a route with exact same name as long as the verbs are different (POST / GET).

My contact form has 3 textfields (name, email and subject) and one textarea (message).

Advertisement

Answer

Probably you are talking about a view/model presenter which is another layer between your model and view and in this case you may build your own or use existing packages:

  1. ShawnMcCool/laravel-auto-presenter

  2. robclancy/presenter

Also read this article to get the clear idea about presenter.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement