1. Go to this page and download the library: Download scrawler/router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
scrawler / router example snippets
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
}
//Hello.php
class Hello
{
public function getWorld()
{
return "Hello World";
}
}
class Controller
{
public function methodFunction($arguments1, $arguments2)
{
//Definition goes here
}
}
class User
{
public function getFind($id)
{
//Function definition goes here
}
}
// 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()
{
}
}
// Inside main.php
class Main
{
// Resolves `/`
public function getIndex()
{
}
// Resolves `/abc`
public function getAbc()
{
}
// Resolves `/hello`
public function getHello()
{
}
}
// Inside hello.php
class Hello
{
// Resolves `/hello`
public function getIndex()
{
}
// Resolves `/hello/abc`
public function getAbc()
{
}
}