yahyamallak / masar
Lightweight PHP router
V1.1.0
2024-08-01 00:08 UTC
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^11.3@dev
This package is auto-updated.
Last update: 2025-07-08 19:10:53 UTC
README
A lightweight and flexible PHP router with support for dynamic route parameters.
Requirements
- PHP 8.0 or higher
Features
- Simple and intuitive API
- Support for ( GET / POST / PUT / PATCH / DELETE ) requests
- Dynamic route parameters
- Support for parameters rules
- Easy to integrate and extend
- Support for named routes
- Support for controllers
- Support for middlewares
Installation
Run this command to install the library:
composer require yahyamallak/masar
Usage
1. Basic Setup
require_once dirname(__DIR__) . "/vendor/autoload.php"; use Masar\Exceptions\NotFoundException; use Masar\Http\Request; use Masar\Routing\Router; $router = new Router(); // Define routes $router->get('/', function() { return "Welcome to the homepage!"; }); $router->get('/about', function() { return "About us page"; }); $router->post('/submit', function() { return "Form submitted"; }); $router->put('/users/{id}/change', function($id) { return "User " . $id . " has been edited."; }); $router->patch('/users/{id}/edit', function($id) { return "edit name of user : " . $id; }); $router->delete('/users/{id}/delete', function($id) { return "delete user " . $id; }); // Create a request object $request = new Request(); // Dispatch the router try { $router->dispatch($request); } catch (NotFoundException $e) { echo $e->getMessage(); }
2. Using Route Parameters
You can define dynamic segments in your routes using curly braces:
$router->get('/user/{id}', function($id) { return "User profile for user with ID: " . $id; }); $router->get('/post/{slug}', function($slug) { return "Displaying post: " . $slug; });
3. Using Where For Parameters Rules
Supported Rules :
- ":digit"
- ":number"
- ":letter"
- ":word"
- ":slug"
$router->get('/user/{id}', function($id) { return "User profile for user with ID: " . $id; })->where(["id" => ":number"]);
4. Using Named Routes
naming the route :
$router->get('/users', function() { return "All users."; })->name("users");
getting the route name :
use Masar\Routing\Route; Route::get("users");
5. Using Controllers
Before using controllers you need to go through some quick steps to tell the router where to find them.
Step 1 : Create a config file or just an array with some configuration.
You create an associative array with keys ( controllers | middlewares ) and as values you put their namespaces.
$config = [ "controllers" => "App\Controllers", "middlewares" => "App\Middlewares" ];
Step 2 : You pass the configuration to the router.
All you got to do is to give the config array to the router and it will handle the rest.
$router = new Router($config);
Step 3 : You define the routes with controllers
Example 1 :
$router->get('/profile/{id}', [UserController::class, "index"]);
Example 2 :
$router->patch('/posts/{id}/edit', "PostController@edit");
6. Using Middlewares
$router->get("/admin", function() { return "Admin dahsboard."; })->middleware("auth");
7. Using route grouping
Example 1 :
$router->middleware("auth")->group(function() use($router) { $router->get("/", [HomeController::class, "index"]); $router->get("/about", [AboutController::class, "index"]); });
Example 2 :
$router->middleware(["auth", "role"])->group(function() use($router) { $router->get("/", [HomeController::class, "index"]); $router->get("/profile", [UserController::class, "profile"]); });
Example 3 :
$router->prefix("admin")->group(function() use($router) { $router->get("/", [AdminController::class, "index"]); $router->get("/settings", [AdminController::class, "settings"]); });
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is open-sourced software licensed under the MIT license.