简体   繁体   中英

Laravel 5 Response json and input

What is the Laravel 5 equivelant for Laravel 4s?:

Response::json
Response::input

And what facade do I need to use?

Inject the ResponseFactory into your class/method:

<?php namespace App;

use Illuminate\Contracts\Routing\ResponseFactory;

class SomeClass {

    protected $response;

    public function __construct(ResponseFactory $response)
    {
        $this->response = $response;
    }

    public function someMethod()
    {
        return $this->response->json($data);
    }
}

Or:

// This will only work if method is resolved by service container
public function someMethod(ResponseFactory $response)
{
    return $response->json($data);
}

You can find a map of Laravel façades and what to type-hint instead at http://laravel.com/docs/master/facades#facade-class-reference

Alternative, you can still use façades, you just need to import them:

<?php namespace App;

use Response;

class SomeClass {

    public function someMethod()
    {
        return Response::json($data);
    }
}

But I'd recommend going the injecting-contracts routes. It's just a better approach.

Response::json() ----> response()->json(['key'=>'value'])

laravel 4.2 way works too btw.

Response::input() (?) [input should be in request] ----> Request::input()

here also, you can Input facade. In practice, not much changed in L5 as long as facades are concerned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM