简体   繁体   English

Laravel 中联系表单的 ViewModel

[英]A ViewModel for a Contact Form in Laravel

I am not new to MVC, but I am just getting started with Laravel.我对 MVC 并不陌生,但我才刚刚开始使用 Laravel。 In ASP.NET MVC we have ViewModels which is basically a data object that we can pass to the view.在 ASP.NET MVC 中,我们有 ViewModels,它基本上是一个我们可以传递给视图的数据对象。 The view can then use the ViewModel in whatever way it wishes.然后视图可以以任何它希望的方式使用 ViewModel。

Is there such a thing in Laravel? Laravel 里有这种东西吗? I want to create a ViewModel that I can pass to my contact view and have it create a form scaffold around it!我想创建一个可以传递给我的联系人视图的 ViewModel 并让它围绕它创建一个表单脚手架!

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).在 ASP.NET MVC 中,只要动词不同(POST / GET),我们就可以拥有一个名称完全相同的路由。

My contact form has 3 textfields (name, email and subject) and one textarea (message).我的联系表单有 3 个文本字段(姓名、电子邮件和主题)和一个文本区域(消息)。

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:可能您正在谈论view/model presenter ,它是您的modelview之间的另一层,在这种情况下,您可以构建自己的或使用现有的包:

  1. ShawnMcCool/laravel-auto-presenter ShawnMcCool/laravel-auto-presenter

  2. robclancy/presenter抢劫/主持人

Also read this article to get the clear idea about presenter .另请阅读本文以清楚了解presenter

In Laravel we have view models too.在 Laravel 中我们也有视图模型。 You can use the vie model package from github.您可以使用来自 github 的 vie 模型包。 Here's the link to the package on GitHub: View models in Laravel这是 GitHub 上包的链接:在 Laravel 中查看模型
You can install the package from composer by running:您可以通过运行以下命令从 Composer 安装软件包:

composer require spatie/laravel-view-models

You can also use nwidart for better and easier management of your mvc model.您还可以使用 nwidart 更好、更轻松地管理您的 mvc 模型。 nwidart gives you the feature of modules in your laravel app. nwidart 为您提供了 Laravel 应用程序中模块的功能。 More information on nwidart .有关nwidart 的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM