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

Passing params to filters #70

Open
crichardson9 opened this issue Dec 11, 2017 · 2 comments
Open

Passing params to filters #70

crichardson9 opened this issue Dec 11, 2017 · 2 comments

Comments

@crichardson9
Copy link

crichardson9 commented Dec 11, 2017

Is there a way to pass information to filters?

For example:

$router->get('/client/add', [$controller, 'getAdd'], ['before' => ['permissionCheck', 'client_create']]);

and:

$router->filter('permissionCheck', function($permission) {
    echo $permission; // client_create
});
@nmsobri
Copy link

nmsobri commented Feb 25, 2019

Did you got the solution?

@dottwatson
Copy link

Here is my solution. basically i've extended the Dispatcher for store current resolved route and vars.

`use Phroute\Phroute\Dispatcher;

class RouteDispatcher extends Dispatcher{

protected $routeInfo =  [];

/**
 * Dispatch a route for the given HTTP Method / URI.
 *
 * @param $httpMethod
 * @param $uri
 * @return mixed|null
 */
public function dispatch($httpMethod, $uri)
{
    $dispatchRoute = new \ReflectionMethod($this,'dispatchRoute');
    $dispatchRoute->setAccessible(true);

    $parseFilters = new \ReflectionMethod($this,'parseFilters');
    $parseFilters->setAccessible(true);

    $dispatchFilters = new \ReflectionMethod($this,'dispatchFilters');
    $dispatchFilters->setAccessible(true);

   
    list($handler, $filters, $vars) = $dispatchRoute->invokeArgs($this,[$httpMethod, trim($uri, '/')]);

    $this->routeInfo = [
        'uri' => trim($uri,'/'),
        'vars' => $vars
    ];

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

    if(($response = $dispatchFilters->invokeArgs($this,[$beforeFilter])) !== null)
    {
        return $response;
    }
    
    $resolvedHandler = $this->handlerResolver->resolve($handler);
    
    $response = call_user_func_array($resolvedHandler, $vars);

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

public function getRouteInfo(){
    return $this->routeInfo;
}

`

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

3 participants