imponeer/smarty-sunrise-http-router
Composer 安装命令:
composer require imponeer/smarty-sunrise-http-router
包简介
Smarty extensions to use with amazing router from sunrise-php
README 文档
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.
imponeer/smarty-sunrise-http-router 适用场景与选型建议
imponeer/smarty-sunrise-http-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.12k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 imponeer/smarty-sunrise-http-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imponeer/smarty-sunrise-http-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-03