1

I've extended the Illuminate\Http\Request class and am passing it along to my controller.

In my controller, I check if the request has an Accept: application/json header, using the $request->wantsJson() method.

If I use the base Illuminate\Http\Request class it works perfectly fine, but if I use my extended class, it says that the Accept header is null.

use Illuminate\Http\Request;

class MyRequest extends Request
{
   ...
}

Controller

class MyController
{
    public function search(MyRequest $request) {
        if ($request->wantsJson()) {
            // return json
        }
        // return view
    }
}

This does not work. If I instead replace MyRequest with an instance of Illuminate\Http\Request it works. If I var_dump $request->header('Accept'), it's NULL when using MyRequest.

andrewtweber
  • 24,520
  • 22
  • 88
  • 110

1 Answers1

1

Extend Illuminate\Foundation\Http\FormRequest instead:

use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

The FormRequestServiceProvider performs a series of configuration steps that set up the request. You could replicate that in your own service provider, of course.

EricMakesStuff
  • 521
  • 4
  • 5
  • That fixed it, thanks. Actually I had originally extended `FormRequest` but then wanted it to be accessible by guests, so the `function authorize()` thing was what I really needed – andrewtweber Sep 01 '15 at 21:33