Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel5中间件中操作JSON_Php_Json_Laravel_Laravel 5 - Fatal编程技术网

Php 在Laravel5中间件中操作JSON

Php 在Laravel5中间件中操作JSON,php,json,laravel,laravel-5,Php,Json,Laravel,Laravel 5,我有一个Ajax请求,它被发送到一个Laravel 5应用程序。 但我需要重新格式化/更改/。。。将JSON发送到控制器之前 有没有办法在中间件中操纵请求体(JSON) <?php namespace App\Http\Middleware; use Closure; class RequestManipulator { /** * Handle an incoming request. * * @param \Illuminate\Http\

我有一个Ajax请求,它被发送到一个Laravel 5应用程序。 但我需要重新格式化/更改/。。。将JSON发送到控制器之前

有没有办法在中间件中操纵请求体(JSON)

<?php namespace App\Http\Middleware;

use Closure;

class RequestManipulator {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->isJson())
        {
            $json = json_decode($request->getContent(), TRUE);
            //manipulate the json and set it again to the the request
            $manipulatedRequest = ....
            $request = $manipulatedRequest;
        }
        \Log::info($request);
        return $next($request); 
    }
}

是的,您可能有两种类型的中间件,一种在请求之前运行,另一种在请求之后运行,您可以找到有关它的更多信息

要创建一个中间件,可以使用以下命令生成一个中间件:

php artisan make:middleware ProcessJsonMiddleware
然后在内核上用友好的名称注册它

protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
        'process.json' => 'App\Http\Middleware\ProcessJsonMiddleware',
    ];
此中间件只是一个示例,它删除了阵列的最后一个元素,并在请求时替换它:

<?php namespace App\Http\Middleware;

use Closure;
use Tokenizer;

class ProcessJsonMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->isJson())
        {
            //fetch your json data, instead of doing the way you were doing
            $json_array = $request->json()->all();

            //we have an array now let's remove the last element
            $our_last_element = array_pop($json_array);

            //now we replace our json data with our new json data without the last element
            $request->json()->replace($json_array);
        }

        return $next($request);

    }

}

artisan命令中有一个输入错误:processJSONT;)哎呀,修好了。谢谢,我已经用自定义的验证请求而不是
Request
尝试了这个方法,但是在\light\Http\Request中它不起作用。你知道为什么会发生这种情况吗?@lenny.myr你用定制请求解决了吗?我也有同样的问题,它只在没有标题
内容类型:application/json
的情况下工作,否则定制请求最终会保留原始数据。与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。顺便说一句,这是“提前感谢”,而不是“提前感谢”。
public function index(Request $request)
{
    //var_dump and die our filtered json
    dd($request->json()->all());
}