Laravel 表单方法 VS 传统编码

2023-11-01php开发问题
0

本文介绍了Laravel 表单方法 VS 传统编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在学习 Laravel,发现它非常有用且有趣.

I am currently learning Laravel and finding it really useful and interesting.

目前我正在制作一个简单的在线申请表.

At the moment I am making a simple online application form.

使用 Laravel 语法做事的最大优势是什么:

What are the biggest advantages to doing things using the Laravel syntax like:

{{ Form::open(array('url' => 'foo/bar')) }}

相对于简单的:

<form action="foo/bar">

或者:

echo Form::text('username');

代替:

<input type="text" name="username" />

Laravel的方式一定更好,我只是想知道为什么?

The Laravel way must be better, I just wish to know why exactly?

推荐答案

使用内置的 HTML 助手有很多好处:

Using built-in HTML helpers have many benefits:

  1. 使用 Form::open 添加 CSRF 保护输入隐藏(默认)

  1. Using Form::open you add CSRF protection input hidden (by default)

使用表单元素(inputs/textarea 等)和 withInput 方法进行重定向,让您可以轻松地用相同的数据填写表单,几乎无需编码

Using form elements (inputs/textarea etc.) and withInput method for Redirection allows you to easily fill in the form with the same data with almost no coding

如果你使用 Redirect::route('form'->withInput(); 并且有输入text {{Form::text('username')}} 它将自动将输入的值设置为旧数据 - 您无需自己编写代码检查它

If you use Redirect::route('form'->withInput(); and have input text {{Form::text('username')}} it will automatically set input's value the old data - you don't need to code it yourself checking it

此外,如果您想将字段与标签匹配起来会容易得多:

Also if you want to match fields with labels its much easier:

{{ Form::label('username', 'Enter username') }}
{{ Form::text('username') }}

它将生成以下代码:

<label for="username">Enter username</label>
<input name="username" type="text" id="username">

如您所见,id 将自动创建

so as you see id will be created automatically

可能还有更多.然而,主要的缺点是您需要学习并且它不便携,以防您想将您的网站移动到其他框架,但每个解决方案都有优点和缺点.

Probably there are some more. However the main disadvantage is that you need to learn and it's not portable in case you want to move your site to other Framework but each solution has pros and cons.

这篇关于Laravel 表单方法 VS 传统编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17