https://github.com/infostreams/php-rails-routing
Rails like routing for PHP
https://github.com/infostreams/php-rails-routing
php router
Last synced: about 1 year ago
JSON representation
Rails like routing for PHP
- Host: GitHub
- URL: https://github.com/infostreams/php-rails-routing
- Owner: infostreams
- License: mit
- Created: 2012-10-11T08:18:57.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2015-11-27T08:48:04.000Z (over 10 years ago)
- Last Synced: 2023-03-30T06:15:01.165Z (over 3 years ago)
- Topics: php, router
- Language: PHP
- Size: 14.6 KB
- Stars: 8
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Rails like routing for PHP
==========================
Installation
------------
First, navigate to the directory that contains the script that you want to use
as the main entry point for your application.
Basically, the installation comes down to: setup the .htaccess file, and
include 'router.php' from your main script.
In smaller steps, this would translate to:
1. Put router.php somewhere where it can be included from that script
2. Copy 'htaccess.txt' to the directory that contains your entry script
3. Rename the 'htaccess.txt' file to '.htaccess'
4. The default 'htaccess.txt' file assumes your main script is index.php. If that's not the case, change accordingly.
5. Include the 'router.php' file from your main script
6. Set up the routes. Examples below or in 'example/index.php'
Examples
--------
(these examples assume the script is running in the 'example/' subdirectory)
require('router.php');
$r = new Router();
// The default page people will see, e.g. this is a
// mapping for http:///example
// -> runs HelloController->overview()
$r->map("", "Hello::overview");
// mapping for http:///example/hello/en
// -> runs HelloController->world()
$r->map("hello", "Hello::world");
$r->map("hello/en", "Hello::world");
// mapping for http:///example/hello/fr
// -> runs HelloController->monde()
$r->map("hello/fr", "Hello::monde");
// mapping for http:///example/.
// -> runs FileController->download($filename, $ext)
// where $filename matches and $ext is either 'txt' or 'json'
$r->map(":filename\.:ext",
"File::download",
// regular expressions determine what is valid for 'filename' and 'ext'
array("filename"=>'[\w\d_-]+', "ext"=>"(txt|json)"));
// generic mapping for http:///example//
// -> for example http:///example/person/all will run
// PersonController->all()
// -> or http:///example/organisation/add will run
// OrganisationController->add()
$r->map(":controller/:action");
// generic mapping for http:///example//
// -> for example http:///example/person/2 will run
// PersonController->view(2)
// -> or http:///example/organisation/3 will run
// OrganisationController->view(3)
$r->map(":controller/:id",
array('action'=>'view'),
array("id"=>"[0-9]+")); // only allow numeric values for 'id'
// any other initialization you need
// Make sure to or require() your controller classes' files prior to $r->run()
// (same-file controllers can go at the bottom of index.php)
$r->run();
?>
Changelog
---------
Based on http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/
but extended in significant ways:
1. Can now be deployed in a subdirectory, not just the domain root
2. Will now call the indicated controller & action. Named arguments are
converted to similarly method arguments, i.e. if you specify :id in the
URL mapping, the value of that parameter will be provided to the method's
'$id' parameter, if present.
3. Will now allow URL mappings that contain a '?' - useful for mapping JSONP urls
4. Should now correctly deal with spaces (%20) and other stuff in the URL