如何最小化在同一控制器的 Laravel 函数中使用的 try..catch()

How to minimize try..catch() which is used in Laravel functions of the same Controller

我有这样的控制器:

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 变量。

现在为了重构它,我将 try..catch() 添加到 __construct() 函数:

       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());
           }
        }

但这会 return 一个 ErrorException:

Undefined variable: tron

指的是这一行:

$addressH = $tron->toHex($address);

所以问题是,如何通过最小化 try..catch() 并在函数中使用它而不会出错来编写更少的代码?

非常感谢你们的任何想法或建议...

提前致谢。

有一种方法可以帮助您重构代码以减少重复代码。

Laravel管道

您可以创建一个 class 并在该 class 的方法中添加您的逻辑,然后通过该 class 发送您的数据并在其中执行您的重复逻辑。

例如,为了使 $tron 在您喜欢的任何地方创建一个 class 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();