Laravel 5 framework learning form, laravel5 framework form

Source: Internet
Author: User

Laravel 5 framework learning form, laravel5 framework form

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

Copy codeThe Code is as follows:
Route: get ('articles/create', 'articlescontroller @ create ');

Then modify the Controller

Copy codeThe 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:

Copy codeThe 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...

Copy codeThe 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.

Copy codeThe 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 ') 

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.

Copy codeThe 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:

Copy codeThe 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 ') 


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.

Copy codeThe Code is as follows:
{!! Form: open (['url' => 'articles '])!}

Then we process the form submission event in the route.

Copy codeThe 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('articles');  }

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['published_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.

Public function index () {// obtain the Article in reverse order // you can do this // $ articles = Article: orderBy ('hhed _ at', 'desc ') -> get (); // simple method. Of course, there is also oldest () $ articles = Article: latest ('hhed _ at')-> get (); return view ('articles. index', compact ('articles '));}

The above is all the content of this article, hoping to help you learn the Laravel5 framework.

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.