PHPackages                             emant/brownie-php - 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. [Framework](/categories/framework)
4. /
5. emant/brownie-php

ActiveLibrary[Framework](/categories/framework)

emant/brownie-php
=================

A tiny routing framework inspired by Express.js

v0.0.2(2y ago)038MITPHP

Since May 29Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/Em-Ant/brownie-php)[ Packagist](https://packagist.org/packages/emant/brownie-php)[ RSS](/packages/emant-brownie-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

BrowniePHP
==========

[](#browniephp)

[![brownie illustration](brownie.png)](brownie.png)

A Tiny PHP Routing Framework
----------------------------

[](#a-tiny-php-routing-framework)

BrowniePHP is a lightweight PHP routing framework, inspired by Express.js, that provides a simple and intuitive way to handle HTTP requests and define routes for your web applications. It aims to be minimalistic yet powerful, allowing you to quickly build RESTful APIs or handle various HTTP methods with ease.

Features
--------

[](#features)

- Define routes using HTTP methods (GET, POST, PATCH, PUT, DELETE)
- Handle middleware functions for request preprocessing
- Extract parameters from route URLs
- Parse JSON request bodies automatically
- Access request context and headers easily
- Simple and easy to integrate with existing PHP projects

Installation
------------

[](#installation)

You can install BrowniePHP using Composer. Run the following command in your project directory:

```
composer require emant/brownie-php
```

Getting Started
---------------

[](#getting-started)

To define a route, create an instance of the `Router` class and use the appropriate method based on the desired HTTP method:

```
use Emant\BrowniePhp\Router;

$router = new Router();

$router->get('/users', function ($ctx) {
    // Handle GET /users request
});

$router->post('/users', function ($ctx) {
    // Handle POST /users request
});

$router->put('/users/{id}', function ($ctx) {
    // Handle PUT /users/{id} request
    // Access the {id} parameter using $ctx['params']['id']
});

// ... Define more routes
```

You can add global middleware functions using the `use` method:

```
$router->use(function ($ctx) {
    // Perform preprocessing logic here
    // Access request context via $ctx array
});
```

The `Router` class provides an `all` method, which allows you to define a route that matches all HTTP methods. This means that any incoming request, regardless of the HTTP method used, will be matched. It provides a convenient way to handle common functionality or apply middleware , regardless of the specific method.

```
$router->all('/common-route', function ($ctx) {
    // Common logic or middleware for all routes
});
```

To start the routing process, call the run method:

```
$router->run();
```

This will match the incoming request to the defined routes and execute the corresponding route handler or middleware functions.

Inside the route handler or middleware functions, the request context is available as an associative array named `$ctx`. You can access various request properties such as method, path, query parameters, request body, and headers through this array.

Mounting a Nested Router
------------------------

[](#mounting-a-nested-router)

The `all` method provided by the `Router` class allows you to mount a nested router on a specific path. This is useful when you want to group related routes together under a common prefix or when you want to delegate the handling of certain routes to a separate router instance.

To mount a nested router, follow these steps:

1. Create an instance of the nested router using the `Router` class constructor.

```
$nestedRouter = new Router();

$nestedRouter->get('/subroute', [$controller, 'index']);
$nestedRouter->post('/subroute', [$controller, 'create']);
// Add more routes as needed...
```

2. Mount the nested router on a specific route of the main router using the all method.

```
$mainRouter = new Router();

$mainRouter->all('/prefix', [$nestedRouter, 'run']);

$mainRouter->get('/home', [$homeController, 'index']);
$mainRouter->post('/users', [$userController, 'create']);
// ...
```

3. Run the main router to start processing incoming requests.

```
$mainRouter->run();
```

Utils Class
-----------

[](#utils-class)

The Utils class in BrowniePHP provides a collection of utility methods that offer functionality commonly used in web development. Here are some key methods available in the class:

`send_json($data)`

This method sends JSON-encoded data as a response to the client. It supports JSONP by checking for a callback parameter in the request URL. If the parameter is present, the response is wrapped in a callback function. Otherwise, the response is sent as standard JSON. This method can be used to send JSON data back to the client in a consistent and structured manner.

`enable_cors($origin = '*')`

This method enables Cross-Origin Resource Sharing (CORS) by setting the necessary headers in the response. It allows requests from different origins to access the resources of your application. The $origin parameter specifies the allowed origin or origins. By default, it is set to '\*', allowing requests from any origin. The method sets the `Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, and Access-Control-Allow-Methods` headers accordingly.

### Error Handlers

[](#error-handlers)

The Utils class also includes error handler methods that simplify the process of returning error responses. These methods facilitate sending error messages and appropriate HTTP status codes to the client.

`server_error($error_type, $description, $status_code)`

This method generates an error response with a specified error type, description, and HTTP status code. It constructs an associative array with the error details and sets the appropriate status code. The error response is then sent as JSON using the `send_json` method.

`unknown_error()`

The unknown\_error method is a convenience wrapper for the `server_error` method. It simplifies handling internal server errors by providing a pre-defined error type, description, and HTTP status code.

`not_found()`

Similar to the `unknown_error` method, `not_found` is a convenience wrapper for the `server_error` method. It simplifies handling resource not found errors by providing a pre-defined error type, description, and HTTP status code.

Contributing
------------

[](#contributing)

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the [GitHub repository](https://github.com/Em-Ant/brownie-php).

License
-------

[](#license)

BrowniePHP is open-source software licensed under the [MIT license](https://opensource.org/license/mit/).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance41

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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 ~196 days

Total

2

Last Release

887d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ae19ac16691a2436931113882e02f959f6f9db2a562e4407159439bdb3ffd2f?d=identicon)[Em-Ant](/maintainers/Em-Ant)

---

Top Contributors

[![Em-Ant](https://avatars.githubusercontent.com/u/12955734?v=4)](https://github.com/Em-Ant "Em-Ant (4 commits)")

---

Tags

microframeworkphprouter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/emant-brownie-php/health.svg)

```
[![Health](https://phpackages.com/badges/emant-brownie-php/health.svg)](https://phpackages.com/packages/emant-brownie-php)
```

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)[laravel/pail

Easily delve into your Laravel application's log files directly from the command line.

91245.3M590](/packages/laravel-pail)[nette/bootstrap

🅱 Nette Bootstrap: the simple way to configure and bootstrap your Nette application.

68435.8M592](/packages/nette-bootstrap)

PHPackages © 2026

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