• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    PHP
  • Created over 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⛔️DEPRECATED Intelligent, elegant routing for CodeIgniter

DEPRECATED: Pigeon

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

More Repositories

1

codeigniter-base-model

⛔️DEPRECATED CodeIgniter base CRUD model to remove repetition and increase productivity
PHP
1,049
star
2

codeigniter-base-controller

⛔️DEPRECATED CodeIgniter base controller with view autoloading and layout support
PHP
113
star
3

codeigniter-schema

⛔️DEPRECATED Expressive table definitions
PHP
89
star
4

julian

⛔️DEPRECATED Brilliantly clever PHP calendar class
PHP
87
star
5

sassphp

PHP bindings to libsass - fast, native Sass parsing in PHP!
C
43
star
6

basic

A simple BASIC interpreter written in PHP
PHP
27
star
7

taggable

The best ExpressionEngine tagging engine
PHP
20
star
8

inferno

Quick, lightweight and flexible xUnit-flavoured PHP unit testing
PHP
20
star
9

sherlock

Asset pipelining so simple, it's elementary
PHP
19
star
10

sp_title_filler

Because sometimes you just don't nEEd a title
PHP
12
star
11

sp_table_select

Populate a dropdown with the contents of any table in your database
PHP
11
star
12

jquery.unique-element-id

A simple jQuery plugin to get a totally unique ID for any element
JavaScript
9
star
13

jquery.computed-style

A simple jQuery plugin to fetch the computed CSS styles for any element.
JavaScript
8
star
14

mojoblog

Simple, sexy blogging for MojoMotor
PHP
7
star
15

godfiles

Feel like the Lord almighty when you $ bash
6
star
16

pipes

Clear, easy to use and sophisticated PHP package management
PHP
6
star
17

presenters

Clean up your views with PHP presenters
PHP
6
star
18

postmaster

A tiny SMTP server that stores and exposes your emails in an HTTP API for testing
CoffeeScript
5
star
19

acts_as_subscribable

A loose wrapper around ActiveRecord that simplified subscriptions
Ruby
5
star
20

youoweme

Harass and get paid by friends and family members
Ruby
4
star
21

peepcode-downloader

Download your accessible PeepCode files with ease!
Ruby
4
star
22

blocks.markdown

A small Twig template filter for parsing text as Markdown within the Blocks CMS
PHP
4
star
23

porter

Lightweight, framework-agnostic PHP authorisation system
PHP
3
star
24

traversable_string

Traverse strings like a boss
Ruby
3
star
25

revisionhub

Collaborate and share revision notes
Ruby
3
star
26

hoipolloi

Treat your tweets like your inbox
Ruby
2
star
27

tailer-cowen

Searchable embeddings database for Marginal Revolution
Python
2
star
28

revisionhub-notes

All the revision notes you'll ever need
2
star
29

sp_select_pages

A dropdown for page selection in Low Variables
2
star
30

pipesphp.org

The source code to the Pipes project
PHP
2
star
31

housing-completions-vs-price-per-square-metre-by-local-authority

Jupyter Notebook
1
star
32

pipes-test-pipe

A quick example/test PHP library for Pipes
PHP
1
star
33

pherialize

A PHP serialize() specification and parser in Ruby
Ruby
1
star
34

tuxedo

Give your forms some jazz
PHP
1
star
35

laravel-ideal-hmvc-structure

PHP
1
star
36

gitstarted

Get started with contributing to open source
Vue
1
star
37

alchemy

An acceptance testing tool for Algolia indexes
Go
1
star
38

jamierumbelow.github.io

HTML
1
star
39

crudcontroller

⛔️DEPRECATED An extensible, simple base CRUD controller for Laravel 5 applications
PHP
1
star