Analysis of subviews and form reuse in Laravel5 framework
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');
Let us use artisan’s route:list on the command line to view our current routes:
php artisan route:list
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');
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')); }
Now create view
@extends('layout') @section('content') <h1 id="Edit-nbsp-nbsp-article-title-nbsp-nbsp">Edit: {!! $article->title !!} </h1> <hr/> ...
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]) !!}
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]) !!}
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'); }
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'); }
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()) <ul class="alert alert-danger"> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> @endif
In create.blade.php, just replace the error handling code with the following statement:
@include('articles.list')
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
<p class="form-group"> {!! Form::label('title', 'Title:') !!} {!! Form::text('title', null, ['class' => 'form-control']) !!} </p> <p class="form-group"> {!! Form::label('body', 'Body:') !!} {!! Form::textarea('body', null, ['class' => 'form-control']) !!} </p> <p class="form-group"> {!! Form::label('published_at', 'Publish On:') !!} {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!} </p> <p class="form-group"> {{--这里要设置变量,依据是编辑还是修改来改变,当然也可以不放置在partial中--}} {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!} </p>
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
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
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools