Laravel 5 Framework Learning Form _php Example

Source: Internet
Author: User
Tags compact

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

Copy Code code as follows:

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

Then modify the controller

Copy Code code as follows:

Public Function Create () {
Return view (' Articles.create ');
}

We return to a view and create this view. We can of course use HTML directly to create a form, but we have a better way to function. We use an open source library, Jeffrey Way developed illuminate\html. Install dependent libraries:

Copy Code code as follows:

Composer require illuminate/html

Laravel libraries need to be registered to Laravel for use. In config/app.php, we can see the provider field provided by Laravel, which describes the Laravel library functionality. In Laravel Framewirk Service Providers ... Finally add our new Htmlprovider

Copy Code code as follows:

' Illuminate\html\htmlserviceprovider ',

We do not want to use illuminate\html\fromfacade so long name to introduce, we need a short name. Locate the aliases segment in the current app.php and add the alias at the end.

Copy Code code as follows:

' Form ' => ' Illuminate\html\formfacade ',
' Html ' => ' Illuminate\html\htmlfacade ',

OK, now we're going to create the view, views/articles/create.blade.php

@extends (' layout ')

@section (' content ')
   
 

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

  Public function Show ($id) {
    dd (' show ');
    
    $article = Article::findorfail ($id);

    Return view (' Articles.show ', compact (' article '));
  }


Yes, you're right. Add the DD () method to the Show method, which simply prints out a message and dies. We'll visit/articles/create again, you see what you see out of the show.

Why did we access the create results route to show us? Let's take a look at the route and what's going on.

Copy Code code 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} means that this is a wildcard, everything behind the articles/will match, you know? Our/articles/create was also matched by him. Omg!

The solution is to adjust the order:

Copy Code code as follows:

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

That is, from special to normal, in the future routing settings should always pay attention to this problem. Now we're on a visit to articles/create everything OK.

Look at the source code in the browser, you will find not only generated method and action at the same time generated a hidden _token field as the server to verify the form, to avoid hackers forgery attacks.

Let's modify our view to add fields:

@extends (' layout ')

@section (' content ')
   
 


When the form is submitted, it is actually submitted to the articles/create using the Post method, but according to RESTful's custom, we want to be able to post to/articles, we modify the view's form method, and set the path of the submission.

Copy Code code as follows:

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

Then we handle the form submission event in the route.

Copy Code code as follows:

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

We'll handle the controller.

Note: Remove the USE statement below, and we are using the Request//use app\http\requests\request in the façade interface
;

Introduce the Request use Illuminate\support\facades\request in the following namespaces
;

  Public function Store () {
    //Use Illuminate\html\request to return all form input fields
    $input = Request::all ();

    Let's go straight back to $input and take a look at return
    $input;
  }

We can see the JSON results of the input form directly. If you want only the value of the Title field, you can use Request::get (' Titel ').

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

Article::create ($input);

It's so simple, it's so willful

If you haven't forgotten Mass assignment, we've defined $fillable arrays in our model to define which fields can be directly populated directly when you create them.

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 ');
  }

Add a record try, very good. But don't forget. We also have a field called Published_at, let's deal with it.

  Public function Store () {
    $input = Request::all ();
    $input [' published_at '] = Carbon::now ();

    Article::create ($input);
    
    Return redirect (' articles ');
  }

Add a new record to test.

There is also a problem, the newly added should be shown at the front, we will modify the following controller.

 Public Function Index () {
    //reverse Access Article
    //can be this
    //$articles = Article::orderby (' published_at ', ' desc ')->get ();
    The simple way, of course, is oldest ()
    $articles = article::latest (' published_at ')->get ();

    Return view (' Articles.index ', compact (' articles '));
  }

The above mentioned is the whole content of this article, hope to be able to learn Laravel5 frame to be helpful to everybody.

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.