Combine modules with presenter out of them

maky.klf
Member | 1
+
0
-

Hi,

I would like to have this hierarchy on those URLs. Most of presenters would be in their separate modules, but there are few that are common for them (where do I put Error presenters?).

  • …/admin/paper/edit/4, …/admin/another/whatever ⇒ Modules/Admin/PaperPresenter, AnotherPresenter
  • …/front/comment ⇒ Modules/Front/CommentPresenter
  • …/sign/in ⇒ Modules/SignPresenter
  • …/download/my-file ⇒ Modules/DownloadPresenter

I tried RouterFactory:

public static function createRouter(): RouteList
{
	return (new RouteList)
		->addRoute('sign/<action>', 'Sign:')
		->addRoute('download/<action>[/<id>]', 'Download:')
		->withModule('Admin')
		->addRoute('<presenter>/<action>[/<id>]', 'Paper:')
		->withModule('Front')
		->addRoute('<presenter>/<action>[/<id>]', 'Comment');
}

But I got warning: Invalid link: No route for Sign:out() for root URL: localhost/.

I also probably don't have correctly set the app up in common.neon, am I missing the presenters out of modules?

application:
	errorPresenter: Error
	mapping:
		*: My\Namespace\Modules\*\*Presenter

(I use psr-4 autoload with: My\Namespace → app, the Modules/ are directly in the app/)

How would you solve this (some out-of-modules presenters and most of module-presenters)? Is it a good practice/idea? What would be better?

Thank you in advance for any tip.

Rick Strafy
Nette Blogger | 67
+
0
-

Hi, look at https://github.com/…at-file-blog, it's an example repository with configured modules, you can see there how to create router properly and how to set presenter mapping in neon config.

ErrorPresenter is usually in the main (Front for example) module, as in that mentioned repository.

If you need different error pages, just check the URL in error presenter if it starts with “/admin” for example and set another template.

m.brecher
Generous Backer | 758
+
0
-

I recommend (a) place every presenter into certain module, (b) add prefix to every module with the exception of Front module:

public static function createRouter(): RouteList
{
    $router = new RouteList;

	$router->withModule('Admin')
		->addRoute('admin/<presenter>/<action>[/<id>]', 'Paper:default')

	$router->withModule('Front')
		->addRoute('sign/<action>', 'Sign:default')
        ->addRoute('download/<action>[/<id>]', 'Download:')
		->addRoute('<presenter>/<action>[/<id>]', 'Comment');

        return $router;
}