The figure above shows an outline of the Laravel HTTP layer. At present, in my work, webpage, App and small program are implemented in the way of front and background separation. The front page obtains data from the back end through the interface and renders itself. Therefore, Session, view and CSRF protection in the outline will not be sorted out. Due to the limitation of HTTP layer content is too much, I will sort out routing, middleware, controller in this article. In the next installment, I’ll cover HTTP requests, responses, and form validation.

0 x00 routing

The basic routing

Building the most basic route requires only a URI and a closure. All Laravel routes are defined in the routing file in the Routes directory. Laravel provides two routing files by default: web.php and api.php. The former defines web page routing and applies web middleware group by default. The latter defines stateless routing and applies API middleware groups.

Laravel’s Route Facade provides routing methods such as GET, POST, PUT, Patch, DELETE, options, match, and any to support single, multiple, and all HTTP methods.

Routing parameters

Laravel can constrain the mandatory, optional, and specific formats of routing parameters in a variety of ways.

After routing

Laravel can name specified routes or controller methods, and generate urls for named routes.

Routing group

Laravel routing groups allow sharing of routing attributes. Common routing groups include middleware, namespaces, subdomain routing, and route prefixes.

Routing model binding

Laravel routing model bindings provide a convenient way to automatically inject models into routes, either implicitly or explicitly.

Implicit binding binds the ID field of the Eloquent model by default. The binding key name can be customized through the model’s getRouteKeyName method.

Explicit binding uses the route’s Model method to declare classes for existing parameters.

0x01 Routing Middleware

Laravel middleware provides a convenient mechanism to filter HTTP requests coming into the application. Think of middleware as a series of “layers” that HTTP requests must pass through to trigger your application. Each layer can detect incoming requests and even deny access to your application entirely.

Common examples of middleware include authentication middleware, logging middleware, CSRF middleware, etc. Multiple individual middleware can also be defined as middleware groups. Laravel comes with an out-of-the-box set of Web and API middleware.

0 x02 controller

The controller can organize the related request processing logic into a separate class. Is an alternative to defining all request processing logic in a closure form in a routing file.

Resource controller

Laravel resource routing can assign a typical CRUD route to a controller with only one line of code, which greatly improves the development efficiency of back-end services.

In the case of the photo controller, two steps define the service that handles all the photo HTTP requests saved by the application.

  1. Create a controller with a single command
  2. Register a resource route for this controller

The resource routing declaration creates multiple routes to handle the various resource operations that are contained in the controller.

In addition to providing the default actions, you can customize the actions as needed.

  1. You can define only partial resource routes
  2. You can override the default resource route name
  3. You can override the default parameter name of the resource route
  4. Language localization of resource URIs can be implemented
  5. Additional resource controllers can be attached as needed

Dependency injection and controllers

Laravel uses a service container to resolve all controllers. You can use type constraints on any dependency in the constructor of a controller, or you can use dependent type constraints in controller methods.

The next step

In the next installment, I’ll cover Laravel HTTP requests, responses, and form validation. These are the most important parts of any web application.

Mind maps will become blurred during the export process. If you need a high-definition mind map source file, please add the wechat official account: UP2048 and reply to “Brain Map” to obtain it.

– EOF –

Recommended reading

  • Laravel Mind mapping: Laravel core concepts
  • A beginner’s guide to Laravel Mind Mapping
  • 15 Mind maps to help you quickly learn the basics of PHP speech
  • What is the process of network interface design in my mind?
  • Design of multi-platform unified user system