在 laravel 中设置默认中间件
Set default middleware in laravel
我是 Laravel Framework 的新手。我想设置一个将在所有 HTTP 请求中调用的中间件。我如何设置中间件?
使用全局中间件。来自 the docs:
If you want a middleware to run during every HTTP request to your application, list the middleware class in the $middleware
property of your app/Http/Kernel.php
class.
如果您希望中间件在对您的应用程序的每个 HTTP 请求期间 运行,请在您的 app/Http/Kernel.php class 称为全局中间件.
举个例子
创建中间件
我们可以使用 artisan 轻松创建新的中间件。
php artisan make:middleware AdMiddleware
在我们创建了一个新的中间件组件之后,我们需要考虑修改代码以满足我们的需要。
更新我们的中间件文件:
在你 运行 执行 make:middleware 命令后,你应该会在 app/http/middleware 中看到你的新中间件文件。打开它,我们将创建一个中间件,它将获取请求的 IP 地址,然后确定该请求来自哪个国家/地区。
<?php
namespace App\Http\Middleware;
use Closure;
class AdMiddleware {
/** * Handle an incoming request.
* * * @param \Illuminate\Http\Request $request
* * @param \Closure $next * @return mixed */
public function handle($request, Closure $next) { // Test to see if the requesters have an ip address.
if ($request->ip() == null) {
throw new \Exception("IP ADDRESS NOT SET");
} $country = file_get_contents('http://api.hostip.info/get_html.php?ip=' . $request->ip());
echo $country;
if (strpos($country, "UNITED STATES")) {
throw new \Exception("NOT FOR YOUR EYES, NSA");
} else {
return redirect("index");
} return $next($request);
}
}
此代码基本上接受请求,并作为示例检查它的位置,然后再决定是显示仅限美国的广告还是适合世界其他地区的广告。对于那些想要建立一个具有来自多个国家/地区的亚马逊联盟链接的网站的人来说,这可能非常有益。
请注意,您不应在中间件部分传回任何内容,而应将重定向传递给视图,而不是像我为简洁起见所做的那样打印出内容。
正在注册您的中间件
注册中间件时,您有 2 个选择。第一个选择是在您的应用程序处理的每个请求上添加中间件 运行。您可以通过打开 App\Http\Kernel.php 并将其添加到您的 $middleware 数组来做到这一点,如下所示:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
// our new class.
\App\http\Middleware\AdMiddleware::class,
];
第二种选择是仅在已注册的路由上使用中间件 运行,您可以这样注册它:
<?php
/** * The application's route middleware.
* * * @var array */
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'ad' => \App\Http\Middleware\AdMiddleware::class,
];
然后像这样将中间件添加到特定的路由中:
Route::get('/ip', ['middleware' => 'ad', function() { return "IP"; }]);
我是 Laravel Framework 的新手。我想设置一个将在所有 HTTP 请求中调用的中间件。我如何设置中间件?
使用全局中间件。来自 the docs:
If you want a middleware to run during every HTTP request to your application, list the middleware class in the
$middleware
property of yourapp/Http/Kernel.php
class.
如果您希望中间件在对您的应用程序的每个 HTTP 请求期间 运行,请在您的 app/Http/Kernel.php class 称为全局中间件.
举个例子
创建中间件
我们可以使用 artisan 轻松创建新的中间件。
php artisan make:middleware AdMiddleware
在我们创建了一个新的中间件组件之后,我们需要考虑修改代码以满足我们的需要。
更新我们的中间件文件:
在你 运行 执行 make:middleware 命令后,你应该会在 app/http/middleware 中看到你的新中间件文件。打开它,我们将创建一个中间件,它将获取请求的 IP 地址,然后确定该请求来自哪个国家/地区。
<?php
namespace App\Http\Middleware;
use Closure;
class AdMiddleware {
/** * Handle an incoming request.
* * * @param \Illuminate\Http\Request $request
* * @param \Closure $next * @return mixed */
public function handle($request, Closure $next) { // Test to see if the requesters have an ip address.
if ($request->ip() == null) {
throw new \Exception("IP ADDRESS NOT SET");
} $country = file_get_contents('http://api.hostip.info/get_html.php?ip=' . $request->ip());
echo $country;
if (strpos($country, "UNITED STATES")) {
throw new \Exception("NOT FOR YOUR EYES, NSA");
} else {
return redirect("index");
} return $next($request);
}
}
此代码基本上接受请求,并作为示例检查它的位置,然后再决定是显示仅限美国的广告还是适合世界其他地区的广告。对于那些想要建立一个具有来自多个国家/地区的亚马逊联盟链接的网站的人来说,这可能非常有益。
请注意,您不应在中间件部分传回任何内容,而应将重定向传递给视图,而不是像我为简洁起见所做的那样打印出内容。
正在注册您的中间件
注册中间件时,您有 2 个选择。第一个选择是在您的应用程序处理的每个请求上添加中间件 运行。您可以通过打开 App\Http\Kernel.php 并将其添加到您的 $middleware 数组来做到这一点,如下所示:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
// our new class.
\App\http\Middleware\AdMiddleware::class,
];
第二种选择是仅在已注册的路由上使用中间件 运行,您可以这样注册它:
<?php
/** * The application's route middleware.
* * * @var array */
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'ad' => \App\Http\Middleware\AdMiddleware::class,
];
然后像这样将中间件添加到特定的路由中:
Route::get('/ip', ['middleware' => 'ad', function() { return "IP"; }]);