mrjgreen/phroute

Custom resolver route parameters

iparrabb opened this issue · 0 comments

Hi,

I have an issue about route parameters and Dependency Injection usign PHP-DI.

<?php
namespace Application\Resolvers;

use DI\Container;
use Phroute\Phroute\HandlerResolverInterface;

class RouterResolver implements HandlerResolverInterface
{
    private $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function resolve($handler, /* ¿¿$vars?? */)
    {
        /*
         * Only attempt resolve uninstantiated objects which will be in the form:
         *
         *      $handler = ['App\Controllers\Home', 'method'];
         */
        if(is_array($handler) and is_string($handler[0])) {
            $handler[0] = $this->container->call($handler, /* ¿¿$vars?? */);
        }

        return $handler;
    }
}

This work's fine for routes like this

$router->get('/', ['Application\Controllers\LoginController', 'loginForm']);
$router->post('/', ['Application\Controllers\LoginController', 'login']);

But doesn't work's for routes with parameters like this

$router->get('/hello/{name}', ['Application\Controllers\HomeController', 'hello']);

It is because the call method of PHP-DI Container receives 2 arguments, handler and parameters, I can see that HandlerResolverInterface contains the resolve method with the handler, but, what about the $vars?, I need $vars in resolver.

public function dispatch($httpMethod, $uri)
    {
        list($handler, $filters, $vars) = $this->dispatchRoute($httpMethod, trim($uri, '/'));

        list($beforeFilter, $afterFilter) = $this->parseFilters($filters);

        if(($response = $this->dispatchFilters($beforeFilter)) !== null)
        {
            return $response;
        }
        
        $resolvedHandler = $this->handlerResolver->resolve($handler, $vars); <--- here!

        $response = call_user_func_array($resolvedHandler, $vars);

        return $this->dispatchFilters($afterFilter, $response);
    }

Any help are welcome.