这里我直接在控制器中实现的 , 需要自己做一下类的抽取
这里参照的某框架的链式操作思想
思想是通过一个条件或者一个方法, 进行一个结构的判断
然后通过clone, 把这个类返回出去
达到链式操作的样子
/**
* @RequestMapping("/ger")
*/
public function ins(Request $request)
{
return $this->if($request->isGet())
->then(function (){
return "get请求";
})
->if($request->isPost())
->then(function (){
return "post请求";
})
->getRes();
}
private $bools; private $res;
private function if(bool $bool=false)
{
$this->bools = $bool;
return clone $this;
}
private function then(callable $fun)
{
if($this->bools){
$this->res = $fun();
}
return clone $this;
}
private function getRes()
{
return $this->res;
}