PHP TP framework first do not say how the security or performance, that function is indeed more powerful, and the threshold is much lower than other frameworks, do some private projects, it is a magic tool ah, recently to do a small work, using this framework, but also read the manual while doing while learning.

Middleware, injection and dependency, facade patterns, etc., were used in development. This article briefly describes a few things learned in development, as a learning record of my own.

Middleware is not new and advanced, there are many frameworks that have similar implementations, such as YII behavior and interceptors. I think it’s more about decoupling the business from some logic, making the whole business clearer, so you don’t have to write a bunch of code in one place.

For example, if you want to control log writing, permission authentication, source judgment, whitelist, return Json, etc., you can write in different middleware, avoiding the code to write in one place to judge, can also reuse the code, so, appropriate use of middleware to write, or quite cool.

In TP, there are several different types of middleware, such as global middleware, application middleware, routing middleware, and controller middleware, which are executed in the same order as above.

Global middleware

Middleware defined in app/middleware.php

return [
    // Session initialization
     \think\middleware\SessionInit::class
];
Copy the code

Application middleware

This is defined under my app, so if I have an API under my app directory, then I’ll define it under app/ API /middleware.php

return [
    \app\api\middleware\Log::class
];
Copy the code

Routing middleware

Middleware can be registered when a route is registered, and the corresponding middleware is executed when the route is executed.

 Route::post('create-order'.'app\\api\\controller\\order\OrderPay@createOrder')->middleware(app\\middleware\Auth::class);
Copy the code

Controller middleware

Define the $Middleware property in the controller, as in the example, and middleware is executed when index control is accessed



class Index extends FrontController
{

 protected $middleware = [ \app\admin\middleware\CheckAdmin::class];   public function index(a)  {  echo 'test';  } } Copy the code

routing

This thing is very good ah, you can flexibly control the URL address display, but is to hide the real address, beautify the address can be easily realized, for example, I now this project, because to compatible with the previous address, when the time to migrate just want to change the domain name on the line, so need to make the path into the same.

It used to be the project. NET, and the URL inside of it is v1,v2, this kind of thing is very easy in NET, you just configure an annotation.

Unexpectedly, this is also easy to implement in TP. Just configure the route.

Configure the routing

/route/[app] / app.php/app /[app] / app.php /route/[app] / app.php

Configure the route definition in /route/ API /app.php

use think\facade\Route;

Route::group('hpay'.function(a){
    Route::post('create-order'.'app\\api\\controller\\order\OrderPay@createOrder');
    Route::post('query-order'.'app\\api\\controller\\order\OrderQuery@queryOrder');
}); Copy the code

Add an app mapping to config/app.php

    'app_map'= > [        Env::get('htpay.version'.'v1') = >'api'// Mapping is the mapping of path v1 into the API application.].Copy the code

If you access http:// domain name /v1/hpay/create-order v1 is mapped to the API application, hpay/create-order is mapped to the configured controller, perfect implementation, code doesn’t need to change anything.

Post :: POST: Route:: POST: Route:: POST: Route:: POST: Route:: POST: Route:: POST: Route:: POST: Route:: POST

Ok, so this is some of the things that I used during the development, and I’ll keep writing if THERE’s anything I need to write down as I go along.