PHPackages                             yahyamallak/masar - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yahyamallak/masar

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yahyamallak/masar
=================

Lightweight PHP router

V1.1.0(1y ago)214MITPHPPHP ^8.0

Since Jul 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/yahyamallak/Masar)[ Packagist](https://packagist.org/packages/yahyamallak/masar)[ RSS](/packages/yahyamallak-masar/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (1)Versions (5)Used By (0)

Masar
=====

[](#masar)

A lightweight and flexible PHP router with support for dynamic route parameters.

[![PHP Version](https://camo.githubusercontent.com/6da9704adc310b64eea60cc463a5e5c8dd738da86ba6c3e2af0cfd4600621755/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e302d626c75652e737667)](https://camo.githubusercontent.com/6da9704adc310b64eea60cc463a5e5c8dd738da86ba6c3e2af0cfd4600621755/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e302d626c75652e737667)

Requirements
------------

[](#requirements)

- PHP 8.0 or higher

Features
--------

[](#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
------------

[](#installation)

Run this command to install the library:

```
composer require yahyamallak/masar
```

Usage
-----

[](#usage)

### 1. Basic Setup

[](#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

[](#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

[](#3-using-where-for-parameters-rules)

#### Supported 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

[](#4-using-named-routes)

#### naming the route :

[](#naming-the-route-)

```
$router->get('/users', function() {

    return "All users.";

})->name("users");
```

#### getting the route name :

[](#getting-the-route-name-)

```
use Masar\Routing\Route;

Route::get("users");
```

### 5. Using Controllers

[](#5-using-controllers)

#### Before using controllers you need to go through some quick steps to tell the router where to find them.

[](#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.

[](#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.

[](#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

[](#step-3--you-define-the-routes-with-controllers)

##### Example 1 :

[](#example-1-)

```
$router->get('/profile/{id}', [UserController::class, "index"]);
```

##### Example 2 :

[](#example-2-)

```
$router->patch('/posts/{id}/edit', "PostController@edit");
```

### 6. Using Middlewares

[](#6-using-middlewares)

```
$router->get("/admin", function() {

    return "Admin dahsboard.";

})->middleware("auth");
```

### 7. Using route grouping

[](#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
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

License
-------

[](#license)

This project is open-sourced software licensed under the MIT license.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

3

Last Release

701d ago

Major Versions

V0.1.0 → V1.0.02024-07-31

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/30195651?v=4)[Yahya Mallak](/maintainers/yahyamallak)[@yahyamallak](https://github.com/yahyamallak)

---

Top Contributors

[![yahyamallak](https://avatars.githubusercontent.com/u/30195651?v=4)](https://github.com/yahyamallak "yahyamallak (36 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/yahyamallak-masar/health.svg)

```
[![Health](https://phpackages.com/badges/yahyamallak-masar/health.svg)](https://phpackages.com/packages/yahyamallak-masar)
```

###  Alternatives

[birgir/geo-query

Modify the WP\_Query to support the geo\_query parameter. Uses the Haversine SQL optimization by Ollie Jones.

683.0k](/packages/birgir-geo-query)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
