Description
Add the following to your routes.php
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->resource('recipe', 'App\Http\Controllers\IndexController');
});
Add, edit or replace the following two methods in your IndexController
class IndexController extends Controller
{
use Helpers;
public function index()
{
$dispatcher = app('Dingo\Api\Dispatcher');
try {
$response = $dispatcher->with(
[
'title' => '!!!',
'description' => '',
'lng_code' => 'en'
])->post('recipe');
}
catch (Exception $e)
{
debug($e);
}
}
public function store(StoreRecipeRequest $request)
{
return $request->get('title');
}
}
Create the following file named StoreRecipeRequest.php in app\Http\Requests\
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class StoreRecipeRequest extends \Dingo\Api\Http\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 [
'title' => 'required|string|max:128',
'description' => 'string',
'lang_code' => 'required|max:2'
];
}
}
Now, using (for example) Postman to call the store() method with the same data being passed in as in the Controller's index() function, the results returned are as expected.
{
"message": "422 Unprocessable Entity",
"errors": {
"lang_code": [
"The lang code field is required."
]
},
"status_code": 422,.....
However, if you try to run the controller's index() function, you'll see this: ValidationHttpException in FormRequest.php line 22:
Removing the type hinting from the store() method (so that no more FormRequest is executed), and manually writing the validation logic in the store() method, like so:
public function store(\Illuminate\Http\Request $request)
{
$this->validate($request, [
'title' => 'required|string|max:128',
'description' => 'string',
'lang_code' => 'required|max:2'
]);
}
does NOT work either, this time throwing the following: InternalHttpException in Dispatcher.php line 543:
Am I doing it wrong?