Analysis of subviews and form reuse in Laravel5 framework

不言
Release: 2023-04-01 06:42:02
Original
1172 people have browsed it

This article mainly introduces the use of subviews and form reuse in the Laravel5 framework. It is very detailed and comprehensive. It is very helpful for everyone to master the Laravel5 framework. Friends who need it can refer to it

We need to deal with the issue of editing articles. Of course we can add new routes manually, like this:

Route::get('/articles/{id}/edit', 'ArticleController@edit');
Copy after login

Let us use artisan’s route:list on the command line to view our current routes:

php artisan route:list
Copy after login

In line with RESTful In this case, it may be a good choice to directly use laravel's resource route. Then we will remove all the routes and only add the only one:

Route::resource('articles', 'ArticlesController');
Copy after login

Use php artisan route:list again to view the route, wow , a bunch of routes that meet our expectations are generated. Look at each item carefully.

Now add method in controller:

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

Now create view

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

Edit: {!! $article->title !!}


...
Copy after login

Okay, I admit that these codes were copied from create.blade.php and modified a bit. The question is do we need to repeat them? We will deal with this issue later, now let’s look at the form submission issue. In the routing, php artisan route:list, read it again, the modification uses the PATCH method, let's modify the view:

{!! Form::open(['method' => 'PATCH', 'url' => 'articles/' . $article->id]) !!}
Copy after login

Visit /articles/1/edit in the browser, check the source code, and find laravel The hidden field of _method=PATCH is automatically generated.

The first problem is that we edit the article, but the article information is not displayed. Let's modify the view:

{!! Form::model($article, ['method' => 'PATCH', 'url' => 'articles/' . $article->id]) !!}
Copy after login

OK, everything's ok, except that the published_on field is still set to the current date, and then We'll handle it.

Now add the method in the controller:

public function update($id, \Illuminate\Http\Request $request) { $article = Article::findOrFail($id); $article->update($request->all()); return redirect('articles'); }
Copy after login

We also need to verify during the modification process, let us reuse our Request class, rename CreateArticleRequest to the 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'); }
Copy after login

The remaining problem now is that our new and edit use most of the same code, such as displaying errors, but they exist in two copies, let's Modify this question.

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

@if ($errors->any()) 
    @foreach($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif
Copy after login

In create.blade.php, just replace the error handling code with the following statement:

@include('articles.list')
Copy after login

Let's deal with the form code again. The form code is different except that the form is different from the submit button. , others are almost the same. 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']) !!}

{{--这里要设置变量,依据是编辑还是修改来改变,当然也可以不放置在partial中--}} {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}

Copy after login

Modify create.blade.php

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

Write a New Article


@include('articles.list') {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open(['url' => 'articles']) !!} @include('articles.form_partial', ['submitButtonText' => 'Add Article']) {!! Form::close() !!} @stop
Copy after login

Modify edit.blade.php

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

Edit: {!! $article->title !!}


@include('articles.list') {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::model($article, ['method' => 'PATCH', 'url' => 'articles/' . $article->id]) !!} @include('articles.form_partial', ['submitButtonText' => 'Update Article']) {!! Form::close() !!} @stop
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Pages and Form Validation of Laravel 4

About Laravel framework database CURD operations and coherent operations Analysis

About the method of PHP framework Laravel plug-in Pagination to implement custom paging

The above is the detailed content of Analysis of subviews and form reuse in Laravel5 framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!