A simple, lightweight and efficient PHP routing library.
- Make sure you have a index.php file in your root directory.
Put this
.htaccess
file in your root directory.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
- Composer command
composer require joydeep-bhowmik/php-router
in your index.php file
require __DIR__ . '/vendor/autoload.php';
use JoydeepBhowmik\PHPRouter\Router;
$router = new Router();
$router->get('/', function () {
return 'Home';
});
$router->dispatch();
echo Router::$view;
$router->get('/', function () {
return 'get request';
});
$router->post('/', function () {
return 'post request';
});
$router->put('/', function () {
return 'put request';
});
$router->delete('/', function () {
return 'delete request';
});
$router->any('/', function () {
return 'anytype of request';
});
$router->dispatch();
$router->get('*', function () {
return '404 not found';
});
$router->get('/profile/{user}/{id}', function ($user, $id) {
return 'Username ' . $user . ' and user id is ' . $id;
});
$router->baseUrl('/shop');
$router->get('/profile/{user}/{id}', [ExampleController::class, 'index']);
$router->addRoute('profile/{user}/{id}', function () {
return 'Hello';
}, 'METHOD_NAME');
class Example_middleware
{
public function handle()
{
return true;
}
}
$router->middleware(Example_middleware::class, function () use ($router) {
$router->get('/', function () {
return 'Home';
});
});
$router->dispatch()