[5.4] add ability to create nested model controllers#18606
Conversation
- new stub for nested controllers - ‘parent’ option for make command - if we want a parent, determine the replacement values based on the model name
|
I'm curious why you would really need a nested URL in the first place? If each document has a unique auto-incrementing ID or even a UUID, couldn't the url just be "/documents/{id}"? |
|
there are definitely many ways to tackle this, and I think part of it probably does come down to personal preference. I agree the nested URL can become less important on the If you decide to go with <form>
<input name="department_id" />
<input name="client_id" />
</form>or you could make multiple views specific to each parent.
which results in a lot of code duplication, and then you have to determine which view to render, and which model to attach it to. IMO, it is much easier to have the parent in the URL.
Then you can have one view that gets passed the 'owner'. Your <h1>{{ $owner->getDocumentableName() }}</h1>
<form>
...
</form>With many-to-many relationships this nested route pattern becomes even more important since you often do not have IDs or UUIDs. If we look at the classic
because our ID doesn't exist. We need a nested URL like
Again, there are definitely ways to accomplish this without nested URLs, but I believe they are a common valid way to handle this. In fact, you can find nested URLs on both Forge and Envoyer.
|
The PR in Laravel's repo laravel/framework#18606
when we have polymorphic relationships, we often have nested routes and controllers. for example, we have the following models:
Client,Department,Document.Clients andDepartments can have multipleDocuments, soDocuments are polymorphic to their owners.In this situation we could have 2 controllers:
ClientDocumentControllerandDepartmentDocumentControllerand the following routes:make:controlleris great for generating single model resource controllers but doesn't help with these nested relationships. this PR adds the ability to quickly generate these nested controllers by passing a 'parent' flag with the command.