Passed
Push — main ( c3557b...0a89cd )
by Chema
56s queued 15s
created

Controller::customAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
require_once \dirname(__DIR__) . '/vendor/autoload.php';
6
7
use Gacela\Router\Entities\Request;
8
use Gacela\Router\Router;
9
use Gacela\Router\Routes;
10
11
# To run this example locally, you can run in your terminal:
12
# $ composer serve
13
14
class Controller
15
{
16
    public function __construct(
17
        private Request $request,
18
    ) {
19
    }
20
21
    public function __invoke(): string
22
    {
23
        $number = $this->request->get('number');
24
25
        if (!empty($number)) {
26
            return "__invoke with GET 'number'={$number}";
27
        }
28
29
        return '__invoke';
30
    }
31
32
    public function customAction(int $number = 0): string
33
    {
34
        return "customAction(number: {$number})";
35
    }
36
}
37
38
Router::configure(static function (Routes $routes): void {
39
    # Try it out: http://localhost:8081/docs
40
    $routes->redirect('docs', 'https://gacela-project.com/');
41
42
    # Try it out: http://localhost:8081?number=456
43
    $routes->get('/', Controller::class);
44
45
    # Try it out: http://localhost:8081/custom/123
46
    $routes->get('custom/{number}', Controller::class, 'customAction');
47
48
    # Try it out: http://localhost:8081/custom
49
    $routes->any('custom', Controller::class);
50
});
51