[英]Access middleware in laravel controller
我正在基於子域明智在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,
];
並顯示函數名稱在頁面中也必須是字符串錯誤,如何在控制器中訪問此返回值?
為了實現您的目的,您應該使用 Helper 而不是 Middleware。 根據 Laravel 文檔
中間件提供了一種方便的機制來過濾進入應用程序的 HTTP 請求。 例如,Laravel 包含一個中間件,用於驗證您的應用程序的用戶是否已通過身份驗證。 如果用戶未通過身份驗證,中間件會將用戶重定向到登錄屏幕。 但是,如果用戶通過了身份驗證,中間件將允許請求進一步進入應用程序。
您可以按如下方式創建自定義 Helper 並在您的應用程序中的任何位置使用它
第 1 步:創建您的 Currency Helpers 類文件並為其提供匹配的命名空間。 編寫您的類和方法:
<?php // Code within app\Helpers\Currency.php
namespace App\Helpers;
class Currency
{
public static 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 );
}
}
第 2 步:創建別名:
<?php // Code within config/app.php
'aliases' => [
...
'Currency' => App\Helpers\Helper::class,
...
第 3 步:在項目根目錄中運行composer dump-autoload
第 4 步:在控制器中像這樣使用它
<?php
namespace App\Http\Controllers;
use Currency;
class SomeController extends Controller
{
public function __construct()
{
Currency::convert($value);
}
我不認為中間件是實現您想要的功能的最佳方式,而是使用中間件來完成您的要求,中間件本身必須有一個名為handle
而不是convert
的函數。 您也可以將結果閃存到您的會話中以在控制器內訪問它。
還要注意句柄函數的返回,因為它是進程繼續所必需的
namespace App\Http\Middleware;
use Closure;
class Currency
{
public function handle($request, Closure $next)
{
$explodedArr = explode('.', $_SERVER['HTTP_HOST']);
$sub = array_shift($explodedArr);
$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));
session()->flash('convert_result', json_encode($data) );
return $next($request);
}
}
// and you should be able to get the result in your controller like so
session('convert_result');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.