dingo / dingo/api

Internal requests do NOT work properly

Open
#1,024 15 comments 0 reactions 0 assignees View on GitHub
bug duplicate planned
Dominant language
PHP
Stars
9.4k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Add the following to your routes.php

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

``` php
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
'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.

``` php
{
"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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.