I'm currently writing a tool to manage certain articles, technical documentations, ...
Every article has a part number, which is always in the format xxxx-xxx-xx
, where x
is a digit. Currently, my routing is set up as follows:
admin/{partnumber} (List all documentation entries)
admin/{partnumber}/new (New documentation entry)
admin/{partnumber}/edit (Edit the article itself)
admin/{partnumber}/{id} (Edit the documentation with the given id for the article)
Where partnumber
must match the following regular expression:
(\d{4}-\d{3}-\d{2}|\d{9})
This is, so I don't necessarily have to type in the dashes. However, to make my urls look a little easier and make them more comprehensible, I'd like to automatically redirect to the entered url, but add the dashes.
example.com/admin/123456789 => example.com/admin/1234-567-89
example.com/admin/123456789/new => example.com/admin/1234-567-89/new
...
I figured I could create a route for \d{9}
separately, like the following:
Route::get('admin/{partnumber}',
'ArticleAdminController@redirectWithDashes')
->where('partnumber', '\d{9}');
And then perform a redirect in that controller's function. However, this would only work for admin/{partnumber}
, not for any of the other routes.
I can't think of any way to do it, except making every function for all the routes twice, once just for redirection. That seems a little verbose to me though and can't be the solution.
Another idea I had was creating a middleware for the specified routes and redirect to the routes themselves, but with different parameters. But that doesn't seem like a good use case for middleware in my opinion.
Am I missing something? Is this possible at all?
What I would do when absolutely wanting to fix this in laravel is create a middleware, for instance
PartnumberMiddleware
:Not tested and you need to fill in the blanks. But this should work once you registered the middleware.
Added the mutation to the partnumber, added getting the route from the request object.