PHP code example of k9u / request-mapper

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

    

k9u / request-mapper example snippets


use K9u\RequestMapper\Annotation\GetMapping;
use My\App\Presentation\Blog;

class BlogController
{
    /**
     * @GetMapping("/blogs/{id}")
     */
    public function show($id)
    {
        // snip(find blog by $id)
        $blog = [
            'id' => $id,
            'title' => 'Hello world!',
            ...
        ];

        return $blog;
    }
}

use K9u\RequestMapper;

$request = $serverRequestFactory->createServerRequest('GET', 'http://example.com/blogs/1', $_SERVER);

$handlerResolver = new HandlerResolver('/path/to/src/Presentation');
$handler = $handlerResolver($request);

// $handler->class      = 'My\App\Presentation\Blog\BlogController'
// $handler->method     = 'show'
// $handler->pathParams = ['id' => '1']

$handlerClassFactory = ...;
/* @var HandlerClassFactoryInterface $handlerClassFactory */

$handlerMethodArgumentsResolver = ...;
/* @var HandlerMethodArgumentsResolverInterface $handlerMethodArgumentsResolver */

$handlerInvoker = new HandlerInvoker($handlerClassFactory, $handlerMethodArgumentsResolver);
$result = $handlerInvoker($handler, $request);

var_export($result);
// array (
//   'id' => 1,
//   'title' => 'Hello world!',
//   ...
// )