Trying to show InvalidPageException

I am trying to follow PatrickLouys/no-framework-tutorial/10-dynamic-pages and got through most of the code but have an issue with the portion that is supposed to catch the InvalidPageException. My code seems to be catching the pages that exist fine because when I go to localhost:8000/public it shows me the data properly:

but when I got to a page that doesn’t exist it doesn’t seem to show me the error properly. Here is the src/Page/InvalidPageException.php file specifically

<?php declare(strict_types = 1);

namespace Example\Page;

use Exception;

class InvalidPageException extends Exception
{
    public function __construct($slug, $code = 0, Exception $previous = null)
    {
        $message = "No page with the slug `$slug` was found, the error is working...";
        parent::__construct($message, $code, $previous);
    }
}

What is displayed going to a page that doesn’t exist:

If you’d like to view the rest of my source code in the project you can do so at github/lowheartrate/heartfx.

The tutorial says to run the server with php -S localhost:8080, which means it will look in your filesystem to see if a file exists, serve it when it does, and show a Not Found (like your screenshot!) when it doesn’t.

What you want instead is that every request should go through your index.php, regardless of whether the URL exists as a file or not.

In order to do so, start the php server with php -S localhost:8080 public/index.php

Then your pages/page-six.md is routed through index.php, instead of being looked up on disk, and you should see your error.

1 Like

@rpkamp UPDATE: I got it so it it throws the InvalidPageException by using the following in my cmd prompt.

I also had to comment out the return 'I am a placeholder'; as you can see here in order to get the InvalidPageException to be called. Unfortunately though when I go to a page that does exist like localhost:8080/pages/page-one.md it gives me 404 - Page not found because the InvalidPageException only works for some reason when I am launching the server from C:\wamp64\www\github\HeartFX\public\index.php

Also when trying to move on to the last step of PatrickLouys/no-framework-tutorial/10-dynamic-pages.md which is catching the InvalidPageException and giving the user a 404 error instead it gives me the following error


I have updated my code again on github/lowheartrate/heartfx if you’d like to see my updated code. This errors occurs with any page I go to view that does exist.

Yes, that is the drawback of the internal PHP server - you can either serve everything from disk, or run everything through a front-controller, but out of the box it won’t match.

However you could put this at the top of your index.php:

if (file_exists(ltrim($_SERVER['REQUEST_URI'], '/'))) {
    return false; //  indicate to the PHP server that we won't handle the request
}

So that if a file exists on disk with the requested name, then it will be served by the PHP server, otherwise index.php will continue to run.

1 Like

OK, I have tried to run it on my WAMPserver but the InvalidPageException isn’t even functioning there. I really am at a dead end with this tutorial running into these issues and limitations on the localhost server and the project not functioning properly in the WAMPserver.

Just don’t understand how this is supposed to run because following the tutorial it says to use php -S localhost:8000 and in order to get my InvalidPageException to function I have to use php -S localhost:8000 public/index.php which later on gives me more elements of the site becoming unfunctional / more of a headache.

I was recommended to this framework as my attempt at MVC didn’t go as planned and was told MVC really isn’t a good option for PHP. Maybe you know of another framework to use or any recommendation as to how to properly start a PHP project?

I personally really like the Symfony framework, but the toturial you got there seems quite useful to grasp the basics of PHP, so I’m not sure you should be giving it up quite to soon.

Have you tried the code in my previous post?

Also, what have you done to set up WAMP?

I threw the code you sent in your previous post

into the top of my public/index.php file but doesn’t seem to change anything seeming it still says that file doesn’t exist and gives me 404 - Page not found.

It just seems more complex then it should because I am making more changes to the code that PatrickLouys/no-framework-tutorial doesn’t call for and is interefering with the code later in the chapter(s).

As far as my WAMPserver goes, I haven’t done many changes to the configuration of it if at all that I recall. The only thing you could say I have done is put my github folder into the www directory so I can seamlessly see changes to the site and push them to my GitHub repository (lowheartrate/HeartFX).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.