All Projects → scrawler-labs → router

scrawler-labs / router

Licence: MIT license
An Fully Automatic RESTful PHP Router

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to router

Cortex-Plugin
Cortex Plugin, a routing system for WordPress
Stars: ✭ 21 (-58.82%)
Mutual labels:  routing, fastroute
Jaguar
Jaguar, a server framework built for speed, simplicity and extensible. ORM, Session, Authentication & Authorization, OAuth
Stars: ✭ 286 (+460.78%)
Mutual labels:  restful, routing
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (+382.35%)
Mutual labels:  restful, routing
yates
YATES (Yet Another Traffic Engineering System)
Stars: ✭ 46 (-9.8%)
Mutual labels:  routing
accountbook server
📔 记账本 Django 后端
Stars: ✭ 20 (-60.78%)
Mutual labels:  restful
Mignis
Mignis is a semantic based tool for firewall configuration.
Stars: ✭ 43 (-15.69%)
Mutual labels:  routing
mobx-router5
Router5 integration with mobx
Stars: ✭ 22 (-56.86%)
Mutual labels:  routing
nepomuk
A public transit router for GTFS feeds (currently only static) written in modern c++
Stars: ✭ 22 (-56.86%)
Mutual labels:  routing
router-example
Use React Router DOM to create a Single Page Application (SPA).
Stars: ✭ 50 (-1.96%)
Mutual labels:  routing
scion-microfrontend-platform
SCION Microfrontend Platform is a TypeScript-based open-source library that helps to implement a microfrontend architecture using iframes.
Stars: ✭ 51 (+0%)
Mutual labels:  routing
rrx
⚛️ Minimal React router using higher order components
Stars: ✭ 69 (+35.29%)
Mutual labels:  routing
rest-api
Laravel restfull api boilerplate
Stars: ✭ 57 (+11.76%)
Mutual labels:  restfull-api
REST API Test Framework Python
REST API Test Framework example using Python requests and flask for both functional and performance tests.
Stars: ✭ 43 (-15.69%)
Mutual labels:  restful
cloudflare-worker-router
A super lightweight router (1.3K) with middleware support and ZERO dependencies for CloudFlare Workers.
Stars: ✭ 144 (+182.35%)
Mutual labels:  routing
angular-httpclient
Angular 15 Example HttpClient
Stars: ✭ 21 (-58.82%)
Mutual labels:  routing
pinecone
Peer-to-peer overlay routing for the Matrix ecosystem
Stars: ✭ 361 (+607.84%)
Mutual labels:  routing
express-mysql-demo
基于node.js + express + mysql实现的restful风格的CRUD简单示例
Stars: ✭ 44 (-13.73%)
Mutual labels:  restful
premiere
A simple way to consume APIs with Javascript.
Stars: ✭ 67 (+31.37%)
Mutual labels:  restful
go-zero
A cloud-native Go microservices framework with cli tool for productivity.
Stars: ✭ 23,294 (+45574.51%)
Mutual labels:  restful
dcompass
A high-performance programmable DNS component aiming at robustness, speed, and flexibility
Stars: ✭ 260 (+409.8%)
Mutual labels:  routing

Scrawler Router



🔥An Fully Automatic, Framework independent, RESTful PHP Router component🔥
🇮🇳 Made in India 🇮🇳

Demo

🤔 Why use Scrawler Router?

  • Fully automatic, you dont need to define single manual route.
  • No configrations , works out of the box with any php project.
  • Stable and used internally within many Corpuvision's projects
  • 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

<?php

use Scrawler\Router\RouteCollection;
use Scrawler\Router\Router;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;


$dir = /path/to/your/controllers;
$namespace = Namespace\of\your\controllers;

$collection = new RouteCollection($dir,$namespace)

/**
* As of v3.2.0 you can now enblae route caching and optionally pass your own PSR 16 implementation
* $collection = new RouteCollection($dir,$namespace,true);
* OR with your own cache adapter
* $cache = new Psr\SimpleCache\CacheInterface(); 
* $collection = new RouteCollection($dir,$namespace,true,$cache);
**/

$router = new Router($collection);
//Optional you can now pass your own Request object to Router for Router to work on
//$router = new Router($collection,Request $request);


//Dispatch route and get back the response
$response = $router->dispatch();
// Or Alternatively you can pass header as optional argument
//$response = $router->dispatch(['content-type' => 'application/json']);


//Do anything with your Response object here
//Probably middleware can hook in here

//send response
$response->send();

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

🦊 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()
    {
    
    }
}

🔄 Redirection

If you want to redirect a request you can return a Symphony redirect response

 use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpFoundation\Session\Session;

 // Inside hello.php
class Hello
{

// set flash messages
    
    public function getAbc()
    {
      // redirect to external urls
      return new RedirectResponse('http://example.com/');

     // Or alternatively you can set your arguments in flashbagk and redirect to internal URL 
     // Note you may use any flash manager to achieve this but as HttpFoundation is already a dependecy of Router here is a exmple with Symfony Session
     $session = new Session();
     $session->start();   
     $session->getFlashBag()->add('notice', 'Profile updated');
     return new RedirectResponse('http://mydomain.com/profile');
    
    }
}

Infact from Scrawler Router 3.1.0 you can directly return object of \Symfony\Component\HttpFoundation\Response from your controller

👏 Supporters

If you have reached here consider giving a star to help this project ❤️ Stargazers repo roster for @scrawler-labs/router

Thank You for your forks and contributions Forkers repo roster for @scrawler-labs/router

🖥️ 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].