ViragRouter is a lightweight PHP router package that allows you to easily define and handle routes in your PHP applications.
With ViragRouter, you're equipped with a robust suite of capabilities essential for efficient route handling: seamless route definition, dynamic parameter management, route grouping with middleware support, named routes for URL generation, and more. Its lightweight architecture guarantees minimal overhead, delivering optimal performance without sacrificing functionality.
ViragRouter is actively developed and continuously enhanced. While it's currently in its developmental phase and may contain occasional bugs, so it is not recommended for production use at this time. We appreciate your patience and understanding as i work towards delivering a stable and reliable product. However, you can safely use it for learning purposes, exploring its features, and exploring advanced routing concepts in PHP.
You can install ViragRouter via Composer:
composer require viragrajput/router
php -S localhost:8000 -t public
You can define routes using the Route
class provided by ViragRouter. Here's an example of how to define a route:
use Virag\Router\Route;
Route::get('/hello', function () {
echo "Hello, World!";
});
Route::get('/hello', [HelloController::class, 'index']);
You can define route parameters by enclosing them in curly braces {}
. These parameters will be passed to your route handler. Here's an example:
use Virag\Router\Route;
Route::get('/users/{id}', function ($id) {
echo "User ID: $id";
});
Route::get('/users/{id}', [UserController::class, 'show']);
You can group routes and apply common attributes using the group
method. Here's an example:
use Virag\Router\Route;
Route::group(['middleware' => 'auth'], function () {
Route::get('/dashboard', 'DashboardController@index');
Route::get('/settings', 'SettingsController@index');
});
Route::group(['middleware' => 'auth'], function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/settings', [SettingsController::class, 'index'])->name('settings');
});
You can name your routes using the name
method. Named routes allow you to generate URLs based on the route name. Here's an example:
use Virag\Router\Route;
Route::get('/profile', 'ProfileController@index')->name('profile');
Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
You can apply middleware to routes using the middleware
method. Middleware allows you to filter HTTP requests entering your application. Here's an example:
use Virag\Router\Route;
Route::get('/admin', 'AdminController@index')->middleware('admin');
Route::get('/admin', [AdminController::class, 'index'])->middleware('admin');
You can generate URLs for named routes using the generateUrl
method. Here's an example:
use Virag\Router\Route;
$url = Route::generateUrl('profile');
echo "Profile URL: $url";
You can handle incoming requests using the dispatch method of the Router class. Here's an example:
use Virag\HttpFoundation\Request;
use Virag\HttpFoundation\Response;
$router = new Router();
$request = Request::createFromGlobals();
$response = $router->dispatch($request);
$response->send();
Certainly! Below is an example of how you can integrate the ViragRouter package into your custom PHP Project:
First, install the ViragRouter package via Composer:
composer require viragrajput/router
In your custom Project, you'll need to define routes using the ViragRouter Route
class. Here's an example of how you can define routes in your Project:
// index.php
require_once 'vendor/autoload.php';
use ViragRouter\Route;
Route::get('/', function () {
echo "Welcome to my custom Project!";
});
Route::get('/hello/{name}', function ($name) {
echo "Hello, $name!";
});
In your Project entry point (e.g., index.php
), create a router instance and handle incoming requests:
// index.php
require_once 'vendor/autoload.php';
use ViragRouter\Router;
use Virag\HttpFoundation\Request;
use Virag\HttpFoundation\Response;
$router = new Router();
$request = Request::createFromGlobals();
$response = $router->dispatch($request);
$response->send();
In your custom Project, ensure that you have logic to handle requests and invoke the appropriate route handlers based on the incoming requests.
Depending on your custom Project's architecture and requirements, you may need to configure additional features such as middleware, route groups, named routes, etc., provided by ViragRouter.
By following these steps, you should be able to integrate the ViragRouter package into your custom PHP project and define and handle routes effectively. Adjustments may be needed based on your project's specific requirements and architecture.
This package is open-source software licensed under the MIT license.