Laravel 5 Framework Learning sub-view and form reuse, Laravel framework
We need to deal with the issue of editing articles. Of course we can add new routes manually, just like this:
Copy the Code code as follows:
Route::get ('/articles/{id}/edit ', ' articlecontroller@edit ');
Let's use Artisan's route:list at the command line to see our current route:
Copy the Code code as follows:
PHP Artisan route:list
In a RESTful situation, it might be a good choice to use Laravel's resource route directly, but we'll remove all the routes and add only one:
Copy the Code code as follows:
Route::resource (' articles ', ' Articlescontroller ');
Using PHP artisan route:list to see the route again, wow, a bunch of routes that match our expectations have been generated. Take a closer look at each item.
Now add the method to the controller:
Public function edit ($id) { $article = Article::findorfail ($id); Return view (' Articles.edit ', compact (' article ')); }
Create a View Now
@extends (' layout ') @section (' content ')
...
OK, I admit that the code is copied from the create.blade.php, modified, the question is we need to repeat it? We'll take care of this later, and now we'll look at the form's commit problem. In the route PHP artisan route:list, and then look again, modified using the PATCH method, we can modify the view:
Copy the Code code as follows:
{!! Form::open ([' method ' = ' PATCH ', ' url ' = ' articles/'). $article->id])!
Visit/articles/1/edit in the browser, look at the source code, and discover that Laravel automatically generated the hidden fields of _method=patch.
The problem is that we edit the article, but the information in the article is not shown and we modify the view:
Copy the Code code as follows:
{!! Form::model ($article, [' method ' = ' PATCH ', ' url ' = ' articles/'. $article->id])!
Ok,everything's OK, except that the Published_on field is still set to the current date, which we'll deal with later.
Now add the method to the controller:
Public Function Update ($id, \illuminate\http\request $request) { $article = Article::findorfail ($id); $article->update ($request->all ()); Return redirect (' articles '); }
We also need validation in the process of modification, let's re-use our Request class, rename Createarticlerequest to a more general articlerequest, and don't forget to modify the parameters in the store method.
Public Function Update ($id, Requests\articlerequest $request) { $article = Article::findorfail ($id); $article->update ($request->all ()); Return redirect (' articles '); }
The remaining problem is that our new and edited ones use most of the same code, such as displaying errors, but there are two of them, so let's change the problem.
We create a new file list.blade.php directly below the Views/articles and copy the error handling code from the create.blade.php:
@if ($errors->any ())
@foreach ($errors->all () as $error)
- {{$error}}
@endforeach
@endif
In create.blade.php, you simply replace the error-handling code with the following statement:
Copy the Code code as follows:
@include (' articles.list ')
Let's deal with the form code, where the form code is not the same as the form, and the Commit button is different, the others are similar. We create a view articles/form_partial.blade.php and copy the code.
{!! Form::label (' title ', ' title: ') !! {!! Form::text (' title ', null, [' class ' = ' Form-control ']) ! {!! Form::label (' body ', ' body: ') !! {!! Form::textarea (' body ', null, [' class ' = ' Form-control ']) ! {!! Form::label (' Published_at ', ' Publish on: ') !! {!! Form::input (' Date ', ' Published_at ', date (' y-m-d '), [' class ' = ' Form-control ']) ! {{--the variable is set here, depending on whether it is edited or modified, or not placed in partial--}} {!! Form::submit ($submitButtonText, [' class ' = ' btn btn-primary form-control '])!
Modify create.blade.php
@extends (' layout ') @section (' content ') Write a New article
@include (' articles.list ') {{--use the illuminate\html Open Source Library We added--}}} {!! Form::open ([' url ' = ' articles ') ]! @include (' articles.form_partial ', [' submitbuttontext ' = ' Add article ']) {!! Form::close ()!!} @stop
Modify edit.blade.php
@extends (' layout ') @section (' content ')
@include (' articles.list ') {{--use the illuminate\html Open Source Library We added--}}} {!! Form::model ($article, [' method ' = ' PATCH ', ' url ' = ' articles/'. $article->id]) ! @include (' articles.form_partial ', [' submitbuttontext ' = ' Update article ']) {!! Form::close ()!!} @stop
The above is the article to introduce all the content, I hope to be able to master the LARAVEL5 framework to help you.
http://www.bkjia.com/PHPjc/981359.html www.bkjia.com true http://www.bkjia.com/PHPjc/981359.html techarticle Laravel 5 Framework Learning sub-view and form reuse, Laravel framework we need to deal with the issue of editing articles. Of course we can add new routes manually, just like this: Copy the code ...