simple-php-router icon indicating copy to clipboard operation
simple-php-router copied to clipboard

Double render setRewriteCallback

Open smbongtoi85 opened this issue 3 years ago • 3 comments

SimpleRouter::error(function(Request $request, \Exception $exception) { $request->setRewriteCallback('ErrorController@notFound'); });

smbongtoi85 avatar Mar 25 '22 22:03 smbongtoi85

Hello,

can you please describe your issue/ question more detailed? Also please provide more of your code and an error message, if one exists. Thank you!

~ Marius

DeveloperMarius avatar Mar 29 '22 20:03 DeveloperMarius

Hello, i have same issue.

This is how you can recreate it

I'm trying to add custom Exception handler and setRewriteCallback instead of redirect.

ExceptionHandler

class CustomExceptionHandler implements IExceptionHandler
{
    public function handleError(Request $request, \Throwable $error): void
    {

        /* The router will throw the NotFoundHttpException on 404 */
        if ($error instanceof NotFoundHttpException) {
            // Render custom 404-page
            $request->setRewriteCallback('Controller\PageController@notFound');
            return;
        }

        throw $error;

    }
}

Controller:

class PageController 
{

    public function notFound()
    {
        SimpleRouter::response()->httpCode(404);
        echo "check\n";
    }
}

And there is no routes only one group with set Custom Exception Handler class

SimpleRouter::group(["exceptionHandler" => \Reaspekt\Exception\CustomExceptionHandler::class], function () {
});

Desired outcome: One "check" string printed on page

Outcome: I have two "check" strings printed

DInarGab avatar Mar 21 '23 14:03 DInarGab

Thanks for the detailed report.

What happens if you return an output in the notFound method?

skipperbent avatar Mar 26 '23 22:03 skipperbent