Custom routing implementatoin
- Bill Lions
- Member | 47
Browsing through https://doc.nette.org/…tion/routing#…
and this is what I have come up with for a simple blog crud.
this code works, but any pointers, tips, improvements here would be welcome.
declare(strict_types=1);
namespace App\Router;
use Nette;
use Nette\Application\Routers\RouteList;
class blogRouter implements Nette\Application\IRouter
{
public $defaults = [];
public function __construct(array $defaults = [])
{
$this->defaults = $defaults;
}
/**
*
* @access public
* @param Nette\Http\IRequest $httpRequest
* @throws UI\InvalidLinkException
* @return array
*
*/
function match(Nette\Http\IRequest $httpRequest): ?array
{
$url = $httpRequest->getUrl()->getPathInfo();
$path = pathinfo( $url );
$url = $httpRequest->getUrl()->getPathInfo();
switch( $url )
{
case strstr($url, 'blog/admin/view'):
case strstr($url, 'blog/admin/edit'):
case strstr($url, 'blog/admin/delete'):
$parts = explode( '/', $url );
$params = list($presenter, $admin, $action, $id) = $parts;
return ['presenter'=>'Blog',
'action'=>$action,
'id' => $id
];
case strstr($url, 'blog/admin/add'):
return ['presenter'=>'Blog',
'action'=>'add'
];
case 'blog/admin':
return ['presenter'=>'Blog',
'action'=>'admin',
'id' => null
];
case 'blog':
return ['presenter'=>'Blog',
'action'=>'default'
];
default:
throw new UI\InvalidLinkException("Invalid link destination '{$url}'.");
}
return null;
}
/**
* @access public
* @param array $params
* @param Nette\Http\UrlScript $refUrl
* @return string
*
*/
function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string
{
$auth = $refUrl->getAuthority();
$scheme = $refUrl->getScheme();
$action = $params['action'];
switch( $action )
{
case 'view':
case 'edit':
case 'delete':
$id = $params['id'];
return $scheme.'://'.$auth.'/blog/admin/'.$action.'/'.$id;
case 'add':
return $scheme.'://'.$auth.'/blog/admin/add';
case 'admin':
return $scheme.'://'.$auth.'/blog/admin';
default:
return $scheme.'://'.$auth.'/blog';
}
}
}
- Felix
- Nette Core | 1196
Hi @BillLions.
Do you really need a custom router? I mean what is your use-case? Just route some incoming requests to presenters? I think you can simplify it.
namespace App;
final class RouterFactory
{
public static function createRouter(): RouteList
{
$router = new RouteList;
// This accept whatever action you have (view, add, edit, delete) and points to Blog presenter with action=admin.
$router->addRoute('/blog/admin/<action>', 'Blog:admin');
// This is needed for other routes, default points to Homepage presenter with action=default
$router->addRoute('<presenter>/<action>', 'Homepage:default');
return $router;
}
}
Take a look some real examples:
- Bill Lions
- Member | 47
Felix wrote:
Hi @BillLions.
Do you really need a custom router? I mean what is your use-case? Just route some incoming requests to presenters? I think you can simplify it.
Thanks for the feedback Felix.
The first use case is that each module (e.g. blog) must be and independent, self contained, drop-in, directory.
So all routing and config relevant to a given module must have their own route and config files.
- Blog
- presenters
- BlogPresnter.php
- BlogAdminPresenter.php
- templates
- Blog
- default
- …
- Admin
- default
- …
- Router
- blogRouter.php
- config
- common.neon
I have this automated, so?
php nette module:create:Blog
The next step is to add the ACL to each route.
The ACL is managed via the database.
There is also the possibility of storing the routes in the database and have ACL
done on the routes directly from the database query.
Greatful for any feedback.