Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom resolver route parameters #86

Open
iparrabb opened this issue Jun 30, 2018 · 0 comments
Open

Custom resolver route parameters #86

iparrabb opened this issue Jun 30, 2018 · 0 comments

Comments

@iparrabb
Copy link

iparrabb commented Jun 30, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant