当前位置: 首页 > 后端技术 > PHP

2022-8-29TP6服务基本使用

时间:2023-03-29 14:35:16 PHP

服务1.什么是服务?你可以向容器注册一个对象作为系统服务;服务也用于绑定类。二、服务示例(一)前期准备1、路由//服务测试Route::get('myservice','TestServiceController/index');2.Controller(1)TestServiceController控制器使用命令创建测试服务Controller:phpthinkmake:controllerTestServiceController(2)TestController控制器使用命令创建绑定到容器的控制器:phpthinkmake:controllerTestController添加helloTestController控制器方法:";}}(2)TestService服务类使用命令创建一个TestService服务类:phpthinkmake:serviceTestService在TestService类的register方法中将TestController控制器和User模型绑定到容器中。boot方法在所有系统服务注册完成后被调用,用于定义启动某个系统服务前需要完成的操作。app->bind('测试',\app\controller\TestController::class);}/***执行服务**@returnmixed*/publicfunctionboot(){echo'启动该服务前需要完成的操作'."
";}}(3)修改TestServiceControllercontroller你好('月亮');}}(4)测试调用接口,结果如下:参考service-exampleservice-understandingdocument-service