Bascially over years I have developed my own framework. What it lacks it’s a central routing system.
I would like to integrate a standalone routing library to my framework, instead to reinvent the wheel.
Is there somewhere a standalone php routing library?
If there isn’t can you suggest any guideline for its developing?
I would like something like F3 framework:
$route->add( 'article/view/[0-9]+' ); //> Call Article->view(); (website.net/article/id/123)
$route->add( 'email', 'email.php' ); //> Run email.php (website.net/email)
Edit
I have developed on my own. Here the example usage:
// index.php
require 'router.php';
$router = new Router();
$router
//> It will require controllers/article.php and call one of the view,etc method
->add('(article)/(view|edit|delete|add)/([0-9]+)')
//> Same thing as before, but this time we use underscore as separator
//> It will require controllers/entry.php and call view method
->add('(entry)_(view)_([0-9]+)')
//> Or you can require custom file like this
->add( '(myCustomPage)' , '/controllers/myCustomPath/myPage.php' )
->dispatch();
If you need a simple controller you can run directy a function without having to specify a class. Example:
// myCustomController.php
function myCustomController($id) {
echo 'I am the Custom Controller';
}
// index.php
$router
->add('(myCustomController)/([0-9]+)');
// The routing system will detect there is a function and will call it directly.
// Otherwise will just instanciate a new myCustomController() object
You can of course use Search Engine Friendly URLs like this:
//> This will match something like this: article/123/your-title-here
->add('(article)/([0-9]+)/[a-z0-9-]+')
You can run custom method from custom controller like this:
->add('(ctrlname)/(methodname)/(params)', array('CustomControllerName','CustomMethod') );
Source: http://pastebin.com/9F02GEyN
You may either use Symfony Routing component, or brilliant klein.php router, which is a Sinatra-inspired tool wrapped in a single PHP file.