mrjgreen / phroute

A super fast PHP router, with route parameters, restful controllers, filters and reverse routing.
Other
714 stars 96 forks source link

Error Class not found #104

Open gabrybarr opened 2 years ago

gabrybarr commented 2 years ago

$collector = new Phroute\Phroute\RouteCollector();

$collector->get('/', function(){ echo 'Home Page'; });

$collector->post('products', function(){ return 'Create Product'; });

$collector->put('items/{id}', function($id){ return 'Amend Item ' . $id; });

$dispatcher = new Phroute\Phroute\Dispatcher($collector->getData());

$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

// Print out the value returned from the dispatched function echo $response;

i have this code But it restores me this error. How can i solve it?

Fatal error: Uncaught Error: Class 'Phroute\Phroute\HttpRouteNotFoundException' not found in /var/www/html/gab/kiosk-mvc/Phroute/Dispatcher.php:222 Stack trace: #0 /var/www/html/gab/kiosk-mvc/Phroute/Dispatcher.php(127): Phroute\Phroute\Dispatcher->dispatchVariableRoute() #1 /var/www/html/gab/kiosk-mvc/Phroute/Dispatcher.php(51): Phroute\Phroute\Dispatcher->dispatchRoute() #2 /var/www/html/gab/kiosk-mvc/index.php(59): Phroute\Phroute\Dispatcher->dispatch() #3 {main} thrown in /var/www/html/gab/kiosk-mvc/Phroute/Dispatcher.php on line 222

Sleon4 commented 1 year ago

Can you try using exceptions to check these problems?

try {
    $response = (new Dispatcher($collector->getData()))->dispatch(
        $_SERVER['REQUEST_METHOD'],
        implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), 0))
    );

echo($response);
} catch (HttpRouteNotFoundException $e) {
    echo($e->getMessage());
} catch (HttpMethodNotAllowedException $e) {
    echo($e->getMessage());
}