eylmz / router
Router - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.
v1.0.0
2017-08-19 07:35 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2025-03-30 06:19:45 UTC
README
Router - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.
Version : 1.0.0
- Installation
- Simple Usage
- Available Router Methods
- Route Parameters
- Controller and Method Parameters
- Regular Expression Constraints
- Named Routes
- Route Groups
- License
Installation
You can download it and using it without any changes.
OR use Composer.
It's recommended that you use Composer to install Route.
$ composer require eylmz/router
Simple Usage
.htaccess
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
index.php
<?php require 'vendor/autoload.php'; // or // require 'src/eylmz/Router.php'; use eylmz/Router/Router; Router::setControllerNamespace("App\\Controllers\\"); Router::setMiddlewareNamespace("App\\Middlewares\\"); // Routers Router::any("/url","Controller@Method"); // or Router::any("/url2",function() { }); // #Routers Router::routeNow(@$_GET["url"]);
Available Router Methods
Router::get($url, $callback); Router::post($url, $callback); Router::put($url, $callback); Router::patch($url, $callback); Router::delete($url, $callback); Router::options($url, $callback);
Usage More Than One Routers
Router::match("GET|POST",$url,$callback); //or Router::match(["GET","POST"],$url,$callback);
Usage The Any Methods
Router::any($url,$callback);
Route Parameters
Required Parameters
Router::get("url\{id}",function($myID){ echo "Hello " . $myID; });
Optional Parameters
Router::get("url\{id?}",function($myID=0){ echo $myID; });
Controller and Method Parameters
// Controller -> First Parameter // Method -> Second Parameter Router::get("admin\{controller}\{method}","{?}@{?}"); // or // Custom Router::get("admin\{method}\{controller}","{controller}@{method}");
Regular Expression Constraints
Router::get('url/{id}', function ($myID) { })->where('id', '[0-9]+'); Router::get('user/{id}/{name}', function ($myID, $name) { })->where(['id' => '[0-9]+', 'name' => '[a-zA-Z]+']);
Named Routes
Router::get('user/profile', function () { // })->name('profile');
Generating URLs To Named Routes
$url = Router::route("profile"); // Usage with parameters Router::get('url/{id}/profile', function ($id) { })->name('profile'); $url = Router::route('profile', ['id' => 1]);
Route Groups
Prefix URL
Router::prefix('admin')->group(function () { Router::get('users', function () { // new url -> /admin/users }); });
Middleware
Router::middleware("middleware")->group(function () { Router::get('/', function () { }); Router::get('url/profile', function () { }); });
Usage More Than One Middlewares
Router::middleware(["middleware","middleware2"])->group(function () { Router::get('/', function () { }); Router::get('url/profile', function () { }); });
License
The MIT License (MIT). Please see License File for more information.