Laravel 5 framework learning sub-view and form reuse, laravel framework

Source: Internet
Author: User

Laravel 5 framework learning sub-view and form reuse, laravel framework

We need to solve the issue of editing the article. Of course, we can manually add new routes, just like this:

Copy codeThe Code is as follows:
Route: get ('/articles/{id}/edit', 'articlecontroller @ edit ');

Let's use the route: list of artisan in the command line to view our current route:

Copy codeThe Code is as follows:
Php artisan route: list

In the case of RESTful, it is a good option to directly use the resource routing of laravel. However, we can remove all the routes and add only one of them:

Copy codeThe Code is as follows:
Route: resource ('articles', 'articlescontroller ');

Use php artisan route: list to view routes again. Wow, a bunch of routes meet our expectation. Check each item carefully.

Now add the method to the Controller:

  public function edit($id) {    $article = Article::findOrFail($id);    return view('articles.edit', compact('article'));  }

Create view now

@extends('layout')@section('content')  

Okay, I admit that these codes are all copied from create. blade. php. I modified them. The question is, do we need to repeat them? We will solve this problem later. Now let's take a look at the form submission problem. In the route, php artisan route: list. Let's repeat it again and modify the view by using the PATCH method:

Copy codeThe Code is as follows:
{!! Form: open (['method' => 'patch ', 'url' => 'articles/'. $ article-> id])!}

Access/articles/1/edit in the browser and check the source code. laravel automatically generates the _ method = PATCH hidden field.

First, we edit the article, but the article information is not displayed. Let's modify the View:

Copy codeThe Code is 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 will be processed 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 to verify the modification process. Let's reuse our Request class and rename CreateArticleRequest to a more general ArticleRequest. 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 most of the same code is used for creation and editing. For example, an error is displayed, but there are two copies of the Code, so we can modify the problem.

We directly create the file list. blade. php under views/articles and copy the error handling code from create. blade. php:

@if ($errors->any())  <ul class="alert alert-danger">    @foreach($errors->all() as $error)      <li>{{ $error }}</li>    @endforeach  </ul>@endif

In create. blade. php, you only need to replace the error handling code with the following statement:

Copy codeThe Code is as follows:
@ Include ('articles. list ')

Let's process the form code again. Except the form code, the form code is slightly different from the submit button, and the others are similar. Create a view articles/form_partial.blade.php and copy the code.

<Div class = "form-group"> {!! Form: label ('title', 'title :')!!} {!! Form: text ('title', null, ['class' => 'form-control'])!} </Div> <div class = "form-group"> {!! Form: label ('body', 'body :')!!} {!! Form: textarea ('body', null, ['class' => 'form-control'])!} </Div> <div class = "form-group"> {!! Form: label ('ed hed _ at', 'Publish On :')!!} {!! Form: input ('date', 'hhed _ at', date ('Y-m-d '), ['class' => 'form-control'])!} </Div> <div class = "form-group" >{{ -- set the variable here based on whether it is edited or modified, of course, it can also not be placed in partial --}}{!! Form: submit ($ submitButtonText, ['class' => 'btn btn-primary form-control'])!} </Div>

Modify create. blade. php

@ Extends ('layout ') @ section ('content') 

Modify edit. blade. php

@ Extends ('layout ') @ section ('content') 

The above is all the content introduced in this article, hoping to help you master 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.