PHPackages                             trulyao/php-router - 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. [API Development](/categories/api)
4. /
5. trulyao/php-router

AbandonedArchivedLibrary[API Development](/categories/api)

trulyao/php-router
==================

A simple express-style router for PHP

2.0.5(3y ago)41.8k21MITPHPPHP &gt;=7.4

Since Jul 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/aosasona/php-router)[ Packagist](https://packagist.org/packages/trulyao/php-router)[ Docs](https://github.com/aosasona/php-router)[ RSS](/packages/trulyao-php-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (9)Used By (1)

PHP Router
==========

[](#php-router)

PHP-Router is a modern, fast, and adaptable composer package that provides express-style routing in PHP without a framework.

This website is powered by this package -&gt; [View site](https://phprouter.herokuapp.com/)

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

[](#installation)

```
composer require trulyao/php-router
```

Create a new dockerized PHP project (contains MySQL, Apache2, PHPMyAdmin) that uses this package by running this Composer command:

```
composer create-project trulyao/php-starter hello-world
```

### Update .htaccess file

[](#update-htaccess-file)

This is very important to get the router working correctly.

> Warning: Depending on your Apache configuration, some headers will not be allowed to come through to your application, this has nothing to do with this package, you just need to enable them in your Apache configuration.

```
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* /index.php
RewriteRule .* - [E=HTTP_CONTENT_TYPE:%{HTTP:Content-Type},L]
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

```

Features
--------

[](#features)

- Open source
- Fast and easy-to-use syntax
- Provides request and response objects
- Supports dynamic routing like `/:id` or `/:id/:name`
- Supports 4 **major** HTTP methods (GET, POST, PUT, DELETE)
- Uses callbacks to handle requests
- Supports custom 404 and 500 error pages
- Supports redirection
- Serves static files, JSON and more
- Helper functions for common tasks like sending JSON, setting status codes etc.
- Function and class based controller and middleware support

Usage
-----

[](#usage)

`index.php`

```

```

`/views` - The directory where your views/controllers/source files are located.

`/demo` - This is the base URL for your application eg. `api` for `/api/*` or `v1` for `/v1/*`.

The `$req` object
-----------------

[](#the-req-object)

The `$req` object contains the request data, it also has helper methods for accessing this data.

- `query("name")` - Returns the query string value for the given name or all query string values if no name is given.
- `body("name")` - Returns the body value for the given name or all body values if no name is given.
- `params("name")` - Returns the params value for the given name or all file values if no name is given.
- `path()` - Get current full request path.
- `headers()` - Get all request headers.
- `header("name")` - Get a single request header value.
- `append($key, $value)` - Append data to the request object.
- `data` - The data array for the request object (useful for passing data to the middleware and callback functions).

> Note: The `$req` object is passed to all middleware and callback functions. If you use any of the response methods like `send()`, the middleware will not move on to the next one.

The `$res` object
-----------------

[](#the-res-object)

The `$res` object is used to control the response and holds data related to responses and just like the request object, it has methods you can use.

- `error($message, $status)` - Send a JSON error response.
- `send($data)` - Send a JSON/HTML response; automatically detected.
- `json($data)` - Send a JSON response.
- `render($file)` - Render a view with the built-in mini template engine, you can also pass in your own data.
- `redirect($path)` - Redirect to another path - eg. `/example/login`
- `status($status)` - Set the status code (defaults to 200, optional)
- `use_engine()` - Enable and use the built-in mini template engine for rendering views.

> More methods will also be added in the future.

You can access these any functions outside your index or main file too by using the namespace `Trulyao\PhpRouter\HTTP`. Also, you are not constrained to any particular coding style or variable naming convention either.

Error Pages
-----------

[](#error-pages)

You can easily add a custom 404, 405 or 500 page by creating a file in the `/views` directory (or wherever your base path is; where your controllers or views are) called `404.php`, `405.php` or `500.php`

Views &amp; Templates
---------------------

[](#views--templates)

In views, you are provided with the following variables by default:

- `query` - The query string values.
- `body` - The request body values.
- `params` - The request params values.
- `headers` - The request headers.
- `data` - User-defined data eg. current user from JWT
- `root_dir` - The current request path.
-

> Note: headers are made case-insensitive while accessing them in views. This package also comes with a templating engine that is turned off by default, you can enable or disable it by passing a boolean value to the `render` method. This templating engine is still experimental and does not support a lot of features yet. Check [this](/examples/views/middleware_view.html) file for an example of how to use it.

You can use any of these variables in a PHP view file by using $query\['field\_name'\], $data\['field\_name'\] etc or in a template file (most likely html) by using the directives like @query('field\_name'), @body().

### Components and Raw Code

[](#components-and-raw-code)

You can execute some PHP codes in views by using the `@php ... @endphp` directives. This is quite limited and useful for codes that don't echo anything like the `session_start()` function as all the output is at the top of the view file.

```
@php
session_start();
@endphp
```

You can also use the `@component` directive to include a component file.

```
@component('component.html')
```

For more examples, check out the [examples](/examples) directory. If you have forked and/or cloned this repo; to start the test server, run `composer run start:dev`, the base URL is `http://localhost:20000/demo`. Here are all the available endpoints:

- \[GET\] `/`
- \[GET\] `/render`
- \[GET\] `/render/middleware`
- \[GET\] `/json`
- \[GET\] `/dynamic/:id`
- \[GET\] `/dynamic/:id/nested`
- \[GET\] `/dynamic/:id/:name`
- \[GET\] `/middleware`
- \[GET\] `/redirect`
- \[POST\] `/:id`
- \[PUT\] `/:id`
- \[DELETE\] `/:id`
- \[GET | POST | PUT | DELETE\] `/chained`

Contribute
----------

[](#contribute)

- Fork the repository on GitHub
- Add your own code
- Create a pull request with your changes highlighted
- For more information, contact me [here](https://twitter.com/trulyao)

> This documentation will be updated as soon as new features and methods are added to the package.
>
> Warning: PHP, like a lot of other languages, runs from top to bottom, to avoid conflicts, put your chained routes at the bottom of the file; it is still fairly unstable and may override your dynamic routes eg. putting `/chained` which is a chained route before `/:id` for a GET request will only direct you to the `/chained` route because it technically matches the latter. Using a URL like `http://domain.com/work#web` will NOT return anything after the `#` symbol, this is a not an issue with this package or PHP, this data is never sent to the server by the browser. See a discussion [here](https://expressionengine.com/forums/topic/210620/get-full-url-with-hashtag)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

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

Total

8

Last Release

1346d ago

Major Versions

1.0.3 → 2.0.02022-08-19

PHP version history (3 changes)1.0.0PHP &gt;=7.0

1.0.2PHP &gt;=7.1

2.0.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/b34ea1a48c2f04d0900dc41e4632e57450d4e52566f1ace92d1f1c05ba5ab117?d=identicon)[trulyao](/maintainers/trulyao)

---

Top Contributors

[![aosasona](https://avatars.githubusercontent.com/u/97124713?v=4)](https://github.com/aosasona "aosasona (3 commits)")

---

Tags

composercomposer-packagephpphpapirouterexpress

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/trulyao-php-router/health.svg)

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

###  Alternatives

[jstolpe/instagram-graph-api-php-sdk

Instagram Graph API PHP SDK

13998.4k2](/packages/jstolpe-instagram-graph-api-php-sdk)

PHPackages © 2026

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