2017 © Pedro Peláez
 

library pigeon

Intelligent, elegant routing for CodeIgniter

image

jamierumbelow/pigeon

Intelligent, elegant routing for CodeIgniter

  • Wednesday, April 4, 2018
  • by jamierumbelow
  • Repository
  • 7 Watchers
  • 111 Stars
  • 8,579 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 26 Forks
  • 9 Open issues
  • 2 Versions
  • 6 % Grown

The README.md

DEPRECATED: Pigeon

Intelligent, elegant routing for CodeIgniter

No Maintenance Intended, (*1)

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., (*2)

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:, (*3)

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

...and create/edit your composer.json:, (*4)

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

...and install it!, (*5)

$ php composer.phar install

Remember to include Composer's autoload file in index.php:, (*6)

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., (*7)

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():, (*8)

$route = Pigeon::draw();

Remember to do this after you define your routes., (*9)

Basic routing

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

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

The route method also allows a controller#action input:, (*11)

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

You can also pass through an array of the controller and action:, (*12)

$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:, (*13)

$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:, (*14)

$r->resources('posts');

...is the same as:, (*15)

$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., (*16)

Nesting Routes

Pigeon also makes it very easy to nest routes inside other routes:, (*17)

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

The above is equivalent to:, (*18)

$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:, (*19)

$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:, (*20)

$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., (*21)

Then, simply run the phpunit command on the test file:, (*22)

$ 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\-_]+), (*23)

Version 0.1.0 * Initial Release, (*24)

The Versions

04/04 2018

dev-master

9999999-dev https://github.com/jamierumbelow/pigeon

Intelligent, elegant routing for CodeIgniter

  Sources   Download

MIT

The Requires

  • php >=5.3.1

 

rest codeigniter routing routes

25/09 2012

v0.2.0

0.2.0.0 https://github.com/jamierumbelow/pigeon

Intelligent, elegant routing for CodeIgniter

  Sources   Download

MIT

The Requires

  • php >=5.3.1

 

rest codeigniter routing routes