Originally from: Https://jellybool.com/post/programming-with-laravel-5-laravel-forms-input
Before we start, let's just beautify the interface a little bit first:
First get the static file to Https://github.com/JellyBool/blog-css-js, then modify the following three files separately:
1. App.blade.php2. Articles/index.blade.php3. articles/show.blade.php
The following view code modifies the section, and if you are lazy, you can use CTRL + C DAFA.
In app.blade.php: Replace the original @yield (' content ') code with the following code:
@yield (' content ')
is to add a div and a section outside.
Then introduce these two CSS files:
One is bootstrap, one is custom.
In the articles/index.blade.php file, we put each $article in the label:
@foreach ($articles as $article) ID}} "> {{$article->title}}
{{$article->intro}} ID}} "> more @endforeach
Then the last thing to do is modify the articles/show.blade.php view file:
@section (' content ') ID}} "rel=" bookmark "> {{$article->title}}
{{$article->content}} @endsection
Finally look at the effect:
At the end of the tutorial, you can basically complete a small product like my blog.
OK, after a little landscaping, we can get into our theme: Use forms form in laravel.
Prelude
Now that we need to create an article, we first need to show the page that created the article, just like the SF article-writing page: Http://segmentfault.com/write
So we need to go through the contents of the previous article:
To register the route, add in routes.php:
Route::get (' article/create ', ' articlecontroller@create ');
We specify article/create to load the Articlecontroller's Create () method, and then we create it in Articlecontroller:
Public function Create () { return view (' Articles.create '); }
This create () method loads directly into our create.blade.php, so we created this view file, went to the previous views/articles/folder, created a new create.blade.php, and wrote these test contents:
@extends (' app ') @section (' content ') Write a new article
@endsection
Browser Access Try Http://blog.dev/article/create:
After everything is loaded correctly, we begin to work on our forms use, because when we create the article is the form of the submission, we can receive the content, or almost every Web application is inseparable from the form, even if it is a registration, login page, also requires the existence of the form, As long as you need to collect user information or want to have some UGC, you can not leave the form. So let's start using Laravel's forms form.
Using illuminate/html
Here we use an official package:https://github.com/illuminate/html.
We install it through composer:
Composer require illuminate/html
Wait a minute, after the installation is successful, how do we tell Laravel that the package is installed? Or how do we combine this package with Laravel's entire system?
By providing service provider and specifying facade! This makes it perfect to combine with the laravel.
Why call service provider and facade directly? Because I know how to translate these two to be appropriate, to understand a bit
Here's a little bit more: in PHP's many composer package, there will be different versions of the framework, such as Htmlpurifier, the filter HTML and anti-XSS package, there is this laravel version:
Https://github.com/mewebstudio/Purifier, more or less, some very good package will have laravel version.
Configuration
Then back to service provider and facade, it may be confusing to start with the concept of service provider, but you have absolutely no need to worry about it, although open the config/app.php file for a look:
' Providers ' = [ illuminate\foundation\providers\artisanserviceprovider::class, illuminate\auth\ Authserviceprovider::class, Illuminate\broadcasting\broadcastserviceprovider::class, //... ]
You will see providers this array will have a bunch of laravel pre-set service Provider, such as we often use to Auth,controller, etc., can be found here. If you pull down again, you'll see a aliases array like this:
' Aliases ' = [ ' App ' = illuminate\support\facades\app::class, ' Artisan ' and ' = illuminate\ Support\facades\artisan::class, ' Auth ' = Illuminate\support\facades\auth::class, //... ]
Aliases is actually a shortcut, once here to specify a shortcut, we can be used globally in laravel, such as I use in code Auth, in fact, I was in the Illuminate\support\facades\auth::class , and then deep down, we are actually using providers in the Illuminate\auth\authserviceprovider::class this.
While the above is written, illuminate/html has been downloaded:
So, in the way above, let's configure our illuminate/html, add our service Provider in config/app.php providers:
Illuminate\html\htmlserviceprovider::class,
The configuration is probably long like this:
After Ok,service provider is added, let's add our facade, which is added after the value of aliases:
' Form ' = Illuminate\html\formfacade::class,
After configuration, look at the picture as long as this:
Using forms
After these two configurations are ready, we can use it in our create.blade.php view:
@section (' content ') Write a new article
{!! Form::open ()!!} {!! Form::close ()!!} @endsection
We have joined two lines {!!! Form::open ()!!} and {!!! Form::close ()!!}, as for {!! Similar symbols, such as blade {{, do not have to be too entangled in this. Let's revisit Http://blog.dev/article/create to try:
At first glance, there seems to be no change, but you have to look at the page source code or use the Developer tool to check the elements, you can find that the form element has been created.
It is not difficult to find that the Laravel form also defaults to us generating a hidden form (name= "_token"), which is a little security support for the form submission by default Laravel. When the form is submitted, Laravel will automatically check whether the _token is consistent with the _token stored in the session, and if not, jump back to the far page and not allow us to submit the data.
Now that the form is working, we can create the form we need:
{!! Form::open ()!!} {!! Form::label (' title ', ' title: ') ! {!! Form::text (' title ', null,[' class ' = ' Form-control ')) ! {!! Form::label (' content ', ' text: ') !! {!! Form::textarea (' content ', null,[' class ' = ' Form-control ')) !! {!! Form::submit (' published article ', [' class ' = ' btn btn-success form-control ']) ! {!! Form::close ()!!}
We are in {!!! Form::open ()!!} To add a little something, first look at our effect:
Here is a detailed explanation:
{!! Form::text (' title ', null,[' class ' = ' Form-control '))!
Take this to the axe:
Form::text says, as well as a bunch ofAnd so you can write it by reference.
' title ' means name= ' title '
Null means value= '
' Class ' = ' Form-control ' represents class= ' Form-control ', where you can specify a series of properties that you want to specify, such as Id,placeholder
However, when Form::open () does not specify a commit path, the default is to submit to this page, so that our clear division of labor is not very good, because this page is used to load the view, and for the content of our form submission, we want to use another method to deal with, so let's write it.
First in Form::open () specify the URL of the form submission, directly at the Add URL:
{!! Form::open ([' url ' = ' Article/store ')]!
We specify the form post to submit to article/store this address, and then register this routing address in routes.php:
Route::p ost (' Article/store ', ' articlecontroller@store ');
Note here that we used the route::p OST instead of Route::get, which is the route used to receive the post . Then smoothly Cheng Zhang, create the store () method in Articlecontroller:
Public function Store () { $input = Request::all (); return $input; }
In this method, we introduce Laravel's request and use Request::all () to get all the user-submitted content (in this case: _token,name is very content), if you want to get a specific form input, You can use Request::get (), such as Request::get (' title '), and then return directly to see what the user actually entered, let's try it:
Implement create a new article
OK, after successfully getting the content of the user's submission, we need to save these to the database, how to implement it? In the fourth chapter, the eloquent create () method we've mentioned can now come in handy, so we can write this:
Article::create ($input);
Laravel will automatically filter _token this submission.
But after creating an article, we do not want to return $input, but want to re-jump to a page, such as our homepage, because after the post, we need to see whether it successfully line up in the article list, so we finally write:
Public function Store () { $input = Request::all (); Article::create ($input); Return redirect ('/'); }
We directly use Laravel's redirect () function to jump to the first page. This time, just three lines of code will be able to implement our logic, then let's try:
Looks like a success? But we think this sort of a problem, the latest creation of the article is of course on the top, so we go to Articlecontroller's index () method slightly modified:
Public Function Index () { $articles = article::latest ()->get (); Return view (' Articles.index ', compact (' articles ')); }
Replace the original all () with latest ()->get (), Refresh,
We found that the latest article intro unexpectedly blank, we go to the database to see:
We found that the intro of the article we just created was a null value, and Published_at was 0000-00-00 00:00:00, which is not what we want, why is it so? Because we did not have these two data at the time of submission, in order to solve this problem, first we can be very violent to use these two data before using Article::create ($INPUT) configured, such as:
Public function Store () { $input = Request::all (); Add two lines below, and look at the use of Request::get $input [' intro '] = Mb_substr (request::get (' content '), 0,64); $input [' published_at '] = Carbon::now (); Article::create ($input); Return redirect ('/'); }
The intro field takes the first 64 of the Content field directly, and then the Published_at defaults to the time it was created. The code is a bit violent at the moment, but it's a solution, and we'll make the code much cleaner later. Let's try it again:
Bang, success, we're looking at the database:
Here, the basic process of creating an article is complete, but here's another question, if you're trying to create a page in the article without filling it out, submitting the data directly, you see what happens, if you're not sure, you can look at your database and what's going on.
Next section
Given the final question and the contents of the form, the next section seems to be a logical one to say that the form is validated, and in Laravel it is the request Validation.
At last
Happy Hacking