Application mapping and modules

materix
Backer | 77
+
0
-

I find it hard to understand how mapping in common.neon works.

I try to setup a structure with a front-module and a admin-module, but after hours of trying, I can not get it to work.

The folder structure is currently (I am willing to changing it, if that is required to make it work):

app\UI\Front\Page1\Page1Presenter
app\UI\Front\Page2\Page2Presenter
app\UI\Admin\Dashboard1\Dashboard1Presenter
app\UI\Admin\Dashboard2\Dashboard2Presenter

Namespaces in the presenters, follow the folder-structure.

Routing of front-pages are relative to base, e.g. /page1 and /page2, while admin-pages are are prefixed by /admin, e.g. /admin/dashboard1 and /admin/dashboard2.

Router is:

		$router->addRoute('<presenter>/<action>[/<id>]', 'Homepage:default');

Mapping is currently (I have tried many variations without luck):

application:
	mapping:
		Front: [App\UI\Front, *, **Presenter]
		Admin: [App\UI\Admin, *, **Presenter]

When trying to open the front-pages, I get the errors:

Nette\Application\BadRequestException #404
Cannot load presenter 'Page1', class 'Page1Presenter' was not found

or when opening the dashboard-page.

Nette\Application\BadRequestException #404
Cannot load presenter 'Admin', class 'AdminPresenter' was not found.

The thing is, I actually do not understand how application-mapping works.
What does this syntax mean ?

mapping: App\Modules\*\**Presenter
#(taken from the docs)

Any hints would be much appreciated.

Infanticide0
Member | 89
+
0
-

Your folder structure looks fine. Try this mapping (and delete cache folder, to be sure).

application:
	mapping:
		*: App\UI\*\**Presenter
Marek Bartoš
Nette Blogger | 1212
+
+1
-

Use universal mapping and forget about it forever

application:
	mapping:
		*: ['', *, *\*Presenter]

It is 1:1 with namespace, only Presenter is just in class name and not the mapped name

Rick Strafy
Nette Blogger | 76
+
0
-

Marek Bartoš wrote:

Use universal mapping and forget about it forever

application:
	mapping:
		*: ['', *, *\*Presenter]

It is 1:1 with namespace, only Presenter is just in class name and not the mapped name

But you have to add at least starting point, like *: ['App\UI', '*', '*\*Presenter'], otherwise modules must start in the app directory (or even before now that I think about it), and since nette/application 3.2, this is the alias mapping: App\UI\*\**Presenter as shorter syntax.

This syntax also works with multi-module structure, for instance you can have Api:Domain:Status that leads to App\UI\Api\Domain\StatusPresenter. (I tried it before I posted, and wondered why it didn't work out on my app, there is a funny problem, that if you use nette/utils ^3.0 and nette/application ^3.0, nette/application stays locked at 3.1.15, because newer required nette/utils 4.)

Btw presenter mapping is explained here, I also don't know how exactly it works, I just know it does https://doc.nette.org/…tion/modules#…

Last edited by Rick Strafy (2024-06-30 20:13)

Marek Bartoš
Nette Blogger | 1212
+
0
-

otherwise modules must start in the app directory

As I wrote, it's universal. Vendor can contain presenters too.

Infanticide0
Member | 89
+
0
-

I tried your universal mapping and it universally broke my application. How is it supposed to work if I am using same file structure as masterix?

Cannot load presenter 'Front:Home', class 'Front\Home\HomePresenter' was not found.
App\UI\Front\Home\HomePresenter.php
namespace App\UI\Front\Home;

Last edited by Infanticide0 (2024-06-30 20:41)

Rick Strafy
Nette Blogger | 76
+
0
-

The mapping @MarekBartoš posted doesn't work, I think there is typo or something if he's using it, but this works.

application:
	mapping:
		*: ['App\UI', *, *\*Presenter]

or in nette/application 3.2+

mapping: App\UI\*\**Presenter

and it's pretty universal.

Last edited by Rick Strafy (2024-06-30 20:51)

Marek Bartoš
Nette Blogger | 1212
+
+1
-

The mapping @MarekBartoš posted doesn't work

Excusez moi? I am using it for (more than) 3 years – https://github.com/…/wiring.neon#L18

It's as I wrote, mapping is 1:1 – class App\UI\Front\Home\HomePresenter is mapped via App:UI:Front:Home:

Rick Strafy
Nette Blogger | 76
+
0
-

Ah I see, using full paths, my router didn't work because I used “Admin” as a module.. interesting approach.

Last edited by Rick Strafy (2024-06-30 22:18)

materix
Backer | 77
+
0
-

Thank you all for the tips. Mapping is now working.

I also needed to use “withModule()” in the router to make it work:


$router->withModule('Admin')
            ->addRoute('admin/<presenter>/<action>', 'Dashboard:default');

        $router->withModule('Front')
            ->addRoute('<presenter>/<action>[/<id>]', 'Homepage:default');