-
Couldn't load subscription status.
- Fork 11.6k
[11.x] Introduce RouteParameter attribute #53080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Introduce RouteParameter attribute #53080
Conversation
|
Thanks ! |
|
Everything is great, but it lacks support for the Declaration of App\Http\Requests\Private\Contest\UpdateRequest::prepareForValidation(App\Models\Contest $contest): void
must be compatible with Illuminate\Foundation\Http\FormRequest::prepareForValidation() class UpdateRequest extends Request
{
public function rules(#[RouteParameter('contest')] Contest $contest): array
{
return [
// some rules
];
}
protected function prepareForValidation(#[RouteParameter('contest')] Contest $contest): void
{
$this->merge([
'slug' => $this->firstOrSecond('slug', 'title'),
]);
$this->mergeIfMissing([
'params' => [
'winnersCount' => $contest->params?->winnersCount
?? DefaultCount::WINNERS,
'audienceWinnersCount' => $contest->params?->audienceWinnersCount
?? DefaultCount::AUDIENCE_WINNERS,
],
]);
}
}Crutch for that: class UpdateRequest extends Request
{
public function rules(#[RouteParameter('contest')] Contest $contest): array
{
return [
// some rules
];
}
protected function prepareForValidation(): void
{
$this->merge([
'slug' => $this->firstOrSecond('slug', 'title'),
]);
/** @var Contest $contest */
$contest = $this->route('contest');
$this->mergeIfMissing([
'params' => [
'winnersCount' => $contest->params?->winnersCount
?? DefaultCount::WINNERS,
'audienceWinnersCount' => $contest->params?->audienceWinnersCount
?? DefaultCount::AUDIENCE_WINNERS,
],
]);
}
} |
|
@bastien-phi Hey man, is this feature still available? When I dd() the attribute argument, I get an empty Model. I followed the syntax proposed above exactly. |
Check if the SubstituteBindings middleware is active on the route you need. |
@andrey-helldar It is active, Have you used it before, and worked properly? I don't know why I'm getting an empty Model when using it. |
|
Please provide your route definition, controller method and the usage you make of the attribute |
Yes, it works for me. BUT I haven't run the |
Route definition:
Controller method:
FormRequest rules() method: |
|
@medabkari I created this repo with some test code for your issue: https://github.com/rodrigopedra/pr-53080 You can install it by running: git clone https://github.com/rodrigopedra/pr-53080.git
cd pr-53080
composer run setupAnd then run: composer run devTo start the dev server. As of now, it works as expected. Can you please take a look and provide any info on what you are doing differently? |
|
@rodrigopedra Thank you for the repo, I'll check it out, but from my earlier reply, do you see anything that could've caused the issue? |
Not really. I tried to mimic closely to your report. |
I will try to push the framework to a further version, and see if the issue persists, maybe it's a bug. Based on the description above of the feature, I don't think something's wrong from my end. |
Accessing route parameter outside the Controller, in the FormRequest for example, can be done in various ways. In order to have correct IDE completion and good static analysis, we can do :
Both ways feel a bit tricky to me and not very elegant IMHO.
That's why I propose the introduction of the
RouteParameterattribute which can streamline the syntax, leveraging existingContextualAttributeand the container :The attribute can be used on any type of route parameter : Model, enum, string, ...