1. Go to this page and download the library: Download abdelrhman-saeed/route 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/ */
abdelrhman-saeed / route example snippets
use AbdelrhmanSaeed\Route\Api\Route;
use Symfony\Component\HttpFoundation\{Request, Response};
// Initialize the routing system
Route::setup('routes.php', Request::createFromGlobals(), new Response);
use AbdelrhmanSaeed\Route\Api\Route;
use App\Controllers\SomeKindOfController;
// Define routes with dynamic segments
Route::get('users/{user}/posts/{post}', function (int $user, int $post) {
var_dump($user, $post);
});
// Use a controller to handle requests
Route::get('posts/{post}/comments/{comment}', [SomeKindOfController::class, 'someMethod']);
// Define routes with optional segments
Route::get('search/users/{user}/{filter?}', function (int $user, ?string $filter = null) {
// Handle search logic
});
// Supported HTTP methods: ['get', 'post', 'put', 'patch', 'delete']
Route::post('test', function () {
// Handle POST request
});
Route::put('test', function () {
// Handle PUT request
});
// Define multiple methods for a single route
Route::match(['put', 'patch', 'delete'], 'test', function () {
// Handle multiple request types
});
// Define a route for all HTTP methods
Route::any('test', function () {
// Handle any HTTP method
});
use AbdelrhmanSaeed\Route\Endpoints\Rest\Constraints\ConstraintsInterface;
use AbdelrhmanSaeed\Route\Api\Route;
// Define a constraint using regex
Route::get('users/{user}', fn (int $user) => var_dump($user))
->where('user', '[A-z]+');
// Use predefined constraints from ConstraintsInterface
Route::get('users/{user}/posts/{post}', function (int $user, string $post) {
// Handle request
})
->where('user', ConstraintsInterface::NUM)
->where('post', ConstraintsInterface::ALPHANUM);
// Specify allowed values for a segment
Route::get('oauthcallback/{server}', function (string $server) {
// Handle OAuth callback
})
->whereIn('server', ['facebook', 'google']);
use AbdelrhmanSaeed\Route\Middleware;
use Symfony\Component\HttpFoundation\Request;
class RedirectIfAuthenticated extends Middleware
{
public function handle(Request $request): void
{
// Perform checks before proceeding
parent::handle($request);
}
}
Route::get('login', function () {
// Login logic
})->setMiddlewares(RedirectIfAuthenticated::class);