Phprest\Router\Strategy::dispatch PHP Метод

dispatch() публичный Метод

$controller can be one of three types but based on the type you can infer what the controller actually is: - string (controller is a named function) - array (controller is a class method [0 => ClassName, 1 => MethodName]) - \Closure (controller is an anonymous function)
public dispatch ( string | array | Closure $controller, array $vars ) : mixed
$controller string | array | Closure
$vars array - named wildcard segments of the matched route
Результат mixed
    public function dispatch($controller, array $vars)
    {
        $request = $this->container->get('Symfony\\Component\\HttpFoundation\\Request');
        $response = $this->invokeController($controller, array_merge([$request], array_values($vars)));
        if ($response instanceof Response && $response->getContent() !== '') {
            return $this->serialize($response->getContent(), $request, $response);
        }
        return $response;
    }

Usage Example

Пример #1
0
 public function testDispatchWithClassAndMethodAndResponseObject()
 {
     $this->setRequestParameters('phprest-test', 1, '*/*');
     $result = $this->strategy->dispatch('Phprest\\Stub\\Controller\\Simple::getSampleResponse', []);
     $this->assertInstanceOf('Phprest\\HttpFoundation\\Response', $result);
     if ($result instanceof \Phprest\HttpFoundation\Response) {
         $this->assertEquals(json_encode('sample'), $result->getContent());
     }
 }