1. Go to this page and download the library: Download sigmaphp/sigmaphp-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/ */
sigmaphp / sigmaphp-router example snippets
igmaPHP\Router\Router;
function sayHello() {
echo "Hello World\n";
}
$routes = [
[
'name' => 'greeting',
'path' => '/hello',
'method' => 'get',
'action' => 'sayHello',
],
];
// initialize the router
$router = new Router($routes);
// fire the router
$router->run();
igmaPHP\Router\Router;
$webRoutes = lize the router
$router = new Router(array_merge($webRoutes, $apiRoutes));
// fire the router
$router->run();
igmaPHP\Router\Router;
$routes = [
[
'name' => 'users.profile',
'path' => '/users/profile',
'method' => 'get',
'controller' => UserController::class,
'action' => 'profile',
],
];
// define app base path
const BASE_PATH = '/my-app';
// initialize the router
$router = new Router($routes, BASE_PATH);
// fire the router
$router->run();
..... In ProductController.php
public function list($id = null) {
...
}
curl http://localhost/products?page=2&per_page=10
// so , later in your ProductController inside the list method , you can access these parameters :
public function list($id = null)
{
echo $_GET['page']; // 2
echo $_GET['per_page']; // 10
}
$routes = [
[
'name' => 'about_page',
'path' => '/about',
'method' => 'get',
'action' => 'create_about_page',
],
];
.... somewhere in your application define the function and call it either in the same index.php or another file
// pages.php
function create_about_page() {
print "About Us";
}
// SendEmailController.php
class SendEmailController
{
public function __invoke()
{
// .... some code to send email
}
}
class AuthMiddleware
{
public function handler()
{
session_start();
if (empty($_SESSION['user])) {
header('Location: http://example.com');
exit();
}
}
}
igmaPHP\Router\Router;
// initialize the router
$router = new Router($routes);
// set default middleware's methods
$router->setDefaultMiddlewareMethodName('handle');
// fire the router
$router->run();
igmaPHP\Router\Router;
$routes = [
[
'name' => 'users.profile',
'path' => '/users/profile',
'method' => 'get',
'controller' => UserController::class,
'action' => 'profile',
],
];
// initialize the router
$router = new Router($routes);
// set custom 404 (Page Not Found) handler
$router->setPageNotFoundHandler(MyCustomPageNotFoundHandler::class);
// fire the router
$router->run();
use SigmaPHP\Router\Interfaces\PageNotFoundHandlerInterface;
class MyCustomPageNotFoundHandler implements PageNotFoundHandlerInterface
{
public function handle()
{
echo "My Custom Page Not Found Handler !\n";
}
}
namespace SigmaPHP\Router\Interfaces;
/**
* Runner Interface
*/
interface RunnerInterface
{
/**
* Execute the route's action.
*
* @param array $route
* @return void
*/
public function execute($route);
}
namespace MyApp\Util;
use SigmaPHP\Router\Interfaces\RunnerInterface;
class MyCustomRunner implements RunnerInterface
{
/**
* Execute the route's action.
*
* @param array $route
* @return void
*/
public function execute($route)
{
// here we define our execution logic , save logs
// connect to DB , inject dependencies or use call_user_func
// to call actions ... etc
}
}
php -S localhost:8080 -t ./public index.php
igmaPHP\Router\Router;
// initialize the router
$router = new Router($routes);
// set custom static-assets path
$router->setStaticAssetsRoutePath('/site-public');
// set custom static-assets handler
$router->setStaticAssetsRouteHandler(MySuperCoolAssetsHandler::class);
// fire the router
$router->run();
use SigmaPHP\Router\Interfaces\StaticAssetsHandlerInterface;
class MySuperCoolAssetsHandler implements StaticAssetsHandlerInterface
{
/**
* Handle an action triggered by the Router.
*
* @param string $resourcePath
* @return void
*/
public function handle($resourcePath)
{
readfile($resourcePath);exit;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.