【发布时间】:2020-03-05 05:05:35
【问题描述】:
我在 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,
];
并在页面中显示 Function name must be a string 错误, 如何在控制器中访问这个返回值?
【问题讨论】:
-
为什么要使用货币转换作为中间件。它应该用作助手
-
@Sehdev 不需要中间件,
标签: php laravel middleware