laravel Ajax 中间件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32246359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax Middleware
提问by panthro
I seem to remember in Laravel 4 there was an ajax filter, this would only allow requests via ajax.
我似乎记得在 Laravel 4 中有一个 ajax 过滤器,它只允许通过 ajax 请求。
Is there any similar middleware for Laravel 5.
Laravel 5 有没有类似的中间件?
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.
我有一条通过 ajax 从我的数据库中获取数据的路由,我想保护这条路由,这样用户就无法访问它并查看 json 数据字符串。
回答by Can Vural
You can use a middleware to do that.
您可以使用中间件来做到这一点。
php artisan make:middleware AllowOnlyAjaxRequests
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);
}
}
Add 'ajax' => \App\Http\Middleware\AllowOnlyAjaxRequests::class,
to your routeMiddleware
array in app/Http/Kernel.php
.
添加'ajax' => \App\Http\Middleware\AllowOnlyAjaxRequests::class,
到您的routeMiddleware
数组中app/Http/Kernel.php
。
Then you can use ajax
middleware on your routes.
然后你可以ajax
在你的路由上使用中间件。