php Laravel在控制器中添加额外的数据沿着自定义响应

s4n0splo  于 5个月前  发布在  PHP
关注(0)|答案(3)|浏览(65)

我正在调用laravel控制器中的一个函数。我使用了laravel的自定义响应来返回数据。但是一些其他的数据被添加到我的响应中,尽管我没有添加它。

return Response::make(json_encode(array(
    'error' => false, 
    'message' => 'Redaction done successfully.', 
    'is_redacted' => 0)), 200, 
    array(
    'Content-Type' => 'application/json'
    )
);

字符串
上面是我添加的返回我的响应的代码。但是我收到的响应是:

> * Found bundle for host redaction-stage-tcm.tylerhost.net: 0x55998c67ee40
    * Re-using existing connection! (#0) with host redaction-stage-tcm.tylerhost.net
    * Connected to redaction-stage-tcm.tylerhost.net (208.64.239.110) port 443 (#0)
    > POST /ocr/OCRWeb/v2/Documents('mfr_8')/OCRWeb.Scan HTTP/1.1
    Host: redaction-stage-tcm.tylerhost.net
    User-Agent: GuzzleHttp/6.3.3 curl/7.43.0 PHP/5.6.11-1ubuntu3.4
    OData-Version: 4.01
    OData-MaxVersion: 4.01
    Content-Type: application/json
    Content-Length: 2
    upload completely sent off: 2 out of 2 bytes
    < HTTP/1.1 200 
    < Cache-Control: no-transform
    < Cache-Control: no-cache
    < Cache-Control: no-store
    < Cache-Control: must-revalidate
    < Cache-Control: max-age=0
    < Expires: Thu, 01 Jan 1970 00:00:00 GMT
    < Vary: *
    < Pragma: no-cache
    < X-Clacks-Overhead: GNU Terry Pratchett
    < OData-Version: 4.01
    < Preference-Applied: 
    < Content-Type: application/json; odata.metadata=minimal
    < Content-Length: 163
    < Date: Wed, 13 Feb 2019 10:44:40 GMT
    < 
    * Connection #0 to host redaction-stage-tcm.tylerhost.net left intact
    {"error":false,"message":"Redaction done successfully.","is_redacted":0}


我在函数中调用了一些REST API。这个数据与此相关,但为什么它会附加到我的自定义响应中??请帮助我。

ecbunoof

ecbunoof1#

试试这个

return response()->json([
   'error' => false, 
   'message' => 'Redaction done successfully.', 
   'is_redacted' => 0, 
   'Content-Type' => 'application/json'
], 200)

字符串

35g0bw71

35g0bw712#

创建一个名为AddToRequest的中间件,并将其注册到Kernel.php中。

AddToRequest.php

<?php namespace App\Http\Middleware;

use Carbon\Carbon;
use Closure;

class AddToRequest {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->request->add(['dummy' => 'hey man', 'date' => Carbon::now()]);

        return $next($request);
    }
}

字符串

Kernel.php

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
        'inject' => 'App\Http\Middleware\AddToRequest'
    ];

}


AddToRequest中间件的handle()方法中,我设置了两个变量并将其添加到请求中

$request->request->add(['dummy' => 'hey man', 'date' => Carbon::now()]);


然后,在我的控制器中使用inject中间件

public function __construct()
{
    $this->middleware('inject');
    $this->middleware('guest');
}


在控制器的index()方法中,我注入use Illuminate\Http\Request对象,并从请求对象中提取我在中间件中设置的变量,如下所示:

public function index(Request $request)
{
    dd($request->get('dummy'), $request->get('date'));

    return view('welcome');
}

eivgtgni

eivgtgni3#

为了防止你的情况和我的情况类似,我试图在Laravel的log中使用print_r从API控制器中调用的函数中记录一个对象。我使用了print_r,但没有使用“true”开关,结果不是将其作为字符串返回,而是将其发送到标准输出,从而将其置于控制器的正常响应之前。将log命令更改为:

Log::info(print_r($myObject, true));

字符串
帮我解决了问题
print_r(不带 true 参数)、var_dumpvar_export(没有 true 参数),本质上是试图“在屏幕上打印”您提供的任何变量作为参数,并且由于API调用不直接将其响应返回到屏幕,因此这些函数的输出被预先添加到API通常返回的响应中。或者将它们与true参数(对于print_r和var_export)一起使用,将它们的输出作为字符串返回,而不是将其发送到标准输出,这将允许API仅返回其预期的响应。

相关问题