PHPackages                             devamirul/p-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. [HTTP &amp; Networking](/categories/http)
4. /
5. devamirul/p-router

ActivePackage[HTTP &amp; Networking](/categories/http)

devamirul/p-router
==================

Simple, lightweight, and powerful PHP Router with rich features like Middlewares and Controllers is a simple and useful router class. Heavily inspired by the way Laravel handles routing.

v1.0.11(2y ago)22413[1 issues](https://github.com/DevAmirul/p-router/issues)MITPHPPHP &gt;=8.00

Since Nov 3Pushed 2y agoCompare

[ Source](https://github.com/DevAmirul/p-router)[ Packagist](https://packagist.org/packages/devamirul/p-router)[ RSS](/packages/devamirul-p-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)DependenciesVersions (10)Used By (0)

p-router
========

[](#p-router)

```
  _____            _____             _
 |  __ \          |  __ \           | |
 | |__)   ______  | |__) |___  _   _| |_ ___ _ __
 |  ___/ |______| |  _  // _ \| | | | __/ _ \ '__|
 | |              | | \ \ (_) | |_| | ||  __/ |
 |_|              |_|  \_\___/ \__,_|\__\___|_|

```

A simple, lightweight, and powerful PHP Router with rich features like Middleware and Controllers. Heavily inspired by the way Laravel handles routing.

Features:
---------

[](#features)

- Supports `GET` `POST` `PUT` `PATCH` and `DELETE` HTTPS verbs
- The methods that the router supports are :- `get()` `post()` `put()` `patch()` `delete()` `match()` `any()`
- Named Routes
- Regular expression constraints for parameters
- Fallback method
- Middleware
- CSRF protection
- Controller
- Easy way to manage request
- Helper methods
- Command line interface(CLI)

Table of Contents:
------------------

[](#table-of-contents)

- **[Installation](#Installation)**
- **[Examples](#Examples)**
- **[Directories](#Directories)**
- **[Routes](#Routes)**
- **[Middlewares](#middlewares)**
- **[CSRF Protection](#CSRF-Protection)**
- **[Controllers](#Controllers)**
- **[Request](#Request)**
- **[Handle Html View Content File](#Handle-Html-View-Content-File)**
- **[Handle Form](#Handle-Form)**
- **[Helpers](#Helpers)**

Installation:
-------------

[](#installation)

Installation is possible using Composer.

```
composer require devamirul/p-router
```

Add the following script to `composer.json` file:

```
"autoload": {
    "psr-4": {
        "App\\": "app/"
    }
},
"scripts": {
    "start": [
        "php -S 127.0.0.1:8000"
    ],
    "middleware": "cd vendor/devamirul/p-router/src/CLI && php createMiddleware.php && cd -",
    "controller": "cd vendor/devamirul/p-router/src/CLI && php createController.php && cd -",
    "app": "cd vendor/devamirul/p-router/src/CLI && php createApp.php && cd -"
}
```

Run the following command:

```
composer dump-autoload
```

Create `app` folder.

```
composer app
```

Start PHP server.

```
composer start
```

Examples:
---------

[](#examples)

#### Basic route:

[](#basic-route)

```
$router->get('/', function () {
    echo 'welcome to p-route';
})->name('home');
```

or

```
$router->get('/', [WelcomeController::class, 'index'])->name('home');
```

#### Dynamic Route:

[](#dynamic-route)

```
$router->get('/users/:id', function(){
    //
})->where(['id' => '^\d+$'])->name('user');
```

#### Middleware:

[](#middleware)

```
$router->get('/users/:id', function(){
    //
})->middleware('auth')->where(['id' => '^\d+$'])->name('user');
```

**You can do method chaining if you want.**

Directories:
------------

[](#directories)

`app/config`: This folder contains system config files. Make your changes only in the config file.

`app/Middlewares`: Create your custom middlewares in this folder.

`app/Controllers`: Create your custom controllers in this folder.

Routes:
-------

[](#routes)

### Available Router Methods:

[](#available-router-methods)

The router allows you to register routes that respond to any HTTP verb:

```
$router->get($uri, $callback);
$router->post($uri, $callback);
$router->put($uri, $callback);
$router->patch($uri, $callback);
$router->delete($uri, $callback);
$router->match($uri, $callback);
$router->any($uri, $callback);
```

### Basic Routes:

[](#basic-routes)

Routes accept a URI and a closure or a array, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files.

1. First create index.php file.
2. Define Application root path.
3. Require vendor autoload file.
4. Create `router` singleton instance.
5. Define routes.
6. Run the application via the `run()` method.

```
