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




declare(strict_types=1);

use DLRoute\Requests\DLRoute;

:execute();

DLRoute::get('/api/{id}', function (object $params) {
    return [
        'id' => $params->id
    ];
});

$params->id

DLRoute::get('/api/{id}', function (object $params) {
    return [
        'id' => $params->id
    ];
})->filter_by_type([
    'id' => 'integer'
]);

$params['campo']->value;        // "valor"
$params['campo']->offset;       // posición del nombre
$params['campo']->offset_value; // posición del valor
$params['campo']->length;       // longitud del valor

$params['activo']->value;       // null

DLRoute::get('/{ciencia?=algo}/users', fn() => []);

DLRoute::get(
    '/products/{uuid?}/detail',
    [ProductController::class, 'show']
);

use DLRoute\Enums\Methods;

DLRoute::match(
    [Methods::GET, Methods::POST],
    new RouteHandler(
        uri: '/api/{uuid}',
        controller: [ApiController::class, 'handle'],
        mime_type: 'application/json',
        handler_filters: [
            'uuid' => 'uuid'
        ]
    )
);

DLRoute::get(
    '/api/{id}',
    function (object $params) {
        return [
            'id' => $params->id
        ];
    }
)->filter_by_type([
    'id' => 'integer'
]);

->filter_by_type([
    'token' => '/^[a-f0-9]{64}$/'
]);

$auth->Route::get('/profile', fn() => [
        'status' => true
    ]);
});

DLRoute::get('/telemetry', function () {
    return TelemetryRequest::telemetry('Mi API');
});

DLRoute::get('/', fn() => [
    'status' => 'ok'
]);

DLRoute::get(
    '/data',
    fn() => $data,
    mime_type: 'application/json'
);

DLRoute::get('/check', fn() => [
    'status' => true
]);

DLRoute::get(
    '/logout',
    [AuthController::class, 'logout']
);

DLRoute::get(
    '/users/{id}',
    function (object $params) {
        return [
            'id' => $params->id
        ];
    }
);

DLRoute::get(
    '/users/{id}',
    function (object $params) {
        return [
            'id' => $params->id
        ];
    }
)->filter_by_type([
    'id' => 'integer'
]);

DLRoute::get(
    '/products/{uuid?}/detail',
    [ProductController::class, 'show']
)->filter_by_type([
    'uuid' => 'uuid'
]);

use DLRoute\Core\Data\RouteHandler;
use DLRoute\Enums\Methods;

DLRoute::match(
    [Methods::GET, Methods::POST],
    new RouteHandler(
        uri: '/api/{uuid}',
        controller: [ApiController::class, 'handle'],
        mime_type: 'application/json',
        handler_filters: [
            'uuid' => 'uuid'
        ]
    )
);

DLRoute::get('/profile', fn() => [
    'scope' => 'PUBLIC'
]);

$auth->ED'
    ]);
});
text
project/
├── public/
│   └── index.php
├── vendor/
└── composer.json
text
GET /?campo=valor&activo
text
GET /users/123
        │
        ▼
   Servidor web
        │
        ├── /css/app.css ──────► archivo existente
        │
        └── /users/123 ────────► public/index.php
                                      │
                                      ▼
                                   DLRoute
apache
FallbackResource /index.php
text
/index.php
apache
FallbackResource /index.php
apache
<Directory /var/www/example/public>
    Require all granted
    FallbackResource /index.php
</Directory>
text
/etc/apache2/sites-available/
text
docs/tutorial/README.md