drewroberts / blog

📝 Laravel package for my opinionated style of adding blog posts to Laravel projects
MIT License
2 stars 4 forks source link

Registration and handling of multi-segment wild card routes #50 #63

Closed pdbreen closed 3 years ago

pdbreen commented 3 years ago

This PR supports routes of /{topic}/{series?}/{post?} and /{page}/{childPage?}/{grandChildPage?}, but routes with no fixed segment to match on requires the route pattern to be "last" so it doesn't steal routes from other controllers. Unfortunately, Nova makes being "last" much more complicated because it internally defers registration of the NovaServiceProvider until the ServeNova middleware detects that the request is actually a Nova request. As a result, this package must:

As a result of this deferred registration, this blog route will never be cached - it will always be dynamically registered at some appropriate point in the provider boot cycle after request processing has initiated.

Next, the 2 desired routes for this package for page and posts are also identical. This is solved by registering a single generic route - /{slug1}/{slug2?}/{slug3?} - which is handled by a new,BlogController` that does not rely on model binding. Instead, the blog controller detects if the first segment matches a topic - if yes, it parses any remaining segments as series and posts and dispatches to the appropriate Topic, Series or Post controller based on the number of segments. If the first segment is NOT a topic, then the route is treated as a page path, with the remaining segments treated as child and grand child. Failure to resolve to a valid topic or page route for the number of segments provided results in a 404.

NOTE: Only "leaf" pages are considered valid paths. For example, if there is a "parent" page with a "child" page (no grand child), then the only valid route is /page/child - /page by itself will 404. If later the child has a "grand-child", then only /page/child/grand-child is valid - the /page/child route will now 404.

pdbreen commented 3 years ago

@drewroberts Nova forces route registration to be dynamic to ensure its always last. Much more fragile code (things happen differently based on whether the request is nova or not), but - no need to register the route manually in the app after all.

drewroberts commented 3 years ago

Can you also add an exception for /admin along with /nova to pass to those routes?

Is there a way we can add other reserved namespaces in the Page model?

pdbreen commented 3 years ago

I don't understand the request. By being last route registered, Nova routes (and all other routes) will have priority in matching (unit tests for Nova actually require this). If there is a page (or topic) with a slug of "nova" or "admin", the page (or topic) just won't be accessible - the nova route will be rendered instead. Really no need to prevent it -- not being accessible should be incentive enough to get someone to fix it.

drewroberts commented 3 years ago

OK, that makes sense. I looked at your code on how the Nova routes were registered. I like it.