访问 laravel 控制器中的中间件
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,
];
并在页面中显示 Function name must be a string 错误,
如何在控制器中访问此 return 值?
为了实现您的目的,您应该使用 Helper 而不是 Middleware。根据 Laravel 文档
Middleware provide a convenient mechanism for filtering HTTP requests
entering your application. For example, Laravel includes a middleware
that verifies the user of your application is authenticated. If the
user is not authenticated, the middleware will redirect the user to
the login screen. However, if the user is authenticated, the
middleware will allow the request to proceed further into the
application.
您可以创建一个自定义助手,如下所示,并在您的应用中的任何地方使用它
第 1 步: 创建您的 Currency Helpers class 文件并为其提供匹配的命名空间。写下你的 class 和方法:
<?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,
...
第三步: 运行 composer dump-autoload
在项目根目录
第 4 步:在您的控制器中像这样使用它
<?php
namespace App\Http\Controllers;
use Currency;
class SomeController extends Controller
{
public function __construct()
{
Currency::convert($value);
}
我不认为中间件是实现您想要的功能的最佳方式,但是要使用中间件执行您要求的操作,中间件本身必须有一个名为 handle
的函数,而不是 convert
.您也可以将结果闪存到您的会话中以在控制器内访问它。
还要注意 handle 函数的 return,因为它是进程继续所必需的
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');
我在 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 错误, 如何在控制器中访问此 return 值?
为了实现您的目的,您应该使用 Helper 而不是 Middleware。根据 Laravel 文档
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.
您可以创建一个自定义助手,如下所示,并在您的应用中的任何地方使用它
第 1 步: 创建您的 Currency Helpers class 文件并为其提供匹配的命名空间。写下你的 class 和方法:
<?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,
...
第三步: 运行 composer dump-autoload
在项目根目录
第 4 步:在您的控制器中像这样使用它
<?php
namespace App\Http\Controllers;
use Currency;
class SomeController extends Controller
{
public function __construct()
{
Currency::convert($value);
}
我不认为中间件是实现您想要的功能的最佳方式,但是要使用中间件执行您要求的操作,中间件本身必须有一个名为 handle
的函数,而不是 convert
.您也可以将结果闪存到您的会话中以在控制器内访问它。
还要注意 handle 函数的 return,因为它是进程继续所必需的
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');