miladrahimi / phprouter

PhpRouter is a full-featured yet very fast HTTP URL router for PHP projects
MIT License
195 stars 17 forks source link

How to pass the 404 pages as view #46

Closed mcdavidsh closed 2 years ago

mcdavidsh commented 2 years ago

Hello, i am wondering how pass the 404/500 error view after a catch response. ` $this->router->get('/', [HomeController::class,'show']); $this->router->get('/404', function (View $view){ return $view->make('error.404',); });$this->router->get('/500', function (View $view){ return $view->make('error.500'); }); try { $this->router->dispatch(); } catch (RouteNotFoundException $e) { // It's 404!

        $this->router->getPublisher()->publish(new HtmlResponse('Not Found', 404));
    } catch (Throwable $e) {
        // Log and report...
        $this->router->getPublisher()->publish(new HtmlResponse('Internal error.', 500));
    }`

Use $this->router->get('/404', function (View $view){ return $view->make('error.404',); })

    Instead of  $this->router->getPublisher()->publish(new HtmlResponse('Not Found', 404));
miladrahimi commented 2 years ago

Here you can see: https://github.com/miladrahimi/phprouter#error-handling