猿问

在 laravel 控制器中访问中间件

我在 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,

];

并在页面中显示函数名称必须是字符串错误,如何在控制器中访问此返回值?


慕姐4208626
浏览 138回答 2
2回答

紫衣仙女

为了达到您的目的,您应该使用 Helper 而不是 Middleware。根据 Laravel 文档中间件提供了一种方便的机制来过滤进入应用程序的 HTTP 请求。例如,Laravel 包含一个中间件,用于验证您的应用程序的用户是否经过身份验证。如果用户未通过身份验证,中间件会将用户重定向到登录屏幕。但是,如果用户通过了身份验证,中间件将允许请求进一步进入应用程序。您可以如下创建自定义 Helper 并在您的应用中的任何位置使用它第 1 步:创建您的 Currency Helpers 类文件并为其提供匹配的命名空间。编写你的类和方法:&nbsp; <?php // Code within app\Helpers\Currency.php&nbsp; &nbsp; namespace App\Helpers;class Currency{&nbsp; &nbsp; public static function convert($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $sub=array_shift((explode('.', $_SERVER['HTTP_HOST'])));&nbsp; &nbsp; &nbsp; &nbsp; $fromCurrency =&nbsp; "AED";&nbsp; &nbsp; &nbsp; &nbsp; $toCurrency = "$sub";&nbsp; &nbsp; &nbsp; &nbsp; $amount = "1";&nbsp; &nbsp; &nbsp; &nbsp; $url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;&nbsp; &nbsp; &nbsp; &nbsp; $get = file_get_contents($url);&nbsp; &nbsp; &nbsp; &nbsp; $data = preg_split('/\D\s(.*?)\s=\s/',$get);&nbsp; &nbsp; &nbsp; &nbsp; $exhangeRate = (float) substr($data[1],0,7);&nbsp; &nbsp; &nbsp; &nbsp; $convertedAmount = $amount*$exhangeRate;&nbsp; &nbsp; &nbsp; &nbsp; $data = array( 'exhangeRate' => $exhangeRate, 'convertedAmount' =>$convertedAmount,&nbsp; &nbsp; &nbsp; &nbsp;'fromCurrency' => strtoupper($fromCurrency), 'toCurrency' => strtoupper($toCurrency));&nbsp; &nbsp; &nbsp; &nbsp; return json_encode( $data );&nbsp; &nbsp; }}第 2 步:创建别名:<?php // Code within config/app.php&nbsp; &nbsp; 'aliases' => [&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; 'Currency' => App\Helpers\Helper::class,&nbsp; &nbsp; &nbsp;...第三步:composer dump-autoload在项目根目录下运行第 4 步:在控制器中像这样使用它<?php&nbsp;namespace App\Http\Controllers;use Currency;&nbsp;class SomeController extends Controller&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public function __construct()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Currency::convert($value);&nbsp; &nbsp; &nbsp; &nbsp; }

米琪卡哇伊

我不认为中间件是实现您想要的功能的最佳方式,但要使用中间件执行您要求的操作,中间件本身必须有一个名为handle而不是convert. 您也可以将结果闪存到您的会话中以在控制器内部访问它。还要注意句柄函数的返回,因为它是进程继续所必需的namespace App\Http\Middleware;use Closure;class Currency{&nbsp; &nbsp; public function handle($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $explodedArr = explode('.', $_SERVER['HTTP_HOST']);&nbsp; &nbsp; &nbsp; &nbsp; $sub = array_shift($explodedArr);&nbsp; &nbsp; &nbsp; &nbsp; $fromCurrency =&nbsp; "AED";&nbsp; &nbsp; &nbsp; &nbsp; $toCurrency = "$sub";&nbsp; &nbsp; &nbsp; &nbsp; $amount = "1";&nbsp; &nbsp; &nbsp; &nbsp; $url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;&nbsp; &nbsp; &nbsp; &nbsp; $get = file_get_contents($url);&nbsp; &nbsp; &nbsp; &nbsp; $data = preg_split('/\D\s(.*?)\s=\s/',$get);&nbsp; &nbsp; &nbsp; &nbsp; $exhangeRate = (float) substr($data[1],0,7);&nbsp; &nbsp; &nbsp; &nbsp; $convertedAmount = $amount*$exhangeRate;&nbsp; &nbsp; &nbsp; &nbsp; $data = array( 'exhangeRate' => $exhangeRate, 'convertedAmount' =>$convertedAmount,&nbsp; &nbsp; &nbsp; &nbsp;'fromCurrency' => strtoupper($fromCurrency), 'toCurrency' => strtoupper($toCurrency));&nbsp; &nbsp; &nbsp; &nbsp; session()->flash('convert_result', json_encode($data) );&nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; }}// and you should be able to get the result in your controller like sosession('convert_result');
随时随地看视频慕课网APP
我要回答