标签: laravel-response

我如何在Laravel 5.5中的FormRequest类中返回自定义响应?

我正在制作一个API,我希望返回错误数组,格式为$validator->errors();我通过手动方式验证请求时生成的格式.但我不能操纵回应.我想找到正确的方法来实现它.

这可以在Laravel 5.4中使用该formatErrors方法完成,并Illuminate\Contracts\Validation\Validator在FormRequest类中包含类,但对于版本5.5,它不起作用.我不知道怎么做.

这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\ProductRequest;
use Illuminate\Validation\Rule;
use App\Product;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(ProductRequest $request)
    {
        $products = Product::where('code', 'LIKE', '%'.$request->input('search').'%')
        ->where('name', 'LIKE', '%'.$request->input('search').'%')
        ->paginate(10);
        $products->withPath($request->fullUrl());
        return $products;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-validation laravel-request laravel-response laravel-5.5

10
推荐指数
1
解决办法
3890
查看次数

如何在Laravel 5.5中为选定的Request类设置自定义响应

我正在尝试使用Laravel验证来生成自定义错误消息,但是我无法找到我应该重写的函数.

Route:POST:/entries/用于执行验证的EntryController@store用途EntryStoreRequest.

EntryStoreRequest

namespace App\Api\V1\Requests;

class EntryStoreRequest extends ApiRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'message' => [
                'string',
                'required',
                'max:65535',
            ],
            'code' => [
                'string',
                'max:255',
                'nullable'
            ],
            'file' => [
                'string',
                'max:255',
                'nullable'
            ],
            'line' => [
                'string',
                'max:255',
                'nullable'
            ],
            'stack' => [
                'string',
                'max:65535',
                'nullable'
            ]
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

ApiRequest

namespace App\Api\V1\Requests;

use …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-5 laravel-request laravel-response laravel-5.5

6
推荐指数
1
解决办法
4668
查看次数

使用laravel强制下载文件

所以我正在使用Laravel 5,我试图强制下载所选文件,但没有任何反应.

public function downloadUserFile(){
        $result = $_POST['filename'];
        $entry = File::where('filename', $result)->firstOrFail();
        $file = Storage::disk()->get($entry->filePath);
        $headers = array('Content-Type' => $entry->mimetype);
        return response()->download(storage_path($entry->filePath), $result, $headers);
}
Run Code Online (Sandbox Code Playgroud)

并且响应头似乎没问题

Accept-Ranges: none
Cache-Control: public
Connection: Keep-Alive
Content-Disposition: attachment; filename="SIGNAL - Nadezdata.mp3"
Content-Length: 4205059
Content-Type: audio/mpeg
Run Code Online (Sandbox Code Playgroud)

你知道什么是错的吗?

php download laravel laravel-5 laravel-response

5
推荐指数
1
解决办法
1426
查看次数

get the request values in Laravel

I wish to make search query by datepicker and select field. How could I get the requests values from below view file to controller? Where could I modify in the code? thanks.

index.blade.php

<div class="form-group col-sm-6">
    {!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/pdfs/job_finished_search'))) !!} 
       {!! Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) !!}
       {!! Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) !!} 
       {!! Form::select('customer_name', $jobs->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
       {!! Form::submit('Søke', …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-request laravel-response laravel-query-builder

4
推荐指数
2
解决办法
2万
查看次数

Laravel-如何将API资源递归转换为数组?

我正在使用Laravel API资源,并希望将实例的所有部分都转换为数组。

在我的PreorderResource.php

/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request
 * @return array
 */
public function toArray($request)
{
    return [
        'id' => $this->id,
        'exception' => $this->exception,
        'failed_at' => $this->failed_at,
        'driver' => new DriverResource(
            $this->whenLoaded('driver')
        )
    ];
}
Run Code Online (Sandbox Code Playgroud)

然后解决:

$resolved = (new PreorderResource(
  $preorder->load('driver')
))->resolve();
Run Code Online (Sandbox Code Playgroud)

乍一看,该方法可以解决问题,但问题是它无法递归工作。我的资源解析如下:

array:3 [
  "id" => 8
  "exception" => null
  "failed_at" => null
  "driver" => Modules\User\Transformers\DriverResource {#1359}
]
Run Code Online (Sandbox Code Playgroud)

如何解析API资源以递归数组?

php laravel laravel-response laravel-5.6 laravel-resource

3
推荐指数
2
解决办法
1508
查看次数

Laravel 5.5 测试 JSON 标头不起作用

我在测试 Laravel 5.5 时遇到问题。我需要在 TEST HEADER 中发送一个不记名令牌,但不起作用

public function testAuthCheckinvalidToken()
    {
        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $this->token,
        ])->json('GET', 'auth/check');
    ...
    }
Run Code Online (Sandbox Code Playgroud)

当我 dd($response) 时,只设置了默认的 HEADERS:

#headers: array:5 [
            "cache-control" => array:1 [
              0 => "no-cache, private"
            ]
            "date" => array:1 [
              0 => "Tue, 21 Nov 2017 18:48:27 GMT"
            ]
            "content-type" => array:1 [
              0 => "application/json"
            ]
            "x-ratelimit-limit" => array:1 [
              0 => 60
            ]
            "x-ratelimit-remaining" => array:1 [
              0 …
Run Code Online (Sandbox Code Playgroud)

phpunit laravel-5 laravel-request laravel-response laravel-5.5

2
推荐指数
1
解决办法
3885
查看次数

Laravel 6 重写 FormRequest 的 failedValidation 方法时出现自定义验证错误“类不存在”

我创建了一个自定义验证类,位于app\Http\Requests

<?php

namespace App\Http\Requests;

use App\Http\Requests\BaseFormRequest;

class RegisterUserRequest extends BaseFormRequest
{
    protected static $NEEDS_AUTHORIZATION = false;

    protected static $FORM_RULES = [
        'first_name'              => 'required|string|max:80',
        'last_name'               => 'required|string|max:80',
        'email'                   => 'required|unique:users|email',
        'password'                => 'required|string|min:6|confirmed',
        'terms_conditions'        => 'accepted'
    ];

}
Run Code Online (Sandbox Code Playgroud)

BaseFormRequest并在同一目录中扩展:

<?php 

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Exceptions\HttpResponseException;
//use Illuminate\Http\Request;

abstract class BaseFormRequest extends FormRequest 
{
    protected static $NEEDS_AUTHORIZATION = true;
    protected static $FORM_RULES          = [];
    protected static $ERROR_MESSAGES      = [];

    /**
     * Determine if …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-validation laravel-response laravel-6

2
推荐指数
1
解决办法
5662
查看次数

laravel 5.5 FormRequest 类正在重定向到我,我需要发送数组错误响应

当我使用FormRequest扩展类验证请求时遇到问题。因为当收到错误请求时会重定向并且我需要带有验证错误的响应

我在用着:

  • PHP 7.1.1 (cli)(内置:2017 年 1 月 18 日 18:51:14)( ZTS MSVC14 (Visual C++ 2015) x86 )版权所有 (c) 1997-2017 PHP Group Zend Engine v3.1.0,版权所有 (c) 1998 -2017 Zend 技术。
  • Laravel v5.5.2

我的 FormRequest 类:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BillRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-validation laravel-request laravel-response laravel-5.5

1
推荐指数
1
解决办法
911
查看次数

如何更改 Laravel 中的响应?

我有端点可用的 RESTful 服务。

例如,我api/main从服务器请求并获取 JSON 数据。

对于响应,我使用:

return response()->json(["categories" => $categories]);
Run Code Online (Sandbox Code Playgroud)

如何控制 URL 中响应传递参数的格式?

作为示例,我需要这个:api/main?format=json|html它适用response于控制器中的每个。

laravel laravel-5 laravel-response laravel-5.4

0
推荐指数
2
解决办法
6510
查看次数