How do I use FastRoute?

后端 未结 3 577
悲&欢浪女
悲&欢浪女 2021-02-02 15:22

I\'m attempting to use the FastRoute routing library and can\'t get the simple usage example to work.

Here is the basic usage example found on the GitHub page:



        
3条回答
  •  借酒劲吻你
    2021-02-02 15:56

    $r->addRoute(['GET','POST','PUT'], '/test', 'TestClass/testMethod');
    

    Then in your dispatch method:

    $routeInfo = $router->dispatch($httpMethod, $uri)
    switch($routeInfo[0]) {
     case FastRoute\Dispatcher::FOUND:
     $handler = $routeInfo[1];
     $vars = $routeInfor[2];
     list($class, $method) = explode('/',$handler,2);
     $controller = $container->build()->get($class);
     $controller->{$method}(...array_values($vars));
    break;
    }   
    

    Class and method of course could be invoked by call_user_func(), but as I wanted the router to be more abstract and placed outside in the root directory I decided to use DI container, which is brilliant.

提交回复
热议问题