scribe: Url parameter access on formRequest does not work
Hello,
In a FormRequest, access directly to url parameter does not work (null). Here is a code example:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Route;
class UserRequest extends FormRequest
{
public function rules(): array
{
$this->user; // null
$this->route('user'); // null
Route::current()->parameters()['user']; // not null
}
}
Only the third one works but i prefer to avoid this.
I ran php artisan scribe:generate
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 18 (8 by maintainers)
Hi,
Same problem here, i have a basic formRequest with the following rule :
And scribe throw exception cause $this->user is null
I have dived into your package and started to write a solution. My solution work but only if i specify an @urlParam
For example : route PUT /api/users/{user} and controller :
And here the following update that i have wrote into scribe Knuckles\Scribe\Extracting\Strategies\GetFromFormRequestBase.php
But i have found 2 problems and thats why i don’t PR your my solution. First : $endpointData->urlParameters have 2 keys, the one added by Strategies\UrlParameters\GetFromLaravelAPI::class and the second my @urlParams added by Strategies\UrlParameters\GetFromUrlParamTag::class, that don’t cause problem but it’s little strange that my @urlParams doesn’t override the first one ?
Second : in the case i don’t specify the @urlParams i have an not found exception throw by model binding cause GetFromLaravelAPI create a tag user_id and substitution doesn’t work.
Hope it help. 😃
I had a similar issue, which I was able to solve by using instantiateFormRequestUsing.
Here’s a simple example where the rules depend on a route param:
And in AppServiceProvider’s boot method:
Note that the Scribe documentation’s example uses
app()->makeWith()to instantiate the formrequest, which doesn’t work in my experience. When a FormRequest is resolved this way, it automatically triggers theValidatesWhenResolvedtrait, meaning that a ValidationException is thrown if the rules don’t pass (which they probably won’t since there are no request params). But you can instantiate the FormRequest usingnew, and then add “mock” dependencies, before passing it back to Scribe.So basically this workaround works by having the dependencies defined as class attributes, which are normally populated in prepareForValidation(), but can also be set manually.