PHP code example of dlunire / dlroute

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

    

dlunire / dlroute example snippets


DLRoute::execute();

DLRoute::get(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::post(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::put(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::patch(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::delete(string $uri, callable|array|string $controller): DLParamValueType;

use DLRoute\Requests\DLRoute as Route;
use DLRoute\Test\TestController;

Route::get('/ruta', [TestController::class, 'method']);
Route::get('/ruta/{parametro}', [TestController::class, 'method']);

final class TestController extends Controller {
    public function tu_metodo(object $params): object|string {
        return $params; // o HTML si deseas
    }
}

Route::get('/ruta/{id}', [TestController::class, 'method'])
  ->filter_by_type(['id' => 'numeric']);

->filter_by_type(['token' => '/[a-f0-9]+/']);

Route::get('/ruta/{parametro}', function (object $params) {
  return $params;
});

DLRoute::execute();

DLRoute::get(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::post(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::put(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::patch(string $uri, callable|array|string $controller): DLParamValueType;
DLRoute::delete(string $uri, callable|array|string $controller): DLParamValueType;

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

final class UserController extends Controller {
    public function index(object $params): string {
        return view('users.index', ['users' => []]);
    }
}

Route::get('/users/{id}', [UserController::class, 'show'])
  ->filter_by_type(['id' => 'integer']);

Route::get('/info', function (object $params) {
  return ['status' => 'ok'];
});