imponeer/smarty-sunrise-http-router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

License GitHub release PHP Packagist Smarty version requirement

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:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature-name
  5. 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.

统计信息

  • 总下载量: 17.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固