Problems in code example

if (isset($routes[$this->route]['login']) &&
 isset($routes[$this->route]['login']) &&
  !$authentication->isLoggedIn()) {
    header('location: /login/error');
}

1. You are checking the exact same thing twice (isset($routes[$this->route]['login'])).

2. The $authentication variable is not defined in the text. The following is missing above the if statement:

$authentication = $this->routes->getAuthentication();

The code example should instead look like this:

public function run()
{
	$routes = $this->routes->getRoutes();
	$authentication = $this->routes->getAuthentication();
	if (isset($routes[$this->route]['login']) && !$authentication->isLoggedIn()) {
		header('location: /login/error');
	} else {
		$controller = $routes[$this->route][$this->method]['controller'];
		$action = $routes[$this->route][$this->method]['action'];
		$page = $controller->$action();
		$title = $page['title'];
		if (isset($page['variables'])) {
			$output = $this->loadTemplate($page['template'], $page['variables']);
		} else {
			$output = $this->loadTemplate($page['template']);
		}
		include __DIR__ . '/../../templates/layout.html.php';
	}
}

Link to content: PHP & MySQL: Novice to Ninja, 6th Edition - Section 11

So I went and looked at the file in question in the github.

$authentication is defined on the line before this codeblock. Note that the codeblock in the text is NOT the whole file. It doesnt include the definition for $routes either, but it’s in the actual file. I can find no branch of the github that includes EntryPoint.php that does not include $authentication.

The double-isset check is weird, but not strictly a problem - the code will work as written, but is a bit redundant.

1 Like