php routes code example

Example 1: php routing

<?php

$request = $_SERVER['REQUEST_URI'];

switch ($request) {
    case '/' :
        require __DIR__ . '/views/index.php';
        break;
    case '' :
        require __DIR__ . '/views/index.php';
        break;
    case '/about' :
        require __DIR__ . '/views/about.php';
        break;
    default:
        http_response_code(404);
        require __DIR__ . '/views/404.php';
        break;
}

Example 2: laravel group routes

Route::group(['prefix' => 'admin'], function () {
    Route::get('users', function ()    {
        // Matches The "/admin/users" URL
    });
});

Example 3: laravel route namespace and prefix

Route::prefix('admin')->group(function () {
    Route::get('/users', function () {
        // Matches The "/admin/users" URL
    });
});

Example 4: laravel routes return view in web.php

Route::get("/page", function(){
   return View::make("dir.page");
});

Example 5: laravel route

# Using Request
Route::get('user/{name}', function (Request $request) {
    return $request->route('name');
});

Example 6: laravel route pattern

Route::pattern('id', '[0-9]+');
Route::get('user/{id}', function ($id) {
    // Only executed if {id} is numeric...
});