1. Go to this page and download the library: Download mrf0o/php-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/ */
Router::get('/user/profile', function () {
// ...
})->name('profile');
Router::group(['prefix' => '/user'], function () {
// here all routes will be prefixed with /user
Router::get('/update', fn () => die('not implemented')); /* /user/update */
});
namespace Middlewares;
use Mrfoo\PHPRouter\Core\Middleware;
class AuthMiddleware extends Middleware
{
public function handle()
{
if (!isset($_SESSION['user_id'])) {
// redirect to login page
header('Location: /login');
exit;
}
}
}
Router::get('/user/profile', function () {
// ...
})->name('user.profile')->middleware(AuthMiddleware::class);
Router::get('/user/profile', function () {
// ...
})->name('user.profile')->middleware([AuthMiddleware::class, AnotherMiddleware::class]);
namespace Middlewares;
use Mrfoo\PHPRouter\Core\Middleware;
class AuthMiddleware extends Middleware
{
public function handle()
{
if (!isset($_SESSION['user_id'])) {
// redirect to login page
header('Location: /login');
exit;
}
}
public function terminate()
{
// do some cleanup
}
}
rfoo\PHPRouter\Router;
use Controllers\UserController;
Router::get('/greet', function () {
echo 'hello everyone';
})->name('greeting');
Router::post('/user/create', function () {
// ...
$id = $_POST['user_id']; // make sure to sanitize your inputs!
})->name('user.create');
Router::patch('/user/profile', [UserController::class, 'update'])->name('user.profile.update');
// make it bun dem!
Router::run();
bash
composer
shell
php -S localhost:8888 index.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.