"Undefined variable: page ..." (PHP & mySQL: Novice to Ninja, 6th edition)

I’ve been working through the printed book using the recommended windows setup of Vagrant, VirtualBox, Git and am now stuck on pg. 375.

Both the code I’ve typed by hand and the downloaded versions of CMS-Controller-Rewrite and CMS-EntryPoint-Class result in multiple errors for “Undefined variable: page… yadda yadda” when loading index.php, “joke/edit” when editing, and if there is no existing $route set. The error does not occur when loading “joke/list”, “joke/edit” when adding a joke and deleting a joke.

I’m not sure how to fix this. It seems like the $page variable isn’t passed when using the “header('location: ’ . strtolower($route));” redirect?

Any help would be appreciated!

“Undefined variable: page… yadda yadda”.
The yadda yadda is important. It tells you exactly what line caused the failure. Go look at the line that it says.

EDIT: Tagging relevant author: @TomB

Sorry, I left out the yadda yadda because it used to be reproducible (on my machine, presumably for others if using the same code) but for whatever reason, running the same code/project this morning yields no errors?

However, for reference’s sake, this one of many instances from my error log when I tried to run this code (https://github.com/spbooks/phpmysql6/tree/CMS-EntryPoint-Class) over the last 2 days:

2019/03/08 02:36:39 [error] 761#761: *113 FastCGI sent in stderr: “PHP message: PHP Notice: Undefined variable: page in /home/vagrant/Code/Project/classes/EntryPoint.php on line 59
PHP message: PHP Stack trace:
PHP message: PHP 1. {main}() /home/vagrant/Code/Project/public/index.php:0
PHP message: PHP 2. EntryPoint->run() /home/vagrant/Code/Project/public/index.php:8
PHP message: PHP 3. EntryPoint->callAction() /home/vagrant/Code/Project/classes/EntryPoint.php:64
PHP message: PHP Warning: include(/home/vagrant/Code/Project/templates): failed to open stream: Success in /home/vagrant/Code/Project/classes/EntryPoint.php on line 21
PHP message: PHP Stack trace:
PHP message: PHP 1. {main}() /home/vagrant/Code/Project/public/index.php:0
PHP message: PHP 2. EntryPoint->run() /home/vagrant/Code/Project/public/index.php:8
PHP message: PHP 3. EntryPoint->loadTemplate() /home/vagrant/Code/Project/classes/EntryPoint.php:72
PHP message: PHP Warning: include(): Failed opening ‘/home/vagrant/Code/Project/classes/…/templates/’ for inclusion (include_path=‘.:/usr/share/php’) in /home/vagrant/Code/Project/classes/EntryPoint.php on line 21
PHP message: PHP Stack trace:
PHP message: PHP 1. {main}() /home/vagrant/Code/Project/public/index.php:0
PHP message: PHP 2. EntryPoint->run() /home/vagrant/Code/Project/public/index.php:8
PHP message: PHP 3. EntryPoint->loadTemplate() /home/vagrant/Code/Project/classes/EntryPoint.php:72” while reading response header from upstream, client: 192.168.10.1, server: homestead.test, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php7.2-fpm.sock:”, host: “192.168.10.10”

EDIT: um. should I mark this as solved? Or how do i denote that it’s no longer an issue (I’m assuming my machine was just screwy for 2 days)

Also, thank you for your reply!

The problem is almost certainly the URL. That page maps a URL to the controller action. If you go to /thisdoesnotexist it won’t match any of the if statements and the $page variable is not set, giving the error you had.

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