PHPackages                             focus237/the-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. focus237/the-router

ActiveLibrary

focus237/the-router
===================

Simple PHP Router

v1(7y ago)17PHPPHP 7.\*

Since Dec 20Pushed 7y agoCompare

[ Source](https://github.com/focus237/Router)[ Packagist](https://packagist.org/packages/focus237/the-router)[ RSS](/packages/focus237-the-router/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

The Router Documentation
========================

[](#the-router-documentation)

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

[](#installation)

Add the latest version of the-router project running this command.

```
composer require focus237/the-router

```

Features
--------

[](#features)

- Basic routing (`GET`, `POST`, `RESOURCE`).
- Regular Expression Constraints for parameters.
- Named routes.
- Generating url to routes.
- Namespaces.
- Optional parameters
- Sub-domain routing
- Custom boot managers to rewrite urls to "nicer" ones.
- Input manager; easily manage `GET`, `POST`.

Server Setup
------------

[](#server-setup)

### Setting up Apache

[](#setting-up-apache)

Nothing special is required for Apache to work. We've include the `.htaccess` file in the `root` folder. If rewriting is not working for you, please check that the `mod_rewrite` module (htaccess support) is enabled in the Apache configuration.

#### .htaccess example

[](#htaccess-example)

Below is an example of an working `.htaccess` file used by the-router.

Simply create a new `.htaccess` file in your root directory and paste the contents below in your newly created file. This will redirect all requests to your `index.php` file. According your `index.php` is in `public` folder, you'll write something like this

```
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/index.php?url=$1 [QSA,L]

```

### Setting up Ngninx

[](#setting-up-ngninx)

If you are using Nginx please make sure that url-rewriting is enabled.

You can easily enable url-rewriting by adding the following configuration for the Nginx configuration-file for the demo-project.

```
location / {
    try_files $uri $uri/ /public/index.php?$query_string;
}

```

Configuration
-------------

[](#configuration)

Create a new file, name it `routes.php` and place it in your library folder. This will be the file where you define all the routes for your project.

**WARNING: NEVER PLACE YOUR ROUTES.PHP IN YOUR PUBLIC FOLDER!**

In your `index.php` require your newly-created `routes.php` and call the `$router->run()` method. This will trigger and do the actual routing of the requests.

It's not required, but you can set `TheRouter::setDefaultNamespace('\Controllers\Path');` to prefix all routes with the namespace to your controllers. This will simplify things a bit, as you won't have to specify the namespace for your controllers on each route. The default namespace for controllers is `App\Controller` .

**This is an example of a basic `index.php` file:**

```
