Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

php - Laravel Form methods VS traditional coding

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

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

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

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

As opposed to simply:

<form action="foo/bar">

Or:

echo Form::text('username');

Instead of:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using built-in HTML helpers have many benefits:

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

  2. 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

    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

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

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

    it will generate the following code:

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

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...