PHP code example of roy404 / routes

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

    

roy404 / routes example snippets


Route::group(['middleware' => 'auth'], function () {
    Route::get('/dashboard', function () {
        echo 'Welcome to the Dashboard';
    });

    Route::get('/profile', function () {
        echo 'Your Profile';
    });
});

Route::controller(HomeController::class)->group(function () {
    Route::get('/home', 'index');
});

Route::middleware([Auth::class, 'isAuthenticated'])->group(function () {
    Route::get('/profile', function () {
        echo 'Your profile';
    });
});

Route::middleware([Auth::class, 'isAuthenticated'])
    ->group(function () {
        Route::get('/profile', function () {
            echo 'Your profile';
        });
    })
    ->unauthorized(function () {
        // Handle unauthorized access here
        // Example: redirect to login page or return a 401 response
    });

Route::get('/profile', function () {
    echo 'Your profile';
})->middleware([Auth::class, 'isAuthenticated']);

Route::prefix('admin')->group(function () {
    Route::get('/dashboard', function () {
        echo 'Admin Dashboard';
    });
});

Route::name('user')->group(function () {
    Route::get('home', function () {
        echo 'Your home';
    })->name('home');

    Route::get('profile', function () {
        echo 'Your profile';
    })->name('profile');
});

Route::domain('admin.example.com')->group(function () {
    Route::get('/home', function () {
        echo 'Your home';
    });
});


App\Routes\Route;

Route::configure(
    __DIR__,               // Project root directory
    ['routes/web.php'],    // Route definition files
)->routes(function (array $routes) {
    /**
     * This callback receives all registered routes.
     * Useful for debugging, inspecting route metadata,
     * or generating links from named routes.
     */
    echo '<pre>';
    print_r($routes);
    echo '</pre>';
})->captured(function (mixed $content, int $code, string $type) {
    /**
     * This callback handles the final response output.
     * You can customize headers, status codes,
     * or response formatting here.
     */
    http_response_code($code);
    header('Content-Type: ' . $type);
    echo $content;
});

Route::configure(
    string $root,
    array  $routes,
    string $prefix = '',
    string $domain = '',
    array  $middleware = []
)

Route::configure(
    __DIR__,
    ['routes/web.php'],
    prefix: 'api',
    middleware: ['auth']
);



use App\Routes\Route;

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

Route::controller(HomeController::class)->group(function () {
    Route::get('/home', 'index');
    Route::get('/about', 'about');
});

Route::get('/dashboard', function () {
    echo 'Dashboard';
})->name('dashboard');

$link = Route::link('dashboard');


App\Routes\Route;

// Configure the router and handle the response output
Route::configure(__DIR__, ['routes/web.php'])
    ->captured(fn ($content) => print $content);



use App\Routes\Route;

// Define a basic route
Route::get('/', fn () => 'Hello World');

http://localhost:8000

Hello World
bash
 php -S localhost:8000
nginx
location / {
    try_files $uri $uri/ /index.php?$query_string;
}