承接 lucid/mux 相关项目开发

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

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

lucid/mux

Composer 安装命令:

composer require lucid/mux

包简介

Psr7 compatible http Routing Library

README 文档

README

Author Source Code Software License

Build Status Code Coverage HHVM

Installation

> composer require lucid/mux

Usage

Creating coute collections

Manualy

<?php

use Lucid\Mux\Route;
use Lucid\Mux\Routes;

$routes = new Routes;
$routes->add('index', new Route('/', 'Acme\FrontController@getIndex'));

Using the Builder

<?php

use Lucid\Mux\RouteCollectionBuilder as Builder;

$builder = new Builder;

// adds a GET route
$builder->get('/', 'Acme\FrontController@getIndex');

// adds a POST route
$builder->post('/user', 'Acme\UserController@createUser');

// adds a UPDATE route
$builder->update('/user/{id}', 'Acme\UserController@updateUser');

// adds a DELETE route
$builder->delete('/user/{id}', 'Acme\UserController@deleteUser');

Dispatching routes

The router component takes a request context object to dispatch the corresponding routing action.

<?php
use Lucid\Mux\Router;
use Lucid\Mux\Request\Context as RequestContext;

$router = new Router($builder->getCollection());

$request = new RequestContext(
    current(explode('?', $_SERVER['REQUEST_URI'])),
    $_SERVER['REQUEST_METHOD']
);

$response = $router->dispatch($request);

Working with PSR-7 requests

You can easily create a requestcontext from an existing psr7 compatible server request by using the Context::fromPsrRequest() method.

<?php
$request = new RequestContext::fromPsrRequest($psrRequest);

Dispatching named routes

<?php

$options = [
    'id' => 12
];

$response = $router->route('user.delete', $options);

Advanced router configuration

The router mostly relies on two main components:

  1. a handler dispatcher, which is responsible for finding and executing the given action (defined on the route object)
  • a response mapper, which is capable of mapping the responsens to a desired type

The handler dispatcher

By default, the handler dispatcher/resolver will check if the given handler is callable. If the handler is a string containing an @ symbol, it is assumed that the left hand side represents a classname and the right hand site a method.

Dependency Injection

If the handler resolver (Lucid\Mux\Handler\Resolver by default) is constructed with an instance of Interop\Container\ContainerInterface it will also check if the left hand side is a service registered by the di container.

<?php

use Lucid\Mux\Handler\Resolver;
use Lucid\Mux\Handler\Dispatcher;

$resolver = new Resolver($container)
$dispatcher = new Dispatcher($resolver);

The response mapper

By default, the response mapper is a simple passthrough mapper. However it's easy to create a custom mapper that suites your specific needs.

<?php

use Zend\Diactoros\Response;

use Lucid\Mux\Request\ResponseMapperInterface.php;

class PsrResponseMapper implements ResponseMapperInterface
{
    public function mapResponse($response)
    {
        return new Response($response);
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固