0

I have a very simple MVC project (no DB) that works very well when I launch it with a local server ("php -S localhost:8000 -t public").

I use my routing this way:

require "Controllers/AppController.php";

$parsed_url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$appController = new AppController();

switch ($parsed_url) {
    case '/':
    case '/home':
        $appController->home();
        break;
    case '/about':
        $appController->about();
        break;
    case '/achievements':
        $appController->achievements();
        break;
    case '/contacts':
        $appController->contacts();
        break;
    default:
        $appController->pageNotFound();

I deployed it with Heroku, adding a Procfile, as specified in the official documentation, but now I can only get the homepage of my portfolio. All my other links throw me a "Not Found - The requested URL was not found on this server.".

Here's my Heroku log, if that can help:

2022-10-30T09:26:48.666806+00:00 heroku[router]: at=info method=GET path="/about" host=alexis-boucherie-devweb.herokuapp.com request_id=0dd695f8-8252-4cd4-8d3d-e012daf06130 fwd="86.213.16.43" dyno=web.1 connect=0ms service=1ms status=404 bytes=360 protocol=https

What I don't understand, is that if I try to var_dump my "$parsed_url", it does return a "/", for the homepage. So I think it means the program goes through my Switch Case and that my Controller is ok. Also, because I have my homepage showing in the first place.

I tried to modify the page my $appController object calls for, and by doing so, I can access another page. But then again, if I click on any link or manually type the address of one of my pages (for instance "/contacts"), it doesn't work.

In the end, I tried and searched for other topics related to this, found two here on StackOverflow, about adding an .htacess file, but I'm not sure I'm getting how that really works. Could this be the real source of my problem?

  • PHP’s [built-in server](https://www.php.net/manual/en/features.commandline.webserver.php) cheats/helps a lot: “If a URI request does not specify a file, then either index.php or index.html in the given directory are returned. If neither file exists, the lookup for index.php and index.html will be continued in the parent directory and so on until one is found or the document root has been reached. If an index.php or index.html is found, it is returned and $_SERVER['PATH_INFO'] is set to the trailing part of the URI” – Chris Haas Oct 30 '22 at 14:03
  • Instead, see [this](https://stackoverflow.com/a/18406686/231316 ) for a sample htaccess file. Make sure to note the edit for how to avoid the query string which is how older code used to do it. – Chris Haas Oct 30 '22 at 14:04
  • Thank you guys, it was great help! So the .htaccess was what I needed, as I suspected. The thing is that I tried to put one at the "root", which was -I thought- the base folder of my site. Instead I had to put it at the same place as my "index.php", in '/public'. Now it all works perfectly. – Alexis Boucherie Oct 31 '22 at 09:26

0 Answers0