bistro/router 问题修复 & 功能扩展

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

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

bistro/router

Composer 安装命令:

composer require bistro/router

包简介

A route engine for PHP >= 5.3

README 文档

README

A stand-alone routing engine for PHP > = 5.3.

Installation

Packagist all the way!

"require": {
	"bistro/router": "1.0.*"
}

Of course you can always download a zip of the source on GitHub.

Creating Routes

$router = new \Bistro\Router\Router;
$router->add('home', '/')->defaults(array(
	'controller' => 'welcome',
	'action' => 'view'
));

Checking For Matches

$method = $_SERVER['REQUEST_METHOD'];
$uri = isset($_SEVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';

$params = $router->match($method, $uri);
// $params = array('controller' => 'welcome', 'action' => 'view');

If you get an empty array back from match() signifies that there were no matches to the request. Probably 404 time!

Named Route Segements

You can supply named segements for your routes with :{name}.

$router->add('crud', '/:controller/:action/:id?')->defaults(array(
	'id' => null
));

You can put a ? at the end of a named segment to make this segment optional.

Adding Constraints

If you want to add constraints to a named segment you can put a valid regular expression before the :

$router->add('id_only', '/:controller/user|post:action/\d+:id');

In this route the action must be either user or post and the id is an integer.

Method Based Routes

Only want to pick up certain request methods? Just use the helper methods

$router->post('login', '/login')->defaults(array('controller' => 'login', 'action' => 'process'));

The available helper methods are get, post, put and delete.

Wildcard

Want to pick up everything at the end of a url? Easy!

$router->add('wildcard', '/:controller/.*:wildcard')

Adding Request Method Defaults

Building an api and want to add in different parameters for each request method?

$router->add('api', "/:controller/\d+:id?")
	->get(array('action' => 'read'))
	->post(array('action' => 'create'))
	->put(array('action' => 'update'))
	->delete(array('action' => 'delete'));

Reverse Routing

Keep track of your urls in a sane matter with the built in reverse routing functionality.

Seriously... don't hand type urls into your application! This is much easier and allows for greater flexibility.

$router->add('reverse', '/blog/:year/:month/:day');

// Reverse Routing magic!
echo $router->url('reverse', array(
	'year' => 2013,
	'month' => 03,
	'day' => 31
));
// Output: /blog/2013/03/31

Sub-Directory Installation?

If you have installed your app in a subdirectory you can add that information into the router and everything will still work as adverstised.

$router = new \Bistro\Router\Router('subdirectory');

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-04-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固