PHP code example of viragrajput / router

1. Go to this page and download the library: Download viragrajput/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/ */

    

viragrajput / router example snippets


use Virag\Router\Route;

Route::get('/hello', function () {
    echo "Hello, World!";
});

Route::get('/hello', [HelloController::class, 'index']);

use Virag\Router\Route;

Route::get('/users/{id}', function ($id) {
    echo "User ID: $id";
});

Route::get('/users/{id}', [UserController::class, 'show']);

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');
});

use Virag\Router\Route;

Route::get('/profile', 'ProfileController@index')->name('profile');

Route::get('/profile', [ProfileController::class, 'index'])->name('profile');

use Virag\Router\Route;

Route::get('/admin', 'AdminController@index')->middleware('admin');

Route::get('/admin', [AdminController::class, 'index'])->middleware('admin');

use Virag\Router\Route;

$url = Route::generateUrl('profile');
echo "Profile URL: $url";

use Virag\HttpFoundation\Request;
use Virag\HttpFoundation\Response;

$router = new Router();

$request = Request::createFromGlobals();
$response = $router->dispatch($request);
$response->send();

// index.php

ter\Route;

Route::get('/', function () {
    echo "Welcome to my custom Project!";
});

Route::get('/hello/{name}', function ($name) {
    echo "Hello, $name!";
});

// index.php

ter\Router;
use Virag\HttpFoundation\Request;
use Virag\HttpFoundation\Response;

$router = new Router();

$request = Request::createFromGlobals();
$response = $router->dispatch($request);
$response->send();