Error on different lines.

NorseMan

Member
I get an error on line 38, 39, 52, 56, 58, 63 and 71. I cant find the error. I am looking and looking 😮
Can you help me find it?

Code:
<?php
class EntryPoint
{
	private $route;
	
	public function __construct($route)
	{
	$this->route = $route;
	$this->checkUrl();
	}
		
	private function checkUrl()
	{
		if ($this->route !== strtolower($this->route)) {
			http_response_code(301);
			header('location: ' . strtolower($this->route));
			
		}
	}
	
	private function loadTemplate($TemplateFileName, $variables = [])
	{
		extract($variables);
		
		ob_start();
		include __DIR__ . '/../templates/' . $templateFileName;
		
		include ob_get_clean();
	}

	private function callAction() 
	{
		include __DIR__ . '/../classes/databaseTable.php';
		include __DIR__ . '/../includes/dbconnection.php';
		
		$articleTable new DatabaseTable($pdo, 'article', 'id');
		$usersTable new DatabaseTable($pdo, 'users', 'id');
		
		if ($this->route === 'article/list') {
			include __DIR__ . '/../classes/controllers/articleController.php';
			$controller = new articleController($articleTable, $usersTable);
			$page = $controller->list();
		} elseif ($this->route === '') {
			include __DIR__ . '/../classes/controllers/articleController,php';
			$controller = new articleController($articleTable, $usersTable);
			$page = $controller->home();
		} elseif ($this->route === 'article/edit') {
			include __DIR__ . '/../classes/contollers/articleController.php';
			$controller = new articleController(articleTable, $usersTable);
			$page = Controller->edit();
		} elseif ($this->route === 'article/delete') {
			include __DIR__ . '/../classes/controllers/articleController.php';
			$controller = new articleController($articlesTable, $usersTable);
			$page $Controller->delete();
		} elseif ($this->route === 'register') {
			include __DIR__ '/../classes/controllers/registerController.php';
			$controller = new registerController($usersTable);
			$page = $controller->showForm();
		}	
		return $page
	}
	public function run()
	{
		$page = $this->callAction();
		$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';
	}
}
 
First of all try care more about the formatting, the code will be more readable and easily to debug.
Do you have plugin for php in the vscode? It should show you places where exactly are issues.
Let's describe all issues:
Line 38: the equals char is missing
Line 39: the equals char is missing
Line 52: the Controller is unknown, it should be $controller
Line 56: the equals char is missing
Line 58: the dot char is missing
Line 63: the semicolon was expected, but bracket found
Line 71: the $ char is missing

That's about issues that you have pointed. You can improve the code by:
1. set the type of properties and a method parameters
2. use namespaces
3. do not use include or require function
4. use dependency injector

how to write own router you can check here: https://dev.to/fadymr/php-create-your-own-php-router-4g0o
 
It was taking a little too long time before you answered, so I fixed myself. The problem was excactly as you described it. I have been working on clean up the script so it will be more readable. There's a long way to go before i am finished. I was so tired when i contacted you. Thats why I didn't find the errors myself thru the night.

You are listing up number 1 to 4. Tell me more about number 1 and what you mean?
Also about number number 3. What should i use instead of require and include? I don't have a clue :help:
And number 4. What is a "dependency injector"?

In VS Code i use the X Debug. Is this good enough? If not, what do you suggest as a replacement?

I also use DreamWeaver. Actialy i like this bether than VSC. or... I dont know right now.

Thaks for helping me 😊
Tell me if I can give you something back 😊👍
 
I will show you on an example:
1. set the type of properties and a method parameters
Code:
<?php
class EntryPoint
{
	private string $route;
	
	public function __construct(string $route)
	{
		$this->route = $route;
		$this->checkUrl();
	}
then you will assign only string
3. do not use include or require function
Code:
<?php // src/Routing/EntryPoint.php
namespace App\Routing;

use App\Database\DatabaseTable;
use App\Controller\ArticleControler;

// some code

	private function callAction() 
	{		
		$articleTable = new DatabaseTable($pdo, 'article', 'id');
		$usersTable = new DatabaseTable($pdo, 'users', 'id');
		
		switch ($this->route) {
			case 'article/list':
				$controller = new ArticleControler($articleTable, $usersTable);
				$page = $controller->list();
				break;
				// another routes
		}
Code:
<?php // public/index.php
// this is the file where include and required are allowed
// include autoloader in.ex from composer
include '../vendor/autoloader.php';
// include your config files
include '../bootstrap/config.php';
// include you routing
include '../src/EntryPoint.php';

$entryPoint = new EntryPoint($_SERVER['REQUEST_URI']);
$entryPoint->run();
the user should load the index.php first and in the file the autoloader is set up, thanks to that you don't need to include any class, because it will be included when your code will need it
you can read more about autoloader here: https://www.php.net/manual/en/language.oop5.autoload.php
it should be used with PSR-4: https://www.php-fig.org/psr/psr-4/
4. use dependency injector
Dependency Injector is a class that can create an instance of any class. It is easy when the class have parameterless constructor, but DI is mostly for creating the dependency and inject them to the constructor, for example, let's create class to get data from request:
Code:
public class Route {
	public function getPath(): string {
		return $_SERVER['REQUEST_URI'];
	}
}
then in your code we can change:
Code:
<?php
class EntryPoint
{
	private Route $route;
	
	public function __construct(Route $route)
	{
		$this->route = $route;
		$this->checkUrl();
	}
when we will create the EntryPoint by the DI, then the DI will create (or reuse, we can configure that for each class how to do) an instance of Route class and pass to the constructor, so the EntryPoint will have working Route class, and we do not need to create each needed class with all dependencies, we can simply put the parameter to the constructor. For the routing we can also configure the DI for other methods, for example to pass the request data to the method or data from the path like article id
You can find ready to use dependency injector here: https://php-di.org/
 
Really a huge work you have done to help me and make me understand. I appreciate it, I really do.
On this I have to use some time, because this was really much to memorize and swallow.
Thank you su much Michalio
 
Back
Top