Subview and form reuse of Laravel5 framework learning

Source: Internet
Author: User
This article mainly introduces the sub-views in the Laravel5 framework and the usage of form reuse. it is very meticulous and helpful for you to master the Laravel5 framework, if you need it, you can refer to the problem that we need to edit the article. Of course, we can manually add new routes, just like this:

The 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:

The 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:

The 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')  Edit: {!! $article->title !!}   

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:

The 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:

The 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())  
    @foreach($errors->all() as $error)
  • {{ $error }}
  • @endforeach

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

The 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.

{!! Form: label ('title', 'Title :')!!} {!! Form: text ('title', null, ['class' => 'form-control'])!}

{!! Form: label ('body', 'body :')!!} {!! Form: textarea ('body', null, ['class' => 'form-control'])!}

{!! Form: label ('Ed Hed _ at', 'publish On :')!!} {!! Form: input ('date', 'hhed _ at', date ('Y-m-D '), ['class' => 'form-control'])!}

{-- Here we need to set variables based on editing or modification. of course, they can also be left 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') Edit :{!! $ Article-> title !!}
 @ 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 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: 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.