A
A
Alexey Konovalov2020-04-24 17:56:52
PHP
Alexey Konovalov, 2020-04-24 17:56:52

How to do multilevel routing correctly?

Hello! Completely confused with routing, kick in the right direction.
I'm trying to make pages:

  • /user/234 - user page
  • /user/234/albums - user's albums page
  • /user/234/albums/edit/234 - specific user album

It turns out this route:
'user/(?P<user_id>[0-9]+)(/(?P<action>[a-z_-]+))?' => [
   'controller' => 'user'
];

It turns out that user is a controller, 234 is its ID, albums is an action - a controller method. But what is edit in this case? But what if there is more such nesting, for example, when these are categories and subcategories?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2020-04-24
@AndryG

Discover the HTTP methods (get/post/put/delete/patch) - they were invented for a reason.
For ease of solution, take the form CLASS/param/param/param
/user/234 - user's page
/user/234 - similar to the previous
albums/user/234/ - user's albums page 234
albums/567 - user's specific album (if album numbers are cross-cutting)
albums/234/6 - if you need two user-num fields
But what to do - let it specify the HTTP method (get/put/delete)
Well, or CLASS/METHOD/param/param/param

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question