Route - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.
It's recommended that you use Composer to install Route.
Route requires PHP 5.4.0 or newer.
Create an index.php file with the following contents:
<?php
define('DS', DIRECTORY_SEPARATOR, true);
define('BASE_PATH', __DIR__ . DS, TRUE);
require BASE_PATH.'vendor/autoload.php';
$app = System\App::instance();
$app->request = System\Request::instance();
$app->route = System\Route::instance($app->request);
$route = $app->route;
$route->any('/', function() {
echo 'Hello World';
});
$route->end();
If using apache make sure the .htaccess file has exists beside index.php
Routing is done by matching a URL pattern with a callback function.
The callback can be any object that is callable. So you can use a regular function:
Or a class method:
class home
{
function pages(){
echo 'Home page Content';
}
}
$route->get('/', ['home', 'pages']);
// OR
$home = new home;
$route->get('/', [$home, 'pages']);
// OR
$route->get('/', 'home@pages');
$route->any('/', function(){
// Any method requests
});
$route->get('/', function(){
// Only GET requests
});
$route->post('/', function(){
// Only POST requests
});
$route->put('/', function(){
// Only PUT requests
});
$route->patch('/', function(){
// Only PATCH requests
});
$route->delete('/', function(){
// Only DELETE requests
});
// You can use multiple methods. Just add _ between method names.
$route->get_post('/', function(){
// Only GET and POST request
});
// This example will match any page name.
$route->get('/?', function($page){
echo "you are in $page";
});
// This example will match anything after post/ - limited to 1 argument.
$route->get('/post/?', function($id){
// Will match anything like post/hello or post/5 ...
// But not match /post/5/title
echo "post id $id";
});
// More than parameters
$route->get('/post/?/?', function($id, $title){
echo "post id $id and title $title";
});
For “unlimited” optional parameters, you can do this:
// This example will match anything after blog/ - unlimited arguments
$route->get('/blog/*', function(){
// [$this] instanceof ArrayObject so you can get all args by getArrayCopy()
pre($this->getArrayCopy());
pre($this[1]);
pre($this[2]);
});
Array ( [0] => /post/10 [1] => post [2] => 10 )
post
10
You can specify named parameters in your routes which will be passed along to your callback function.
You can validate the args by regular expressions.
// Validate args by regular expressions uses :(your pattern here)
$route->get('/{username}:([0-9a-z_.-]+)/post/{id}:([0-9]+)',
function($username, $id)
{
echo "author $username post id $id";
});
// You can add named regex pattern in route
$route->addPattern([
'username' => '/([0-9a-z_.-]+)',
'id' => '/([0-9]+)'
]);
// Now you can use named regex
$route->get('/{username}:username/post/{id}:id', function($username, $id){
echo "author $username post id $id";
});
Some named regex pattern already registered in routes
[
'int' => '/([0-9]+)',
'multiInt' => '/([0-9,]+)',
'title' => '/([a-z_-]+)',
'key' => '/([a-z0-9_]+)',
'multiKey' => '/([a-z0-9_,]+)',
'isoCode2' => '/([a-z]{2})',
'isoCode3' => '/([a-z]{3})',
'multiIsoCode2' => '/([a-z,]{2,})',
'multiIsoCode3' => '/([a-z,]{3,})'
]
You can specify named parameters that are optional for matching by adding (?)
$route->get('/post/{title}?:title/{date}?',
function($title, $date){
if($title){
echo "<h1>$title</h1>";
}else{
echo "<h1>Posts List</h1>";
}
if($date){
echo "<small>Published $date</small>";
}
});
$route->group('/admin', function()
{
// /admin/
$this->get('/', function(){
echo 'welcome to admin panel';
});
// /admin/settings
$this->get('/settings', function(){
echo 'list of settings';
});
// Nested group
$this->group('/users', function()
{
// /admin/users
$this->get('/', function(){
echo 'list of users';
});
// /admin/users/add
$this->get('/add', function(){
echo 'add new user';
});
});
// Anything else
$this->any('/*', function(){
pre("Page ( {$this->app->request->path} ) Not Found", 6);
});
});
Groups with parameters
$route->group('/{lang}?:isoCode2', function($lang)
{
$default = $lang;
if(!in_array($lang, ['ar', 'en'])){
$default = 'en';
}
$this->get('/', function($lang) use($default){
echo "lang in request is $lang<br>";
echo "include page_{$default}.php";
});
$this->get('/page/{name}/', function($lang, $name)
{
pre(func_get_args());
pre($this->app->request->args);
});
});
Array ( [0] => [1] => about )
Array ( [lang] => [name] => about )
Array ( [0] => ar [1] => about )
Array ( [lang] => ar [name] => about )
$route->use(function (){
$req = app('request');
pre('Do something before all routes', 3);
});
$route->before('/', function (){
pre('Do something before all routes', 4);
});
$route->before('/*!admin', function (){
pre('Do something before all routes except admin', 4);
});
$route->before('/admin|home', function (){
pre('Do something before admin and home only ', 2);
});
$route->after('/admin|home', function (){
pre('Do something after admin and home only', 6);
});
$route->get('/admin', function (){
pre('it`s the admin page');
});
Do something before all routes
Do something before all routes
Do something before admin and home only
it`s the admin page
Do something after admin and home only
<?php
namespace App\Controller;
class testController{
// POST or GET controller/
public function post_getIndex(){
// do something here
}
// GET controller/home
public function getHome(){
// do something here
}
// GET controller/contact
public function getContact(){
// do something here
}
// POST controller/contact
public function postContact(){
// do something here
}
// GET controller/news
public function getNews(){
// do something here
}
// POST or PUT controller/news
public function post_putNews(){
// do something here
}
}
<?php
namespace App\Controller;
class testResource{
// GET resource/
public function index(){
// do something here
}
// GET resource/get
public function get(){
// do something here
}
// GET resource/create
public function create(){
// do something here
}
// POST resource/
public function store($id){
// do something here
}
// GET resource/{id}
public function show($id){
// do something here
}
// GET resource/{id}/edit
public function edit($id){
// do something here
}
// PUT resource/{id}
public function update($id){
// do something here
}
// DELETE resource/{id}
public function delete($id){
// do something here
}
}
$route->any('/page/{pagename}', function() {
echo route('page.show') .'<br>';
echo route('page.show', ['pagename' => 'ThePageName']);
})->as('page.show');
$route->group('/{lang}', function($lang) {
$this->get('/', function($lang) {
echo route('page.show', [
'lang' => 'ar',
'name' => 'ThePageName'
]);
})->as('home');
$this->get('/page/{name}/', function($lang, $name) {
echo route('home', ['lang' => $name]);
})->as('page.show');
});
pre(); // print string or array for debug
dpre(); // the same pre() with die();
br(); // echo string with new line by <br> html tag
app(); // app instance
route();// shortcut for Route::getRoute()
url(); // OR URL constant get domain url
// Variables
app()->x = 'something';
echo app()->x; // something
// OR
echo app('x'); // something
// Functions
app()->calc = function($a, $b){
echo $a + $b;
}
echo app()->calc(5, 4); //9
// Classes
class myClass {
}
app()->myClass = new myClass;
pre( app('myClass') );
app('request')->server; //$_SERVER
app('request')->path; // uri path
app('request')->hostname;
app('request')->servername;
app('request')->port;
app('request')->protocol; // http or https
app('request')->url; // domain url
app('request')->curl; // current url
app('request')->extension; // get url extension
app('request')->headers; // all http headers
app('request')->method; // Request method
app('request')->query; // $_GET
app('request')->body; // $_POST and php://input
app('request')->args; // all route args
app('request')->files; // $_FILES
app('request')->cookies; // $_COOKIE
app('request')->ajax; // check if request is sent by ajax or not
app('request')->ip(); // get client IP
app('request')->browser(); // get client browser
app('request')->platform(); // get client platform
app('request')->isMobile(); // check if client opened from mobile or tablet
// You can append vars functions classes to request class
app('request')->addsomething = function(){
return 'something';
};
Use namespace like folder name and make sure the file name is same the class name.