PHP code example of aloefflerj / yet-another-controller

1. Go to this page and download the library: Download aloefflerj/yet-another-controller 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/ */

    

aloefflerj / yet-another-controller example snippets


    
    
    declare(strict_types=1);
    
    use Aloefflerj\YetAnotherController\Controller\Controller;
    use Psr\Http\Message\RequestInterface;
    use Psr\Http\Message\ResponseInterface;
    
    

    $controller->get('/', function (RequestInterface $request, ResponseInterface $response) {
        $response->getBody()->write('Welcome home');
        return $response->getBody();
    });
    

    $controller->dispatch();
   

$controller->get('/', function (RequestInterface $request, ResponseInterface $response) {
    $response->getBody()->write('Welcome home');
    return $response->getBody();
});

$controller->get('/mascots/{mascot}', function (RequestInterface $request, ResponseInterface $response, \stdClass $args) {
    $response->getBody()->write("PHP {$args->mascot} mascot is awesome");
    return $response->getBody();
});

$controller->put('/mascot', function (RequestInterface $request, ResponseInterface $response) {
    $requestBody = $request->getBody();
    return $requestBody;
});