Ajax中间件 [英] Ajax Middleware
问题描述
我似乎记得在Laravel 4中有一个ajax过滤器,它只允许通过ajax进行请求.
I seem to remember in Laravel 4 there was an ajax filter, this would only allow requests via ajax.
Laravel 5是否有任何类似的中间件.
Is there any similar middleware for Laravel 5.
我有一条通过ajax从数据库中获取数据的路由,我想保护此路由,以使任何用户都无法访问它并看到json数据字符串.
I have a route which gets data from my database via ajax, I want to protect this route so no user can go to it and see a json string of data.
推荐答案
您可以使用中间件来做到这一点.
You can use a middleware to do that.
php artisan make:middleware AllowOnlyAjaxRequests
app/Http/Middleware/AllowOnlyAjaxRequests.php
app/Http/Middleware/AllowOnlyAjaxRequests.php
<?php
namespace App\Http\Middleware;
use Closure;
class AllowOnlyAjaxRequests
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(!$request->ajax()) {
// Handle the non-ajax request
return response('', 405);
}
return $next($request);
}
}
将'ajax' => \App\Http\Middleware\AllowOnlyAjaxRequests::class,
添加到app/Http/Kernel.php
中的routeMiddleware
数组中.
然后,您可以在路由上使用ajax
中间件.
Then you can use ajax
middleware on your routes.
这篇关于Ajax中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!