PHPackages                             bssanchez/phprouter - 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. bssanchez/phprouter

ActiveLibrary

bssanchez/phprouter
===================

Fork of PHPRouter (http://miladrahimi.github.io/phprouter) version 2.3

032PHP

Since Jul 16Pushed 6y agoCompare

[ Source](https://github.com/bssanchez/phprouter23)[ Packagist](https://packagist.org/packages/bssanchez/phprouter)[ RSS](/packages/bssanchez-phprouter/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHPRouter
=========

[](#phprouter)

Free PHP URL router for neat and powerful projects!

Overview
--------

[](#overview)

PHPRouter is a free, neat, powerful and stand-alone URL router for PHP projects.

It's inspired by [Laravel](http://laravel.com/docs/master/routing) routing system and appropriate for no-framework projects and brand new frameworks.

### URL Routing

[](#url-routing)

URL routing means mapping URLs to controllers.

A URL router is a required part for every project/framework with MVC-based architecture.

PHPRouter as a URL router provides pretty URLs.

See following URL which is provided by a weak router:

```
http://example.com/index.php?section=blog&page_type=post&id=93

```

And compare to this one which is provided by PHPRouter:

```
http://example.com/blog/post/93

```

In addition, PHPRouter provides pretty API and easy-to-use methods for you.

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

[](#installation)

### Using Composer (Recommended)

[](#using-composer-recommended)

Read [How to use composer in php projects](http://miladrahimi.com/blog/2015/04/12/how-to-use-composer-in-php-projects)article if you are not familiar with [Composer](http://getcomposer.org).

Run following command in your project root directory:

```
composer require miladrahimi/phprouter

```

### Manually

[](#manually)

You may use your own autoloader as long as it follows [PSR-0](http://www.php-fig.org/psr/psr-0) or [PSR-4](http://www.php-fig.org/psr/psr-4) standards. Just put `src` directory contents in your vendor directory.

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

[](#getting-started)

All of your application requests must be handled by one PHP file like `index.php`. Put following directives in your `.htaccess` file to achieve this goal:

```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [PT,L]

```

Now you can use `Router` class in the PHP file (here `index.php`) to route your application:

```
// Use this namespace
use MiladRahimi\PHPRouter\Router;

// Create brand new Router instance
$router = new Router();

// Map this function to home page
$router->get("/", function () {
    return "This is home page!";
});

// Dispatch all matched routes and run!
$router->dispatch();

```

- Try the example in your server root directory (do not put it in a sub directory).
- You will read how to use PHPRouter in projects in subdirectories in the rest of documentation.

Basic Routing
-------------

[](#basic-routing)

To buy more convenience, most of controllers in the examples in this documentation are defined as closure. Of course, PHPRouter supports plenty of controller types which you will read in the further sections.

There are some simple routes below.

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

// Map this function to home page (GET method)
$router->get("/", function () {
    return "This is home page!";
});

// Map this function to /blog URI (GET method)
$router->get("/blog", function () {
    return "This is blog!";
});

// Map this function to /submit URI (POST method)
$router->post("/submit", function () {
    return "I'm supposed to catch posted data!";
});

// Dispatch routes and run!
$router->dispatch();

```

- All of controllers above are closure functions.
- The `get()` method map controllers to `GET` request methods.
- The `post()` method map controllers to `POST` request methods.

Request methods
---------------

[](#request-methods)

Both `get()` and `post()` methods in `Router` class are shortcuts for mapping controllers to `GET` and `POST` requests. The super method is `map()` which catches request method as its first argument.

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

// Alias: $router->get();
$router->map("GET", "/", function () {
    return "This is Home!";
});

// Alias: $router->post();
$router->map("POST", "/post_data", function () {
    return "Data updated!";
});

// PUT Request method
$router->map("PUT", "/put_data", function () {
    return "Data uploaded!";
});

// DELETE Request method
$router->map("DELETE", "/delete_data", function () {
    return "Data deleted!";
});

// Dispatch routes and run!
$router->dispatch();

```

Multiple request methods
------------------------

[](#multiple-request-methods)

The controller can be mapped to multiple request methods as following example demonstrates:

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

// Map to GET, POST and DELETE request methods
$router->map(["GET", "POST", "DELETE"], "/", function () {
    return "Homepage for GET, POST and DELETE request methods!";
});

$router->dispatch();

```

- PHP &gt;= 5.4 supports `[]` [syntax for array](http://php.net/manual/en/language.types.array.php). you may use old `array()` syntax.

Any request method
------------------

[](#any-request-method)

If the controller can respond to the route with any request method, you may try this method:

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

// Respond to all request methods (GET, POST, PUT, etc)
$router->any("/", function () {
    return "This is Home! No matter what the request method is!";
});

$router->dispatch();

```

Multiple routes
---------------

[](#multiple-routes)

Array of routes is supported too. If the controller can respond to multiple routes, the method below may be useful to you.

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

$router->map(["GET", "POST"], ["/", "/home"], function () {
    return "Homepage for GET, POST and DELETE requests!";
});

$router->dispatch();

```

- The controller above will respond to these request methods and routes:
    - Method: `GET` Route: `/`
    - Method: `GET` Route: `/home`
    - Method: `POST` Route: `/`
    - Method: `POST` Route: `/home`

Controllers
-----------

[](#controllers)

Personally, I hate using closure as a controller. I believe a controller absolutely must be a method. However, following codes shows how to use all kind of controllers.

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

// Closure
$router->get("/1", function () {
    return "Closure as controller";
});

// Stored closure
$closure = function() {
    return "Stored closure as controller";
};
$router->get("/2", $closure);

// Function
function func() {
    return "Function as controller";
}
$router->get("/3", "func");

// Method (Recommended)
class Controller
{
    function method()
    {
        return "Method as controller";
    }
}
$router->get("/4", "Controller@method");

$router->dispatch();

```

Controller class namespaces
---------------------------

[](#controller-class-namespaces)

Because of MVC pattern, developers usually declare controllers with namespaces.

PHPRouter doesn't recognize "used namespaces", so you have to pass them with the class names.

See following example, `Post` class is declared with `Blog` namespace.

```
use MiladRahimi\PHPRouter\Router;

$router = new Router();

$router->get("/blog/posts", 'Controllers\Blog\Post@getAll');

$router->dispatch();

```

An the `Post` class:

```
