相关疑难解决方法(0)

如何在Laravel 5.X中制作Catch-All路线

我需要一个laravel 5.2 routes.php条目,它将捕获网站特定domain.com/premium-section的所有流量,以便我可以在访问高级内容之前提示人们成为成员.

我会回答我自己的问题〜希望人们可以使用这些信息.

php routing laravel-5

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

如何在Laravel 5.1中强制FormRequest返回json?

我正在使用 FormRequest来验证从我的智能手机应用程序的API调用中发送的内容.所以,我希望FormRequest在验证失败时总是返回json.

我看到了Laravel框架的以下源代码,如果reqeust是Ajax或wantJson,则FormRequest的默认行为是返回json.

//Illuminate\Foundation\Http\FormRequest class
/**
 * Get the proper failed validation response for the request.
 *
 * @param  array  $errors
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function response(array $errors)
{
    if ($this->ajax() || $this->wantsJson()) {
        return new JsonResponse($errors, 422);
    }

    return $this->redirector->to($this->getRedirectUrl())
                                    ->withInput($this->except($this->dontFlash))
                                    ->withErrors($errors, $this->errorBag);
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以添加Accept= application/json请求标头.FormRequest将返回json.但我希望提供一种更简单的方法来通过默认支持json来请求我的API,而无需设置任何标头.所以,我试图在Illuminate\Foundation\Http\FormRequest课堂上找到一些强制FormRequest响应json的选项.但我没有找到任何默认支持的选项.

解决方案1:覆盖请求抽象类

我试图覆盖我的应用程序请求抽象类,如下所示:

<?php

namespace Laravel5Cg\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;

abstract class Request extends FormRequest
{
    /**
     * Force response json type when validation fails
     * @var bool …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5 laravel-validation

23
推荐指数
3
解决办法
1万
查看次数

Laravel如何知道Request :: wantsJson是对JSON的请求?

我注意到Laravel有一个简洁的方法Request::wantsJson- 我假设当我发出请求时我可以传递信息来请求JSON响应,但是我该怎么做,以及Laravel使用什么标准来检测请求是否要求JSON?

php laravel laravel-4

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

Laravel 错误声明 App\Exceptions\Handler::report(Throwable $exception)

我正在使用 Laravel 6 并在部署到运行 PHP 7.3 的共享主机时出现以下错误:

App\Exceptions\Handler::report(Throwable $exception)
Run Code Online (Sandbox Code Playgroud)

App\Exceptions\Handler::report(Throwable $exception) 的声明必须与 /home/kb2hm3y8r4wm/public_html/laravel.supremeanimation.com/app/ 中的 Illuminate\Foundation\Exceptions\Handler::report(Exception $e) 兼容第 8 行的 Exceptions/Handler.php

php laravel

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