定制 jamierumbelow/pigeon 二次开发

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

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

jamierumbelow/pigeon

最新稳定版本:v0.2.0

Composer 安装命令:

composer require jamierumbelow/pigeon

包简介

Intelligent, elegant routing for CodeIgniter

README 文档

README

Intelligent, elegant routing for CodeIgniter

No Maintenance Intended

CodeIgniter's routing engine is far too basic. Pigeon wraps around the core routing system to provide HTTP method based routing, RESTful resources and nested routes. It uses a natural DSL to make writing cleverer routes simple and elegant.

Synopsis

Pigeon::map(function($r){
	$r->route('posts/(:num)', 'posts/show/$1');

	$r->get('posts', array( 'Posts', 'index' ));
	$r->post('posts', 'Posts#create' );
	$r->put('posts/(:num)', array( 'Posts', 'update' ));
	$r->delete('posts/(:num)', array( 'Posts', 'delete' ));

	$r->resources('posts');

	$r->resources('posts', function($r){
		$r->resources('comments');
	});
});

$route = Pigeon::draw();

Installation

Install with Composer. Install Composer for your project:

$ curl -s http://getcomposer.org/installer | php

...and create/edit your composer.json:

{
    "require": {
        "jamierumbelow/pigeon": "*"
    }
}

...and install it!

$ php composer.phar install

Remember to include Composer's autoload file in index.php:

require_once './vendor/autoload.php';

Alternatively, download and drag the Pigeon.php file into your application/libraries folder. Autoload the library and away you go.

How It Works

You define your routes using Pigeon's DSL inside your config/routes.php file. Pigeon builds up the routes array internally; to expose it to CodeIgniter, you need to set the standard $route variable using draw():

$route = Pigeon::draw();

Remember to do this after you define your routes.

Basic routing

The most basic routing mechanism is the route method. You can pass through a traditional CodeIgniter routing pattern here:

$r->route('posts/(:num)', 'posts/show/$1');

The route method also allows a controller#action input:

$r->route('posts/(:num)', 'posts#show');

You can also pass through an array of the controller and action:

$r->route('posts/(:num)', array( 'Posts', 'show' ));

HTTP Verb Routing

Pigeon also allows you to only route to a certain function when an HTTP verb is used. This is particularly useful when creating a RESTful system:

$r->get('posts/(:id)', 'posts/show/$1');
$r->post('posts', 'posts/create');
$r->put('posts/(:id)', 'posts/update/$1');
$r->delete('posts/(:id)', 'posts/delete/$1');
$r->patch('posts/(:id)', 'posts/delete/$1');
$r->head('posts/(:id)', 'posts/delete/$1');
$r->options('posts/(:id)', 'posts/delete/$1');

RESTful Resources

Pigeon supports RESTful resource based routes. A simple call to resources will generate a bunch of routes to facilitate a RESTful style application:

$r->resources('posts');

...is the same as:

$r->get('posts', 'posts/index');
$r->get('posts/new', 'posts/create_new');
$r->get('posts/(:any)/edit', 'posts/edit/$1');
$r->get('posts/(:any)', 'posts/show/$1');
$r->post('posts', 'posts/create');
$r->put('posts/(:any)', 'posts/update/$1');
$r->delete('posts/(:any)', 'posts/delete/$1');

You can also define a singular resource with resource.

Nesting Routes

Pigeon also makes it very easy to nest routes inside other routes:

$r->route('posts/(:num)', 'posts#show', function($r){
	$r->route('comments', 'comments#show');
});

The above is equivalent to:

$route['posts/(:num)'] = 'posts/show/$1';
$route['posts/(:num)/comments'] = 'comments/show/$1';

The nesting engine is clever enough to account for parameters in the URL and any further params in nested URLs, which feels very similar to the usual way we write our routes in CI:

$r->route('posts/(:num)', 'posts#show', function($r){
	$r->route('files/(:num)', 'files#show');
});

This will grab the post ID from the post URL and pass it through:

$route['posts/(:num)'] = 'posts/show/$1';
$route['posts/(:num)/files/(:num)'] = 'files/show/$1/$2';

Unit Tests

Install PHPUnit. I'm running version 3.6.10.

Then, simply run the phpunit command on the test file:

$ phpunit tests/Pigeon_test.php

Changelog

Version 0.2.0

  • Adjusted the resource routing to route to create_new rather than new
  • Replaced (:any) in resource routing with ([a-zA-Z0-9\-_]+)

Version 0.1.0

  • Initial Release

统计信息

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

GitHub 信息

  • Stars: 106
  • Watchers: 8
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-09-25