Is there a way that I can generate multiple URLs within the same controller that use the same structure?
#[Route('/project/posts/{id}', name:"project-post-view")] //old url
#[Route('/project/posts/{post_ulid}', name:"project-post-view")]
#[Route('/project/posts/{slug}', name:"project-post-view")] // alternative url
function projectViewPost(int $id, string $post_ulid, string $slug, ManagerRegistry $doctrine)
{
//Function stuff here...
}
The ID url is specifically for legacy emails, where ideally we’d like to forward to the ulid version. Alternatively, if a post uses a slug, it should be in place of the ulid. Is this the format used or how exactly is this defined to fit these conditions?
2
Answers
You have to use Parameter Validation
So your routes should look something like this:
It really depends on the difference between the parameter patterns, but you can make this work using Parameter Validation as suggested by Code Spirit.
You need to put all three nullable parameter slugs together in one route definition:
Also note that the non-optional argument
$doctrine
must come before all of the optional arguments.Tested in v 7.2 (dev):
/project/posts/45
returns:{"id":"45","post_ulid":"","slug":""}
/project/posts/A-45
returns:{"id":"","post_ulid":"A-45","slug":""}
/project/posts/soupy-soup
returns:{"id":"","post_ulid":"","slug":"soupy-soup"}
Be advised this has the potential to match all three parameters. (This is why I say it depends on the difference in the patterns):
/project/posts/45A-27soup
returns:{"id":"45","post_ulid":"A-27","slug":"soup"}
So if your patterns have the potential to overlap, it will be better to use separate functions.