This library exposes Sunrise HTTP Router named routes to Smarty through a {url} template function, allowing templates to generate links without hardcoding paths.
It is designed for applications that keep routing logic in Sunrise HTTP Router and want those routes available directly inside Smarty views.
To install and use this package, we recommend to use Composer:
composer require imponeer/smarty-sunrise-http-routerOtherwise, you need to include manually files from src/ directory.
Register the extension with your Smarty instance and provide a configured router:
use Imponeer\Smarty\Extensions\SunriseHTTPRouter\SunriseHttpRouterExtension;
use Sunrise\Http\Router\RouterInterface;
// $router is a configured Sunrise\Http\Router\RouterInterface instance
$router = $container->get(RouterInterface::class);
$smarty = new \Smarty\Smarty();
$smarty->addExtension(new SunriseHttpRouterExtension($router));# config/services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
Imponeer\Smarty\Extensions\SunriseHTTPRouter\SunriseHttpRouterExtension:
arguments:
- '@Sunrise\Http\Router\RouterInterface'
\Smarty\Smarty:
calls:
- [addExtension, ['@Imponeer\Smarty\Extensions\SunriseHTTPRouter\SunriseHttpRouterExtension']]use Imponeer\Smarty\Extensions\SunriseHTTPRouter\SunriseHttpRouterExtension;
use Sunrise\Http\Router\RouterInterface;
use function DI\create;
use function DI\get;
return [
SunriseHttpRouterExtension::class => create()->constructor(get(RouterInterface::class)),
\Smarty\Smarty::class => create()->method('addExtension', get(SunriseHttpRouterExtension::class)),
];use Imponeer\Smarty\Extensions\SunriseHTTPRouter\SunriseHttpRouterExtension;
use Sunrise\Http\Router\RouterInterface;
$container = new \League\Container\Container();
$container->add(RouterInterface::class, function () {
// Build and return your RouterInterface implementation
});
$container->add(\Smarty\Smarty::class, function () use ($container) {
$smarty = new \Smarty\Smarty();
$smarty->addExtension(new SunriseHttpRouterExtension($container->get(RouterInterface::class)));
return $smarty;
});The {url} function renders a URL for a named route defined in Sunrise HTTP Router.
{url name="home"}{url name="article" attributes=["slug" => "introduction-to-router"]}
{* or the shorter alias *}
{url name="article" attr=["slug" => "introduction-to-router"]}If required route attributes are not provided or the route name does not exist, the router will throw an exception so you can catch and handle the error in your application.
-
PHPUnit - For unit testing
composer test -
PHP CodeSniffer - For coding standards (PSR-12)
composer phpcs # Check code style composer phpcbf # Fix code style issues automatically
-
PHPStan - For static analysis
composer phpstan
Routes are defined and built using Sunrise HTTP Router, and Smarty extension details are available in the Smarty documentation. Review those resources for deeper customization tips.
Contributions are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature-name - Submit a pull request
Please make sure your code follows the PSR-12 coding standard and include tests for any new features or bug fixes.
If you find a bug or have a feature request, please create an issue in the issue tracker.