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.
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.