Laravel5 framework learning form

Source: Internet
Author: User
The FormRequest feature introduced by Laravel5.0 provides set normalization (almost the best practice) and convenience (this is a more powerful and convenient way than any previous choice) yu

Laravel 5.0's newly introduced Form Request feature provides set normalization (almost the Best Practice) and convenience (this is more powerful and more convenient than any other method)

First, let's modify the route to add an article release.

The Code is as follows:


Route: get ('articles/create', 'articlescontroller @ create ');

Then modify the Controller

The Code is as follows:


Public function create (){
Return view ('articles. create ');
}

We return a view to create this view. Of course we can use HTML to create a form directly, but we have a better function. We use an open-source library, illuminate \ html developed by Jeffrey Way. Install the dependency Library:

The Code is as follows:


Composer require illuminate/html

The laravel library must be registered in laravel for use. In config/app. php, we can see the provider field provided by laravel, which describes the library function of laravel. Add the newly added HtmlProvider in Laravel Framewirk Service Providers...

The Code is as follows:


'Illuminate \ Html \ HtmlServiceProvider ',

We do not want to use such a long name as Illuminate \ Html \ FromFacade. We need a short name. Find the aliases segment in the current app. php and add the alias at the end.

The Code is as follows:


'Form' => 'illuminate \ Html \ FormFacade ',
'Html' => 'illuminate \ Html \ HtmlFacade ',

OK. Now let's create a view, views/articles/create. blade. php

@ Extends ('layout ') @ section ('content') Write a New Article

{-- Use the illuminate \ html open source library we added --}}{!! Form: open ()!!} {!! Form: close ()!!} @ Stop

Access/articles/create to see the error, Why? Let's test what went wrong. Make the following changes in the controller:

Public function show ($ id) {dd ('show '); $ article = Article: findOrFail ($ id); return view ('articles. show ', compact ('Article '));}


Yes, you are not mistaken. It is to add the dd () method to the show method. This method simply outputs a message and then dies. Let's access/articles/create again. What do you see, and show is output.

Why did we access the create result route to show? Let's check what happened to the route.

The Code is as follows:


Route: get ('articles', 'articlescontroller @ Index ');
Route: get ('articles/{id} ', 'articlescontroller @ show ');
Route: get ('articles/create', 'articlescontroller @ create ');

The above is our route. Note that articles/{id} indicates that this is a wildcard character. All things after articles/will match. Do you know? Our/articles/create is also matched by him. OMG!

The solution is to adjust the order:

The Code is as follows:


Route: get ('articles', 'articlescontroller @ Index ');
Route: get ('articles/create', 'articlescontroller @ create ');
Route: get ('articles/{id} ', 'articlescontroller @ show ');

That is, you should always pay attention to this problem in route settings from special to normal. Now we are accessing articles/create. Everything is OK.

Check the source code in the browser. You will find that not only the method and action are generated, but a hidden _ token field is generated as the server's verification of the form to avoid hacker forgery attacks.

Let's modify our view and add fields:

@ Extends ('layout ') @ section ('content') Write a New Article

{-- Use the illuminate \ html open source library we added --}}{!! Form: open ()!!}

{!! Form: label ('title', 'title :')!!} {!! Form: text ('title', null, ['class' => 'form-control'])!}

{!! Form: label ('body', 'body :')!!} {!! Form: textarea ('body', null, ['class' => 'form-control'])!}

{!! Form: submit ('add article', ['class' => 'btn btn-primary form-control'])!}

{!! Form: close ()!!} @ Stop


When a form is submitted, it is actually submitted to articles/create using the post method, but according to the RESTful habit, we hope to post to/articles, modify the form method of the view and set the submission path.

The Code is as follows:


{!! Form: open (['url' => 'articles '])!}

Then we process the form submission event in the route.

The Code is as follows:


Route: post ('/articles', 'articlescontroller @ store ');

Let's process the controller.

// Note: delete the following use statement. We use the Request // use App \ Http \ Requests \ Request in the facade interface; // introduce Requestuse Illuminate \ Support \ Facades \ Request; public function store () in the namespace below () {// use Illuminate \ Html \ Request to return all form input fields $ input = Request: all (); // we directly return $ input, return $ input ;}

We can directly see the json result of the input form. If you only need the value of the title field, you can use Request: get ('titel ').

How can I add it to a database? With the help of the model, we can directly use the following method,

Article: create ($ input );

It's so simple, it's so capricious

If you have not forgotten the Mass Assignment, we have defined the $ fillable array in our model to define the fields that can be directly filled during create.

Modify the controller, add it to the model, and store it in the database.

Public function store () {$ input = Request: all (); Article: create ($ input); return redirect ('Article ');}

Try adding a record. But don't forget. We also have a field called published_at. Let's process it.

Public function store () {$ input = Request: all (); $ input ['hhed _ at'] = Carbon: now (); Article :: create ($ input); return redirect ('articles ');}

Add a new record in the test.

Another problem is that the newly added controller should be shown at the beginning. Let's modify the following controller.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.