webchemistry / router
Router
Installs: 1 704
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 5
Forks: 1
Open Issues: 0
Requires
- php: >=7.1
- nette/application: ^2.3 || ^3.0
Requires (Dev)
- codeception/codeception: ^2.5
- nette/di: ^2.3 || ^3.0
README
Overview
Installation
Install via composer.
composer require webchemistry/router
neon:
extensions: routers: WebChemistry\Routing\DI\RouterExtension
Configuration
routers: modules: - Front - Admin routers: - App\MainRouter - YourRouter - HisRouter
Main router
<?php namespace App class MainRouter implements WebChemistry\Routing\IRouter { /** * @param RouteManager $routeManager */ public function createRouter(RouteManager $routeManager) { $routeManager->addStyle('name'); $routeManager->setStyleProperty('name', Route::FILTER_OUT, function($url) { return Strings::webalize($url); }); $routeManager->setStyleProperty('name', Route::FILTER_IN, function($url) { return Strings::webalize($url); }); // Admin $admin = $routeManager->getModule('Admin'); $admin[] = new Route('admin/<presenter>[/<action>][/<id [0-9]+>[-<name [0-9a-zA-Z\-]+>]]', [ 'presenter' => 'Homepage', 'action' => 'default', ]); // Front $front = $routeManager->getModule('Front'); $front[] = new Route('<presenter>[/<action>][/<id [0-9]+>[-<name [0-9a-zA-Z\-]+>]]', [ 'presenter' => 'Homepage', 'action' => 'default', ]); } }
Usage autoregistration in own extension
<?php namespace TestPackage\Test\DI; use Nette\Application\IPresenterFactory; use Nette\DI\CompilerExtension; class TestExtension extends CompilerExtension { public function loadConfiguration() { $builder = $this->getContainerBuilder(); $builder->addDefinition($this->prefix('routers')) ->addTag('router') ->setFactory(\TestPackage\Test\TestRouter::class) ->setAutowired(true); $builder->getDefinition('routers.routerManager') ->addSetup('createModule', ['Test']); } public function beforeCompile() { $builder = $this->getContainerBuilder(); $builder->getDefinition($builder->getByType(IPresenterFactory::class)) ->addSetup( 'setMapping', [['Test' => 'TestPackage\Test\Presenters\*Presenter']] ); } }
<?php namespace TestPackage\Test; use WebChemistry\Routing\IRouter; use WebChemistry\Routing\RouteManager; use Nette\Application\Routers\Route; class TestRouter implements IRouter { /** * @param RouteManager $routeManager */ public function createRouter(RouteManager $routeManager): void { $app = $routeManager->getModule('Test'); $app[] = new Route('/test/<presenter>/<action>[/<id>]', 'Default:default'); } }
app/config.neon:
extensions: ... - TestPackage\Test\DI\TestExtension ...
For correct router orders, you have to list all routers in app/config.neon:
extensions: ... routers: WebChemistry\Routing\DI\RouterExtension - TestPackage\Test\DI\TestExtension ... routers: modules: ... - Test ... - App routers: ... - App\MainRouter