简体   繁体   English

没有集体的Laravel形式

[英]Laravel form without Collective

I'm learning Laravel. 我正在学习Laravel。 The current stable version is, as far as I'm aware, 5.8. 据我所知,当前的稳定版本是5.8。 I'm following tutorials and really liking the framework, but it gets a bit troublesome when these tutorials get to the point where they introduce how forms are incorporated. 我正在关注教程,并且确实喜欢该框架,但是当这些教程到达介绍表单如何合并的地步时,这会有些麻烦。 All of those tutorials use LaravelCollective forms, which is no longer working as of 5.8 and it is an abandoned project so I'd prefer not to use it anyway. 所有这些教程都使用LaravelCollective形式,该形式从5.8开始不再使用,并且是一个废弃的项目,因此我宁愿不使用它。

But this leaves me confused as to what the best practices are for using forms with Laravel. 但是,这让我对使用Laravel使用表单的最佳实践感到困惑。 I've had some goes at creating forms, but... most of it is just HTML with hardly any Laravel "in there", if that makes sense. 我在创建表单方面花了很多功夫,但是...如果可以的话,大多数只是HTML,几乎没有Laravel在其中。 The only Laravel bit here is the form action , where it points to the store function in the TodosController . Laravel唯一的一点是form action ,它指向TodosControllerstore函数。 See below, for a file called create.blade.php . 参见下文,获取名为create.blade.php的文件。

@extends('layouts.app')

@section('content')
    <h1>Create Todo</h1>
    <form action="{{action('TodosController@store')}}" method="post">
        @csrf
        <div class="form-group">
            <label for="text">Text</label>
            <input type="text" name="text" class="form-control" placeholder="Enter title"/>
        </div>
        <div class="form-group">
            <label for="body">Body</label>
            <textarea class="form-control"  name="body" id="body" rows="10" placeholder="Enter details"></textarea>
        </div>
        <div class="form-group">
            <label for="due">Due date</label>
            <input type="text" name="due" class="form-control" placeholder="Enter due date"/>
        </div>
        <input type="submit" value="Submit" class="btn btn-primary">
    </form>
@endsection

This works fine, but I just feel like I'm not utilising blade properly here at all. 这工作正常,但是我只是觉得我根本没有正确使用刀片。 Any pointers would be greatly appreciated. 任何指针将不胜感激。

Actually, you're using more laravel there than just the form action . 实际上,您在那里使用的不仅是form action而且还有更多的laravel。 The @csrf stands for Cross-site request forgery and it's the laravel way to protect you against that, as said in the docs: @csrf代表跨站点请求伪造 ,这是保护您免受伪造的laravel方法,如文档所述:

Laravel automatically generates a CSRF "token" for each active user session managed by the application. Laravel为由应用程序管理的每个活动用户会话自动生成CSRF“令牌”。 This token is used to verify that the authenticated user is the one actually making the requests to the application. 此令牌用于验证经过身份验证的用户是实际向应用程序发出请求的用户。

Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. 每当您在应用程序中定义HTML表单时,都应在表单中包含一个隐藏的CSRF令牌字段,以便CSRF保护中间件可以验证请求。 You may use the @csrf Blade directive to generate the token field: 您可以使用@csrf Blade指令生成令牌字段:

When you have a PUT, PATCH OR DELETE form you should use the blade directive @method to inform wich action laravel should use: 当您拥有PUT, PATCH OR DELETE表格时,应使用blade指令@method来告知laravel应该使用的操作:

HTML forms do not support PUT, PATCH or DELETE actions. HTML表单不支持PUT,PATCH或DELETE操作。 So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. 因此,在定义从HTML表单调用的PUT,PATCH或DELETE路由时,您将需要向表单添加一个隐藏的_method字段。 The value sent with the _method field will be used as the HTTP request method: 与_method字段一起发送的值将用作HTTP请求方法:

You can achieve that, simply using: 您可以使用以下方法实现此目的:


    <form action="/foo/bar" method="POST">
    @method('PUT')
    @csrf
    </form>

Besides that, i think you're using laravel/blade just fine. 除此之外,我认为您正在使用laravel / blade就好了。 Make sure you read the docs for more info. 确保您阅读了文档以获取更多信息。

Good luck! 祝好运!

What you have is a good point to start from, however another good place to take a look at is the boilerplate registration form (this is not from the official Laravel project page because the boilerplates are optionally introduced and are not in the official repo by default). 您所拥有的是一个很好的起点,但是,另一个值得一看的好地方是样板注册表单 (这不是来自Laravel官方项目页面,因为可选地引入了样板,默认情况下不在正式的repo中)。

There are a few improvements you can do based on this: 您可以基于此进行一些改进:

 <div class="form-group">
     <label for="text">{{__('Text')}}</label>
     <input type="text" name="text" class="form-control{{ $errors->has('text') ? ' is-invalid' : '' }}" value="{{ old('text') }}"placeholder="Enter title"/>
 </div>

The extras: 附加功能:

  • __('Text') will automatically translate Text based on the selected locale and available language assets. __('Text')将根据所选的语言环境和可用的语言资产自动翻译Text
  • {{ $errors->has('text') ? ' is-invalid' : '' }} {{ $errors->has('text') ? ' is-invalid' : '' }} will "decorate" the field with the bootstrap-4 error style if serverside validation failed (and therefore passed the $errors variable to the view) 如果服务器端验证失败(因此将$errors变量传递给视图),则{{ $errors->has('text') ? ' is-invalid' : '' }}将使用bootstrap-4错误样式“修饰”该字段。
  • {{ old('text') }} will pre-fill the input with the value that was previously filled in case the form failed validation and the user was redirected back to the same page. {{ old('text') }}将使用先前填充的值预先填充输入,以防表单验证失败并且用户被重定向回同一页面。

This will help improve the user experience, however keep in mind these are all server-side tools (because Laravel is a server-side framework) so it's probably a better user experience to also add client-side checks and validation. 这将有助于改善用户体验,但是请记住,这些都是服务器端工具(因为Laravel是服务器端框架),因此添加客户端检查和验证可能是更好的用户体验。

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

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