Create links in Latte with several Modules

shuseisajdi
Member | 8
+
+1
-

I'm sure you wouldn't make such an oversight, but I created several “modules” and set “mapping:” appropriately, but I ran into a problem with “Latte's” n:href" syntax.

This routing configuration

$router->addRoute('/business[/<presenter>[/<action>]]', [
	'module' => 'Business',
	'presenter' => 'Home',
	'action' => 'default',
]);

$router->addRoute('/[<presenter>[/<action>]]', [
	'module' => 'Front',
	'presenter' => 'Home',
	'action' => 'default',
]);

didn't work.

<a n:href="Front:Shop:default">Shop</a>
<a n:href="Business:Office:default">Office</a>

I re-read carefully and found the following statement in nette HP.

Links may also point to other modules. Here, the links are distinguished into relative to the submodules, or absolute. The principle is analogous to disk paths, only instead of slashes there are colons. Let's assume that the actual presenter is part of module Front, then we will write:

`<a n:href=":Admin:Product:show">link to Admin:Product:show</a>`

The leading “:” is a very small point, but a pritty big point!

The problem I had been struggling with for two days was easily solved.
This is a problem that beginners have when creating multiple “modules” for the first time, so I'm writing this as a memo.

Thanks to “Nette” and its manual, I've become able to handle a variety of situations.

m.brecher
Generous Backer | 864
+
+1
-

@shuseisajdi

Try refactor your routing configuration using withModule(‘Module’)

$router->withModule('Business')
	->addRoute('business/<presenter>/<action>', 'Home.default')
	->addRoute(... some other route);

$router->withModule('Front')
	->addRoute('<presenter>/<action>', 'Home.default')
	->addRoute(... some other route);

Comments:

  • after withModule(‘Module’) you can call several times ->addRoute all with the same Module
  • there is no need start route with /
  • if you use <presenter> or <action> these parameters are optional by default if you use as second parameter default value in short version (‘Home.default’)
  • if you want use prefix for one/several modules + another (Front) module without prefix, you must put this non-prefixed module to the at the end, first more specific route, later more common route
  • no guarantee for the sample is 100% correct – I'm writing off the top of my head

Last edited by m.brecher (2024-10-23 14:13)

shuseisajdi
Member | 8
+
0
-

Thans m.brecher.
I did try code of yours with bit changed.

in RouterFactory
$router->withModule('Front')
	->addRoute('/[<presenter>[/<action>]]', 'Home:default');
$router->withModule('Business')
	->addRoute('/business/<presenter>/<action>[/<id>]', 'Trade:default');

in Home/default.latte
<a n:href="Home:default">Home</a>	--> OK
<a n:href=":Business:Trade:default">biz trade</a> --> OK

<a n:href="Business:Trade:default">biz trade</a> --> Error
"Invalid link: Cannot load presenter 'Front:Business:Trade', class 'App\Front\Business\Trade\TradePresenter' was not found."

It seems that I still need a root reference for “Business”.
I understand a bit more about withModule though. Thanks.

Last edited by shuseisajdi (2024-10-24 11:03)