Understanding Routing and Redirects

kodama
Member | 7
+
0
-

Hello,

Seems like routing should be simple in Nette, but it's just confusing me.

The url structure example is:
chinese-dvd-cover-scan/387/portland-street-blues

My route is:
$router->addRoute('chinese-dvd-cover-scan[/<id \d+>]<title>', 'CoverScan:default');

When clicked I'm seeing the following in the address bar:
cover-scan/default/387?title=portland-street-blues

Can someone explain what is happening with the name being trimmed (related to presenter name maybe?) and why does ‘default’ appear? And how do I keep the same structure when clicked? So, basically what should the correct route be?

Many thanks.

nightfish
Member | 485
+
0
-

@kodama Try with $router->addRoute('chinese-dvd-cover-scan[/<id \d+>/<title>]', 'CoverScan:default');

Square brackets denote optional parts of the route – I am not sure, if you want to also support just bare URL chinese-dvd-cover-scan. If that is not the case (and <id> and <title> have to always be present), then route would look like $router->addRoute('chinese-dvd-cover-scan/<id \d+>/<title>', 'CoverScan:default');.

Also pay attention to order of the routes – you want to place specific routes (like your route above) first, general routes (something like <presenter>/<action>) last, because routes are evaluated sequentially and the first matched route is used. So when you have a general route on the first position, it always gets matched and your more specific routes are ignored.

kodama
Member | 7
+
0
-

Thanks for the help nightfish, but this:

$router->addRoute('chinese-dvd-cover-scan[/<id \d+>/<title>]', 'CoverScan:default');

Gives me the exact same result. The url changes to:

cover-scan/default/387?title=portland-street-blues

Last edited by kodama (2024-04-29 17:25)

David Grudl
Nette Core | 8183
+
+1
-

In fact, the problem is not in that particular route, but in a previous one, which, being earlier, has a higher priority. The debugger can help you visualize this. More info about the order of the routes is in the documentation.

kodama
Member | 7
+
0
-

Thanks Dave, you are correct. Actually I only have 2 routes and switching them around worked. lol. I did see the docs about the routes, I just didn't think the default one affected the second one I added. A bit to learn with routing, so that is fine. Thanks.

kodama
Member | 7
+
0
-

Actually, I'm still confused. Let's say I have this in the router file:

$router->addRoute(‘japanese-dvd-cover-scan[/<id \d+>]/<title>’, ‘CoverScan:default’);
$router->addRoute(‘chinese-dvd-cover-scan[/<id \d+>]/<title>’, ‘CoverScan:default’);`

If you go to:
chinese-dvd-cover-scan/1807/18-secrets-of-kung-fu

It shows:
japanese-dvd-cover-scan/1807/18-secrets-of-kung-fu

So, it always shows the first route. Why does ‘chinese-dvd-cover-scan’ match the first route?

David Grudl
Nette Core | 8183
+
+1
-

Ask yourself: if id=1807 and title=18-secrets-of-kung-fu, which link should be generated? Both can be used, Nette cannot distinguish between them, so it generates the first one.

If the user comes through the second one, Nette automatically redirects to the first (this is called autoCanonicalize, it's for SEO reasons, you can turn it off).

Maybe the solution is to make one route with mask <lang>-dvd-cover-scan/<id>/<title>

kodama
Member | 7
+
0
-

Thanks Dave, it seems Nette uses a different kind of routing structure to other frameworks, so just getting used to it.