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) in one, perform data check and verification in Laravel. First, let's modify the route and 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('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.