tp 阅读手册

1.依赖注入,将类以参数的形式传入方法中, 相当于在方法中 new Store();

namespace app\api\controller;
use model\ModelEntity as Store;
class Api{
  //依赖注入 Store,相当于在方式中已 $store = new Store();
  public function storeList(Store $store){
    $list = $store->getList();
  }
}

 

2.钩子和行为,通过行为可不改动框架应用的情况下,来改变/增加功能

绑定行为,定义行为入口方法run(),添加钩子

//1.在tags.php中绑定行为
'getStore' => 'app\http\behavior\StoreBehavior'

//2.也可动态绑定行为
Hook::add('getStore', 'app\http\behavior\StoreBehavior');

//添加钩子
Hook::listen('getStore');

 

3.extend扩展类库目录

注意文件命名空间,use引用

//类似extend/examine/Store.php文件的命名空间为 namespace examine;
//在其它类中 use 引用
use examine\Store;

$result = (new Store())->getList();

 

4.中间件,主要用于HTTP请求的数据过滤/权限检测

类似校验登录token

在路由注册中间件  Route::get('store/getList', 'admin/store/getList')->middleware(app\http\middleware\Auth::class);

//入口方法
public function handle($request, \Closure $next){
  $token = str_replace('Bearer ', '', $request->header('Authorization' ,'Bearer '));
  //..........
  //可向控制器传参
  $request->type = 1;
  $request->withPost(array_merge($request->post(), ['type' => 1]));

  //可重定向return redirect('home/login');
  return $next($request);
}

 

5.助手函数env(),获取环境变量  如env('database')

posted @ 2023-04-25 15:12  东方素  阅读(10)  评论(0编辑  收藏  举报