简体   繁体   English

Laravel 5 Response json和输入

[英]Laravel 5 Response json and input

What is the Laravel 5 equivelant for Laravel 4s?: Laravel 4的Laravel 5等价物是什么?

Response::json
Response::input

And what facade do I need to use? 我需要使用什么门面?

Inject the ResponseFactory into your class/method: ResponseFactory注入您的类/方法中:

<?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 您可以在http://laravel.com/docs/master/facades#facade-class-reference上找到Laravel外墙的地图以及键入提示的内容

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']) Response::json() ----> response()->json(['key'=>'value'])

laravel 4.2 way works too btw. laravel 4.2的方式也顺便说一句。

Response::input() (?) [input should be in request] ----> Request::input() Response::input() (?)[输入应在请求中] ----> Request::input()

here also, you can Input facade. 您也可以在此处Input立面。 In practice, not much changed in L5 as long as facades are concerned. 实际上,只要考虑到外墙,L5不会有太大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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