承接 davidfricker/router 相关项目开发

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

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

davidfricker/router

Composer 安装命令:

composer require davidfricker/router

包简介

Lightweight PHP OOP web request routing system

README 文档

README

A standalone lightweight web request routing system.

Install

Using composer

composer require DavidFricker/Router

Non composer

This package is PSR-4 compliant so creating your own autoloader should easy if you do not wish to use composer.

Usage example

Before any requests can be routed they must be defined and stored within a RouteContainer object.

use DavidFricker\Router\Capsule\RouteContainer;

$RouteContainer = DavidFricker\Router\Capsule\RouteContainer::init();

Create a static route

The following line will create a static route. Static routes are those without any dynamic components that require parsing. The first argument is the URI the client should navigate to. The second argument is the HTTP method they should use to request the. The third argument can be an anonymous function or a string that identifies a method of a given class.

Directing to a class member

// the method, in this instance `getPage`, should accept a $Request object as its single parameter
$RouteContainer->set('/dashboard', RouteContainer::HTTP_METHOD_GET, 'getPage@Namespace\Vendor\Package\Controller\Dashboard');

Directing to an anonymous function

$RouteContainer->set('/dashboard', RouteContainer::HTTP_METHOD_GET, function($Request){
  echo 'Welcome to your dashboard';
});

Create a dynamic route

Dynamic routes are defined in the same fashion as their static counterparts. However, in the defined route any parts that are surrounded by curly braces will be parsed and passed to the called function through the Request object. An example of format and usage can be seen below.

$RouteContainer->set('/confirm/{token}/complete', RouteContainer::HTTP_METHOD_GET, function($Request){
  echo 'Token: ' . $Request->getParsedUrlParameters('token');
});

Request class reference

getMethod

Getter method for the HTTP request method returns string HTTP request method

getUrlElements

Fetch a single part of the path or all in the form of an array. To fetch all parts in an array supply no arguments. To fetch a single part supply the numeric index of the expected position of the part starting from zero. The path elements are split around '/'.

For example, the Request object for the URI /path/to/page would return the string 'to' when calling getUrlElements(1).

getParameters

Fetch parameters sent with the request. This method acts similarly to the $_REQUEST array. To fetch all parameters in an array supply no arguments. To fetch a single value supply the parameter key (in the same way you would fetch a value from the $_POST or $_GET arrays).

getParsedUrlParameters

Fetch parsed elements of the request path. Returns the parsed value from the request URI using the request route. For example, if the template route path were 'path/to/article/{article_id}', the requested URI were 'path/to/article/4455', then the array [article_id => 4455] would be passed to this method. This would then be accessible by the following method call getParsedUrlParameters('article_id')

Bugs

Bug reports welcome, please included details to reproduce the bugs. Commits are also very welcome for bug fixes, features, or refactoring.

License

Released under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固