Skip to content

mobinjavari/router-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple PHP Router

The Best Router With PHP (use)


routers.php

// [*] Creating a new instance of Router
$router = new Router();

// [!] Set the base address (example.com/{project_folder})
$router->setBasePath('/router');

// [*] Adding a new route with the get method
$router->get('/test', function () {
        echo 'test';
});

// [!] Examples of other tasks that can be done with this class
$router->mountPath('/api', function() use ($router) {
     $router->any('/test/[:id]', function ($id) {
        echo 'test'.$id;
    });
    $router->any('/hello/[:nicname]', '/hello.php');
}, ['ANY']);

// [!] set 404 error
$router->setError(function () {
    echo '404 | Page Not Found'
});

// [*] And finally, at the end of the file, we run the following method to run and check all the routes
$router->matchRoute();

hello.php

echo 'Hello ' . $nicname;