Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/scrawler-labs/router

An Fully Automatic RESTful PHP Router
https://github.com/scrawler-labs/router

fastroute php rest-api restful restful-api router routing torophp

Last synced: about 2 months ago
JSON representation

An Fully Automatic RESTful PHP Router

Awesome Lists containing this project

README

        

Scrawler Router

GitHub Workflow Status

[![Codecov](https://img.shields.io/codecov/c/gh/scrawler-labs/router?style=flat-square)](https://app.codecov.io/gh/scrawler-labs/router)
[![Packagist Version](https://img.shields.io/packagist/v/scrawler/router?style=flat-square)](https://packagist.org/packages/scrawler/router)
[![Packagist Downloads](https://img.shields.io/packagist/dt/scrawler/router?style=flat-square)](https://packagist.org/packages/scrawler/router)
[![Packagist License](https://img.shields.io/packagist/l/scrawler/router?style=flat-square)](https://packagist.org/packages/scrawler/router)


๐Ÿ”ฅAn Fully Automatic, Framework independent, RESTful PHP Router component๐Ÿ”ฅ

๐Ÿ‡ฎ๐Ÿ‡ณ Made in India ๐Ÿ‡ฎ๐Ÿ‡ณ

![Demo](http://g.recordit.co/lvQba4mnyB.gif)

Complete docs can be found [here](https://component.scrawlerlabs.com/router/)

## ๐Ÿค” Why use Scrawler Router?
- Fully automatic, you dont need to define single manual route.
- Support manual route defination for your edge use case.
- No configrations , works out of the box with any php project.
- Stable and well tested.
- Saves lot of time while building RESTful applications


## ๐Ÿ’ป Installation
You can install Scrawler Router via Composer. If you don't have composer installed , you can download composer from [here](https://getcomposer.org/download/)

```sh
composer require scrawler/router
```

## โœจ Setup

Note 4.x release changes the way router handles request and response, if you still wanna continue using old way with symfony components goto [3.x branch](https://github.com/scrawler-labs/router/tree/3.x)

```php
register($dir,$namespace);

/**
* you can now also enblae route caching by passing your own PSR 16 implementation
* $cache = new Psr\SimpleCache\CacheInterface();
* $router->enableCache($cache);
**/

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

//Dispatch route and get back the response
[$status,$handler,$args,$debug] = $router->dispatch($httpMethod,$uri);
switch ($status){
case \Scrawler\Router\Router::NOT_FOUND:
//handle 404 error
// $debug contains extra debug info useful to check failure in automatic routing
break;
case \Scrawler\Router\Router::METHOD_NOT_ALLOWED:
//handle 405 method not allowed
break;
case \Scrawler\Router\Router::FOUND:
//call the handler
$response = call_user_func($handler,...$args);
// Send Response
//echo $response
}

```

Done now whatever request occurs it will be automatically routed . You don't have define a single route


## โœ๏ธ Manual routing
Information on manual routing can be found in [docs](https://component.scrawlerlabs.com/router/)


## ๐ŸฆŠ How it Works?

The automatic routing is possible by following some conventions. Lets take a example lets say a controller Hello

```php

## ๐Ÿ”ฅ How does it do it automatically?

Each request to the server is interpreted by Scrawler Router in following way:

`METHOD /controller/function/arguments1/arguments2`

The controller and function that would be invoked will be

```php

## โ‰๏ธ How should I name my function for automatic routing?

The function name in the controller should be named according to following convention:
`methodFunctionname`
Note:The method should always be written in small and the first word of function name should always start with capital.
Method is the method used while calling url. Valid methods are:

```
all - maps any kind of request method i.e it can be get,post etc
get - mpas url called by GET method
post - maps url called by POST method
put - maps url called by PUT method
delete - maps url called by DELETE method
```
Some eg. of valid function names are:
```
getArticles, postUser, putResource
```
Invalid function names are:
```
GETarticles, Postuser, PutResource
```

## ๐Ÿ  Website home page
Scrawler Router uses a special function name `allIndex()` and special controller name `Main`. So If you want to make a controller for your landing page `\` the controller will be defines as follows
```php
// Inside main.php
class Main
{
// All request to your landing page will be resolved to this controller
// ALternatively you can use getIndex() to resolve only get request
public function allIndex()
{
}
}
```

## ๐ŸŒŸ Main Controller
Class name with `Main` signifies special meaning in Scrawler Router , if you wanna define pages route URL you can use main controler
```php
// Inside main.php
class Main
{
// Resolves `/`
public function getIndex()
{
}

// Resolves `/abc`
public function getAbc()
{

}

// Resolves `/hello`
public function getHello()
{

}
}
```

## ๐Ÿ‘‰ Index function
Just like `Main` controller `allIndex(), getIndex(), postIndex()` etc signifies a special meaning , urls with only controller name and no function name will try to resolve into this function.
```php
// Inside hello.php
class Hello
{
// Resolves `/hello`
public function getIndex()
{

}

// Resolves `/hello/abc`
public function getAbc()
{

}
}
```

## ๐Ÿ‘ Supporters
If you have reached here consider giving a star to help this project โค๏ธ
[![Stargazers repo roster for @scrawler-labs/router](https://reporoster.com/stars/dark/notext/scrawler-labs/router)](https://github.com/scrawler-labs/router/stargazers)

Thank You for your forks and contributions
[![Forkers repo roster for @scrawler-labs/router](https://reporoster.com/forks/dark/notext/scrawler-labs/router)](https://github.com/scrawler-labs/router/network/members)


## ๐Ÿ–ฅ๏ธ Server Configuration

#### Apache

You may need to add the following snippet in your Apache HTTP server virtual host configuration or **.htaccess** file.

```apacheconf
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
```

Alternatively, if youโ€™re lucky enough to be using a version of Apache greater than 2.2.15, then you can instead just use this one, single line:
```apacheconf
FallbackResource /index.php
```

#### IIS

For IIS you will need to install URL Rewrite for IIS and then add the following rule to your `web.config`:
```xml













```

#### Nginx

Under the `server` block of your virtual host configuration, you only need to add three lines.
```conf
location / {
try_files $uri $uri/ /index.php?$args;
}
```

## ๐Ÿ“„ License

Scrawler Router is created by [Pranjal Pandey](https://www.physcocode.com) and released under
the MIT License.