imponeer / smarty-sunrise-http-router
Smarty extensions to use with amazing router from sunrise-php
Package info
github.com/imponeer/smarty-sunrise-http-router
pkg:composer/imponeer/smarty-sunrise-http-router
Requires
- php: ^8.1
- imponeer/smarty-extensions-contracts: ^3.0
- sunrise/http-router: ^3
Requires (Dev)
- phpunit/phpunit: ^8 || ^9
README
Smarty Sunrise HTTP Router
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.
Installation
To install and use this package, we recommend to use Composer:
composer require imponeer/smarty-sunrise-http-router
Otherwise, you need to include manually files from src/ directory.
Setup
Modern Smarty Extension (Recommended)
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));
Using with Dependency Injection Containers
Symfony Container
# 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']]
PHP-DI Container
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)), ];
League Container
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; });
Usage
The {url} function renders a URL for a named route defined in Sunrise HTTP Router.
Generate a route URL
{url name="home"}
Passing route attributes
{url name="article" attributes=["slug" => "introduction-to-router"]} {* or the shorter alias *} {url name="article" attr=["slug" => "introduction-to-router"]}
Handling missing attributes
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.
Development
Code Quality Tools
-
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
Documentation
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.
Contributing
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.