Php 如何最小化同一控制器的Laravel函数中使用的try..catch()
我有这样的控制器:Php 如何最小化同一控制器的Laravel函数中使用的try..catch(),php,laravel,constructor,try-catch,laravel-8,Php,Laravel,Constructor,Try Catch,Laravel 8,我有这样的控制器: class TronController extends Controller { public $tron; public function __construct(){ $this->fullNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io'); $this->solidityNode = new \IEXBas
class TronController extends Controller
{
public $tron;
public function __construct(){
$this->fullNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$this->solidityNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$this->eventServer = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
}
public function totalUsers()
{
try {
$tron = new \IEXBase\TronAPI\Tron($this->fullNode, $this->solidityNode, $this->eventServer);
$TransactionBuilder = new \IEXBase\TronAPI\TransactionBuilder($tron);
} catch (\IEXBase\TronAPI\Exception\TronException $e) {
exit($e->getMessage());
}
$address = ADDRESS;
$addressH = $tron->toHex($address);
$contract = CONTRACT;
$contractH = $tron->toHex($contract);
...
}
public function totalTickets()
{
try {
$tron = new \IEXBase\TronAPI\Tron($this->fullNode, $this->solidityNode, $this->eventServer);
$TransactionBuilder = new \IEXBase\TronAPI\TransactionBuilder($tron);
} catch (\IEXBase\TronAPI\Exception\TronException $e) {
exit($e->getMessage());
}
...
}
}
如您所见,在这个控制器的两个函数中,我重复了相同的try..catch()
设置$trone
变量
现在为了重构它,我在\uu construct()
函数中添加了try..catch()
:
public function __construct(){
$this->fullNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$this->solidityNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$this->eventServer = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
try {
$tron = new \IEXBase\TronAPI\Tron($this->fullNode, $this->solidityNode, $this->eventServer);
$TransactionBuilder = new \IEXBase\TronAPI\TransactionBuilder($tron);
} catch (\IEXBase\TronAPI\Exception\TronException $e) {
exit($e->getMessage());
}
}
但这将返回一个错误异常:
未定义变量:tron
指的是这一行:
$addressH=$tron->toHex($address)代码>
所以问题是,我如何通过最小化try..catch()
来编写更少的代码,并在函数中使用它而不会出错
我非常感谢你们的任何想法或建议
提前感谢。有一种方法可以帮助您重构代码,以减少重复代码
拉维尔管道
您可以创建一个类并在该类的方法中添加逻辑,然后通过该类发送数据并在其中执行重复逻辑
例如,为了使$tron
在您喜欢的任何地方创建一个类MakeTron
namespace App\Http\Controller;
class MakeTron
{
public function handle()
{
try {
return new \IEXBase\TronAPI\Tron($this->fullNode, $this->solidityNode, $this->eventServer);
} catch (\IEXBase\TronAPI\Exception\TronException $e) {
exit($e->getMessage());
}
}
}
在代码中只需使用管道
$tron = app(\Illuminate\Pipeline\Pipeline::class)
->through([\App\Http\Controller\MakeTron::class])
->thenReturn();
只创建一个包含try/catch的方法,从中阻止它并返回tron,然后从控制器操作方法调用它们。@biesior请你给我举个例子,我刚刚遇到了很多错误!或者将属性$tron
添加到类中,并通过$this->tron
在构造函数和操作中访问它,当您在控制器(如$tron
中使用它时,它只能在方法的范围内访问。编辑:正如我看到的,你已经有了它-public$tron代码>所以只需将$tron=new…
更改为$this->tron=new
在controler中,在actions中使用$this->tron
阅读它只是好奇,创建专用类的原因是什么,而OP可以通过构造函数中生成的属性来完成,他几乎拥有,只需要正确访问它吗?@BABAKASHRAFI我刚刚创建了一个控制器MakeTron
,并在公共函数totalUsers()
启动后添加了管道,但我得到的目标类[\App\Http\Controller\MakeTron]不存在。
@kkkkk我已经更新了答案<代码>->通过([\App\Http\Controller\MakeTron::class])
@biesior,是的,你是对的。我只是想用decorator方法给出一个解决方案。@biesior甚至不需要退出代码,因为laravel有内置的错误处理程序,可以对其进行自定义以反映不同异常的正确操作,或者只使用report()
而不是phpexit()
函数。