Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kdarel's avatar
Level 3

PHP for Beginners - Ep. 15 PHP router issues

Hello everyone, I am currently working through the PHP for beginners series and I am encountering the following error:

Warning: require(views/index.view.php): Failed to open stream: No such file or directory in /Users/kdarel/websites/demo/controllers/index.php on line 5

Fatal error: Uncaught Error: Failed opening required 'views/index.view.php' (include_path='.:/opt/homebrew/Cellar/php/8.3.12/share/php/pear') in /Users/kdarel/websites/demo/controllers/index.php:5 Stack trace: #0 {main} thrown in /Users/kdarel/websites/demo/controllers/index.php on line 5

To me, it seems like this is telling me that it can't find the 'views/index.view.php' file when it is required in the controllers/index.php file, which would make sense because we are further into the directory tree at this point in the controllers directory and we need to move up one directory to the views directory. But, even if I change the require statement to be require '../views/index.view.php' I still receive the error.

If I manually specify the route in the address bar such as "/" or "/about" I am able to access each page but if I attempt to access them from the Home page using nav buttons, I get the above error.

I have not yet completed the episode because I can't seem to get past the error, but I have compared my source code to that which Jeffrey posted on Github and all files appear to be identical for what I have completed up to this point. Does anyone have any ideas?

Thanks in advance, Kevin

0 likes
15 replies
kdarel's avatar
Level 3

@jlrdw - thanks for the info however I am not running wampserver. I am running php -S localhost:8888.

kdarel's avatar
Level 3

@iamputhea yes I went back and triple checked that I am in fact in the demo directory

jlrdw's avatar

@kdarel on the /about route, what is the complete url that is working?

1 like
kdarel's avatar
Level 3

@jlrdw if I click in the address bar and type in localhost:8888/about I can get to the about page. If I click on the About nav button, I immediately get the error I posted and if I click on the address bar the url it is showing localhost:8888/controllers/about. I have attempted to dd($uri) in the demo/index.php file to get the route it is trying, but I never get to the dd function I just get the error

jlrdw's avatar

@kdarel show the code for the About nav button.

Pretty sure controllers doesn't belong, I looked at the github code, but can't tell why its happening.

1 like
kdarel's avatar
Level 3

@jlrdw here is the wesites/demo/index.php file:

<?php
require 'functions.php';
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
if ($uri === '/') {
    require 'controllers/index.php';
} else if ($uri === '/about') {
    require 'controllers/about.php';
} else if ($uri === '/contact') {
    require 'controllers/contact.php';
} else if ($uri === '/mission') {
    require 'controllers/mission.php';
}

here is the websites/demo/controllers/about.php file

<?php
$heading = "About Us";
require ("views/about.view.php");
jlrdw's avatar

@kdarel your router is setup like:

1 like
kdarel's avatar
Level 3

So here is the weird thing about this. I have been using my Mac so far for all of this. I decided to see if I could recreate the issue on my almalinux laptop and low and behold I get the same error in the same spot. So now I am really stumped :/

jlrdw's avatar

@kdarel try downloading the one from github and see if it works out of the box.

Edit:

I placed the code on my windows 11 computer and everything works.

1 like
Shivamyadav's avatar
Level 20

@kdarel could you show me your navigation code of your nav links files.

1 like
kdarel's avatar
Level 3

@Shivamyadav - hey man I wanted to thank you for pointing me to look at the nav.php file. It turns out, that the the href for the buttons was coded to 'controllers/about' etc and by correcting the path to '/about', '/contact' etc everything is now working as it should on both computers. Thanks again :)

1 like
Shivamyadav's avatar

@kdarel yeah! Man I doubted on your nav links when I read whole forum where you have mentioned that when you look on your url after clicking on navigation links the links point to the different path.

1 like
kdarel's avatar
Level 3

Thanks to everyone that was helping me with this. The issue is now resolved and I can move forward. Thanks again everyone!!

1 like

Please or to participate in this conversation.