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

A "1" ends up in the rendered markup when using this router

Open leggettc18 opened this issue 3 years ago • 4 comments

I have a simple PHP site I was modernizing (mostly just to see if I could). I found this routing library that seemed good and I wanted to replace the one I wrote because this one seemed better. However, when I added it in, everything routed correctly, but I got the number "1" appearing in the markup of every page, just before the end of the body tag. When I switched back to my custom router it went away.

Here is the source code to the project in question. src/routes.php contains the routes, public/index.php is the main index file. Also I was using php -S 0.0.0.0:80 -t public/ to serve the app in development if that's important (didn't want to have to set up a whole Apache/Nginx server in docker in order to develop the app. Reconsidering that lately but this should probably work correctly with PHP's built-in server regardless).

leggettc18 avatar Apr 22 '22 05:04 leggettc18

I have a simple PHP site I was modernizing (mostly just to see if I could). I found this routing library that seemed good and I wanted to replace the one I wrote because this one seemed better. However, when I added it in, everything routed correctly, but I got the number "1" appearing in the markup of every page, just before the end of the body tag. When I switched back to my custom router it went away.

Here is the source code to the project in question. src/routes.php contains the routes, public/index.php is the main index file. Also I was using php -S 0.0.0.0:80 -t public/ to serve the app in development if that's important (didn't want to have to set up a whole Apache/Nginx server in docker in order to develop the app. Reconsidering that lately but this should probably work correctly with PHP's built-in server regardless).

I've been getting this as well, any chance you were able to figure out the issue?

compilekaiten avatar Jan 16 '23 03:01 compilekaiten

@DeveloperMarius Do you have any insight in this?

I'm using require_once to load my views/html.

compilekaiten avatar Jan 16 '23 03:01 compilekaiten

I managed to fixit by adding an exit after requesting a file, the problem is that the return is printed.

return require 'login.html';

to

require 'login.html';
exit;

cdanielzt avatar Feb 21 '23 18:02 cdanielzt

That's because the default behavior of the router is to render multiple routes until an output is returned.

In your case, you can disable this feature, then you don't need to use the exit.

Set the setRenderMultipleRoutes to false before you start the routing, like so:

Router::router()->setRenderMultipleRoutes(false)->start();

I think that should work. If not then you can always just return true.

skipperbent avatar Feb 21 '23 18:02 skipperbent