Laravel's learning process from entry to output

Source: Internet
Author: User

Laravel's learning process from entry to output

This article mainly introduces the content about Laravel's process from entry to output, and shares it for your reference. I will not talk about it much below. Let's take a look at the detailed introduction.

I. Prerequisites

Laravel directory

/path/to

Laravel

Web directory

Laravel/public

Web

Portal File

Web/index.php

II. Detailed process description

Example: http://la.com/test/yueshu/female/20? Name = chenxuelong

1. Define web Routing

File Path:

Laravel/routes/web.php

Modify as follows:

Route::get('/test/{name}/{sex}/{age}' , 'TestController@test')

2. Define Middleware

2.1 middleware path:

Laravel/app/Http/Middleware

2.2 create a middleware Test. php:

Namespace App \ Http \ Middleware; use Closure; class Test {/*** must return a response (response) */public function handle ($ request, Closure $ next) {// what to do before processing the request... $ response = $ next ($ request); // return $ response after processing the request;}/*** after the middleware completes processing, what to do */public function terminate ($ request, $ response) {// what to do after the middleware completes processing ....}}

2.3 Register Middleware

Middleware accessory file path:

Laravel/app/Http/Kernel.php

Added under the routeMiddleware key:

'Test' => \App\Http\Middleware\Test::class

4. Form Verification (request)

File storage directory:

Laravel/app/Http/Requests

Create FormValidate. php verification class:

/*** The verification fails. use referer in the header to redirect to * Previous Page */namespace App \ Http \ Requests; use Illuminate \ Foundation \ Http \ FormRequest; class FormValidate extends FormRequest {// whether authorized access to public function authorize () {return true;} // verify the public function rules () Rule () {/*** required indicates that the maximum length of * max: 255 is required. */return ['name' => 'required | max: 255 '];}

5. Model

File Path:

Laravel/app

Create model Test. php

Namespace App; use Illuminate \ Database \ Eloquent \ Model; class Test extends Model {// obtain all data of a table (query constructor) public function get ($ table) {\ DB: table ($ table)-> get () ;}// obtain (native SQL) public function select ($ SQL, $ params) {\ DB :: select ($ SQL, $ params);} // insert (native SQL) public functioin insert ($ SQL, $ params) {\ DB: insert ($ SQL, $ params);} // update (native SQL) public function update ($ SQL, $ params) {\ DB: update ($ SQL, $ params );} // delete (native SQL) public function delete ($ SQL, $ params) {\ DB: delete ($ SQL, $ params );} // execute the common SQL (native SQL) public function statement ($ SQL, $ params) {\ DB: statement ($ SQL, $ params );} // transaction public function transaction ($ SQL, callable $ callback) {\ DB: transaction ($ SQL, $ callback) :}// manually enable transaction public function beginTransaction () {\ DB: beginTransaction () ;}// manually roll back public function rollBack () {\ DB: rollBack () ;}// submit public function commit () {\ DB: commit ();}}

6. Controller

Controller path:

Laravel/app/Http/Controllers

New controller TestController. php

Namespace App \ Http \ Controllers; use App \ Http \ Controllers \ Controller; // introduces the request verification class use App \ Http \ Requests \ FormValidate;/* optional injection model class (layered) * A Test model class */use App \ Test; class TestController extends Controller {// Constructor (middleware or injection can be added) function _ construct (Test $ test) is introduced here) {// Add the previously registered middleware $ this-> middleware ('test'); // inject the model class $ this-> Test = $ test ;} /*** the action name set in the route * is prompted by the type to introduce request verification */public function test (FormValidate $ request, $ name, $ sex, $ age) {// verification request ..... $ data = []; if (view ()-> exists ('test') {view ('test', $ data );} else {exit ('template file does not exist .... ');}}}

7. View

View path:

Laravel/resource/views

Create a test. blade. php View File

<H1> Laravel completes parsing from entry to output! </H1>

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.