我在 laravel 中创建一个基于子域的货币转换器中间件,app/Http/Middleware/Currency.php这个中间件用于转换货币
namespace App\Http\Middleware;
use Closure;
class Currency
{
public function convert($request, Closure $next)
{
$sub=array_shift((explode('.', $_SERVER['HTTP_HOST'])));
$fromCurrency = "AED";
$toCurrency = "$sub";
$amount = "1";
$url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;
$get = file_get_contents($url);
$data = preg_split('/\D\s(.*?)\s=\s/',$get);
$exhangeRate = (float) substr($data[1],0,7);
$convertedAmount = $amount*$exhangeRate;
$data = array( 'exhangeRate' => $exhangeRate, 'convertedAmount' =>$convertedAmount, 'fromCurrency' => strtoupper($fromCurrency), 'toCurrency' => strtoupper($toCurrency));
return json_encode( $data );
}
}
并写在Kernel.php中
protected $middleware = [
\App\Http\Middleware\Currency::class,
];
并在页面中显示函数名称必须是字符串错误,如何在控制器中访问此返回值?
紫衣仙女
米琪卡哇伊