Loading repository data…
Loading repository data…
DevAmirul / repository
A simple, lightweight, and powerful PHP Router with rich features like Middleware and Controllers. Heavily inspired by the way Laravel handles routing.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
_____ _____ _
| __ \ | __ \ | |
| |__) ______ | |__) |___ _ _| |_ ___ _ __
| ___/ |______| | _ // _ \| | | | __/ _ \ '__|
| | | | \ \ (_) | |_| | || __/ |
|_| |_| \_\___/ \__,_|\__\___|_|
A simple, lightweight, and powerful PHP Router with rich features like Middleware and Controllers. Heavily inspired by the way Laravel handles routing.
GET POST PUT PATCH and DELETE HTTPS verbsget() post() put() patch() delete() match() any()Installation is possible using Composer.
composer require devamirul/p-router
Add the following script to file:
composer.json"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"scripts": {
"start": [
"php -S 127.0.0.1:8000"
],
"middleware": "cd vendor/devamirul/p-router/src/CLI && php createMiddleware.php && cd -",
"controller": "cd vendor/devamirul/p-router/src/CLI && php createController.php && cd -",
"app": "cd vendor/devamirul/p-router/src/CLI && php createApp.php && cd -"
}
Run the following command:
composer dump-autoload
Create app folder.
composer app
Start PHP server.
composer start
$router->get('/', function () {
echo 'welcome to p-route';
})->name('home');
or
$router->get('/', [WelcomeController::class, 'index'])->name('home');
$router->get('/users/:id', function(){
//
})->where(['id' => '^\d+$'])->name('user');
$router->get('/users/:id', function(){
//
})->middleware('auth')->where(['id' => '^\d+$'])->name('user');
You can do method chaining if you want.
app/config: This folder contains system config files. Make your changes only in the config file.
app/Middlewares: Create your custom middlewares in this folder.
app/Controllers: Create your custom controllers in this folder.
The router allows you to register routes that respond to any HTTP verb:
$router->get($uri, $callback);
$router->post($uri, $callback);
$router->put($uri, $callback);
$router->patch($uri, $callback);
$router->delete($uri, $callback);
$router->match($uri, $callback);
$router->any($uri, $callback);
Routes accept a URI and a closure or a array, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files.
router singleton instance.run() method.<?php
/**
* Define root directory.
*/
define('APP_ROOT', dirname(__FILE__));
/**
* Require composer autoloader.
*/
require __DIR__ . '/vendor/autoload.php';
// Get singleton route instance.
$router = Devamirul\PRouter\Router::singleton();
// Define routes.
$router->get('/greeting', function () {
return 'Hello World';
});
// Resolve and run application.
$router->run();
?>
Or create a separate route.php file and include that file in the index.php file.
First create route.php or name the file according to your choice:
<?php
// Define routes
$router->get('/greeting', function () {
return 'Hello World';
});
$router->fallback(function () {
return 'Fallback route';
});
?>
Include route.php in the index.php file:
/**
* Require composer autoloader.
*/
require __DIR__ . '/vendor/autoload.php';
// Get singleton route instance.
$router = Devamirul\PRouter\Router::singleton();
// Require route.php
require_once './route.php';
// Resolve and run application.
$router->run();
Let's discuss the second parameter. The second parameter accepts a closure or an array of key value pairs. The 'key' of the array will be a class and the value will be a method of the class, the method will be invoked by the class.
use App\Controllers\WelcomeController;
$router->get('/', [WelcomeController::class, 'index'])->name('home');
Use return instead of echo.
// Right way.
$router->get('/greeting', function () {
return 'Hello World';
});
// wrong way.
$router->get('/greeting', function () {
echo 'Hello World';
});
Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition:
Route names should always be unique.
$router->get('/user/profile', function () {
// ...
})->name('profile');
Once you assign a name to a given route, you can redirect via toRoute():
return toRoute('profile');
If the named route defines parameters, you may pass the parameters as the second argument to the toRoute function. The given parameters will automatically be inserted into the generated URL in their correct positions:
$router->get('/user/:id/profile', function () {
// ...
})->name('profile');
return toRoute('profile', ['id' => 1]);
If you pass additional parameters in the array, those key / value pairs will automatically be added to the generated URL's query string:
$router->get('/user/:id/profile', function () {
// ...
})->name('profile');
return toRoute('profile', ['id' => 1, 'photos' => 'yes']);
If you use an asterisk (*) in the route, you cannot call it via toRoute().
Sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You can get it through $request->getParam() method.
$router->get('/user/:id', function (Request $request) {
return 'User ' . $request->getParam('id');
});
You may define as many route parameters as required by your route:
$router->get('/posts/:post/comments/:comment', function (Request $request) {
//Get all parameters array.
return 'User ' . $request->getParam();
//Get specific parameter.
return 'User ' . $request->getParam('post');
});
Route parameters will always start with Colon ':' and should contain alphanumeric characters.
Automatically get 'Request' instances in your route callback or controller.
In callback:
$router->get('/user/:id', function (Request $request) {
return 'User ' . $request->getParam('id');
});
In controller:
$router->get('/user/:id', [UserController::class, 'index']);
namespace App\Http\Controllers;
use Devamirul\PhpMicro\core\Foundation\Application\Request\Request;
use Devamirul\PhpMicro\core\Foundation\Controller\BaseController;
class UserController extends BaseController {
public function index(Request $request) {
return 'User ' . $request->getParam('id');
}
}
Occasionally you may need to specify a route parameter that may not always be present in the URI. You may do so by placing a question sign ? mark after the parameter:
It is important to note that optional parameter are always placed at the end of the URLs.
$router->get('/user/:name?', function () {
//
});
You can restrict the format of your route parameter by using the where method on a route instance.
The where() method takes a regular expression as parameter which determines how the parameter should be delimited. The "where()" method will accept the serialized parameters of the router's dynamic parameters:
$router->get('/user/:id', function () {
// ...
})->where(['id' => '^\d+$']);
Regular expression constraints for optional parameter:
$router->get('/user/:name?', function () {
//
})->where(['name' => '^[a-zA-Z ]*$']);
Regular expression constraints for multiple optional parameters:
$router->get('/profile/:id/user/:name?', function () {
//
})->where(['id' => '^\d+$', 'name' => '^[a-zA-Z ]*$']);
Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:
$router->match(['get', 'post'], '/', function () {
// ...
});
$router->any('/', function () {
// ...
});
You can use dynamic routes using asterisks:
$router->get('admin/*', function () {
// ...
});
In the example above, you can dynamically use any path after admin/. The asterisk is used as a wildcard and matches any combination of characters.
app/Middlewares: Middleware provides a convenient mechanism for inspecting and filtering HTTP requests entering your application.
The predefined middleware files are:- AuthMiddleware.php CsrfMiddleware.php
By default you will get a request instance in the handle method.
To create a new middleware, use the composer middleware command:
composer middleware
The command line interface will ask you for a middleware name, you enter a name. It will automatically add "Middleware" to the name you provided. For example, you want to create a middleware named "example". Then your middleware class will be ExampleMiddleware.php
namespace App\Middlewares;
use Devamirul\PRouter\Interfaces\Middleware;
use Devamirul\PRouter\Request\Request;
class AuthMiddleware implements Middleware {
/**
* Handle an incoming request.
*/
public function handle(Request $request): void {
//
}
}
For example, This framework includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to your application's login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.
public function handle(Request $request): void {
if (!isset($_SESSION['user'])) {
redirect('/login');
}
return;
}
After creating the middleware add it to the middleware array in the 'app/config/middleware.php' file. Add your own middleware to this list and assign it an alias of your choice:
'middleware' => [
'csrf' => Devamirul\PRouter\Middleware\Middlewares\CsrfMiddleware::class,
'auth' => App\Middlewares\AuthMiddleware::class
],
If you would like to assign middle