承接 delolmo/symfony-router 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

delolmo/symfony-router

Composer 安装命令:

composer require delolmo/symfony-router

包简介

PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.

README 文档

README

Packagist Version License: MIT

PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.

Requirements

Installation

This package is installable and autoloadable via Composer as delolmo/symfony-router.

composer require delolmo/symfony-router

Example

Consider Symfony's PhpFileLoader to load route definitions from the following file:

# routes.php

use App\Controller\BlogController;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$routes = new RouteCollection();
$routes->add('blog_list', new Route('/blog', array(
    'request-handler' => [BlogController::class, 'list']
)));
$routes->add('blog_show', new Route('/blog/{slug}', array(
    'request-handler' => [BlogController::class, 'show']
)));

return $routes;

For this example, we will be using middlewares/utils for a PSR-15 compliant dispatcher. See link for more PSR-15 implementations.

This example uses a basic anonymous function to print the route's attributes:

use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequest;
use Middlewares\Utils\Dispatcher;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Router;

$fileLocator = new FileLocator(array(__DIR__));

$router = new Router(
    new PhpFileLoader($fileLocator),
    'routes.php',
    array('cache_dir' => __DIR__ . '/cache'),
    new RequestContext('/')
);

$responseFactory = new ResponseFactory();

$dispatcher = new Dispatcher([
    new DelOlmo\Middleware\SymfonyRouterMiddleware($router, $responseFactory),
    function($request, $next) {
        return new HtmlResponse(json_encode($request->getAttributes()));
    }
]);

// Try matching a /blog request
$response = $dispatcher->dispatch(new ServerRequest([], [], '/blog'));

// Will return {"_route": "blog_list", "request-handler" => ["App\Controller\BlogController", "list"]}
$c->get('emitter')->emit($response);

// Try matching a /blog/hello-world request
$response = $dispatcher->dispatch(new ServerRequest([], [], '/blog/hello-world'));

// Will return {"_route": "blog_show", "request-handler" => ["App\Controller\BlogController", "show"], "slug" => "hello-world"}
$c->get('emitter')->emit($response);

Options

The constructor takes two arguments:

__construct(
    \Symfony\Component\Routing\Router $router,
    \Psr\Http\Message\ResponseFactoryInterface $responseFactory
)

The router instance to use and a PSR-17 factory to create the error responses (404 or 405).

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固