scrawler / router
An Fully Automatic RESTful PHP Router.
Installs: 1 646
Dependents: 3
Suggesters: 0
Security: 0
Stars: 54
Watchers: 4
Forks: 9
Open Issues: 2
Requires
- psr/simple-cache: ^3.0
- thecodingmachine/safe: ^2.5
Requires (Dev)
- pestphp/pest: ^3.0
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.0
- rector/rector: ^1.2
- symfony/cache: ^7.1
- thecodingmachine/phpstan-safe-rule: ^1.2
- dev-master
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.0
- 3.x-dev
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.0
- V3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.1
- v2.2.0
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- dev-dependabot/composer/symfony/cache-7.2.4
- dev-dependabot/composer/symfony/cache-7.2.3
- dev-dependabot/composer/pestphp/pest-3.7.4
- dev-renovate/phpstan-packages
- dev-renovate/symfony
- dev-renovate/pestphp-pest-3.x-lockfile
This package is auto-updated.
Last update: 2025-03-03 10:44:43 UTC
README
Scrawler Router
🔥An Fully Automatic, Framework independent, RESTful PHP Router component🔥
🇮🇳 Made in India 🇮🇳
Complete docs can be found here
🤔 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
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
<?php use Scrawler\Router\Router; $dir = '/path/to/your/controllers'; $namespace = 'Namespace\of\your\controllers'; $router = new Router(); // Register your directory for automatic routing $router->register($dir,$namespace); /** * you can now also enable 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
🦊 How it Works?
The automatic routing is possible by following some conventions. Lets take a example lets say a controller Hello
<?php //Hello.php class Hello { public function getWorld() { return "Hello World"; } }
now calling localhost/hello/world
from your browser you will see hello world
on your screen.
🔥 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 class Controller { public function methodFunction($arguments1, $arguments2) { //Definition goes here } }
For Example the following call:
GET /user/find/1
would invoke following controller and method
<?php class User { public function getFind($id) { //Function definition goes here } }
In above example 1
will be passed as argument $id
⁉️ 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
// 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
// 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.
// 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 ❤️
Thank You for your forks and contributions
🖥️ Server Configuration
Apache
You may need to add the following snippet in your Apache HTTP server virtual host configuration or .htaccess file.
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:
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 version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rule name="Toro" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> <add input="{R:1}" pattern="^(index\.php)" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="/index.php/{R:1}" /> </rule> </rewrite> </system.webServer> </configuration>
Nginx
Under the server
block of your virtual host configuration, you only need to add three lines.
location / {
try_files $uri $uri/ /index.php?$args;
}
📄 License
Scrawler Router is created by Pranjal Pandey and released under the MIT License.