定制 slaxweb/router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

slaxweb/router

最新稳定版本:0.6.0

Composer 安装命令:

composer require slaxweb/router

包简介

Router component for the SlaxWeb/Framework

README 文档

README

Build Status

The Router Component is one of the core components of the SlaxWeb/Framework, but it can be used separately as well. It relies on the following components:

The Router helps you route your incoming requests to the correct code, and is one of the essential components in modern web applications, especially those who rely on a single entry point. With a Router, you control where your incoming requests are handled.

Installation

Installation is currently available only through composer, and can be achieved by putting the following into your composer.json file:

"require": {
    "slaxweb/router": "0.4.*@dev"
},
"minimum-stability": "dev"

Usage

Initialization of the Router can be a little bit overwhelming, especially because it relies on two other components, which also need to be initialized separately. Because of this, the Router provides two convenient ways of initializing it, a Factory and a Service Provider if you happen to use the Pimple Dependency Injection Container.

This is just to get you started. Full documentation will follow in the future.

Factory

The Factory provides static methods to initialize the Router properly, and help you get your Route definitions to the Router. This example does not show how to prepare the Config component, which is required by the Logger component. For this please refer to the Config and Logger components.

<?php
use SlaxWeb\Router\Route;
use SlaxWeb\Router\Factory;
use SlaxWeb\Router\Request;
use SlaxWeb\Config\Factory as Config;
use Symfony\Component\HttpFoundation\Response;

require_once "vendor/autoload.php";

$config = Config::init();
// load config, refer to Config component README

// define a route
$route = Factory::newRoute()->set(
    "myUrl",
    Route::METHOD_GET,
    function (Request $request, Response $response) {
        // ...
        $response->setContent("my content");
    }
);
Factory::container($config)->add($route);

// dispatch the request
$response = Factory::response();
Factory::dispatcher($config)->dispatch(Factory::request(), $response);

// and send response to browser
$response->send()

Now if you visit http://yourdomain.com/script.php/myUrl in your browser, you should see 'my content' in your browser.

Service Provider

Using the service provider is quite similar, and can make it a bit easier for you.

<?php
require_once "vendor/autoload.php";

// init the Pimple Container
$container = new Pimple\Container;

// register services
$container->register(new SlaxWeb\Config\Service\Provider);
$container->register(new SlaxWeb\Logger\Service\Provider);
$container->register(new SlaxWeb\Hooks\Service\Provider);
$container->register(new SlaxWeb\Router\Service\Provider);

// load config, refer to Config component README

// defina a route
$route = $container["router.newRoute"]->set(
    "myUrl",
    Route::METHOD_GET,
    function (Request $request, Response $response) {
        // ...
        $response->setContent("my content");
    }
);
$container["routesContainer.service"]->add($route);

// dispatch the request
$container["routeDispatcher.service"]->dispatch(
    $container["request.service"],
    $container["response.service"]
);

// and send response to browser
$container["response.service"]->send();

And if you visit http://yourdomain.com/script.php/myUrl again, you should again see 'my content' in your browser.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-23