Использование пространств имен для создания экземпляров таблиц базы данных и контроллеров.Когда я пытаюсь загрузить «RegisterController», я получаю сообщение об ошибке «Внимание: требуется (../ Furniture / Controllers / page.php): не удалось открыть поток: нет такого файла или каталога в / srv / http / assignment/autoload.php в строке 6 '.
Найти это нечетным, поскольку загружаются экземпляры базы данных, а контроллер - нет?
//AutoLoad.php
<?php
function autoload($className)
{
$fileName = str_replace('\\', '/', $className) . '.php';
$file = '../' . $fileName;
require $file;
}
spl_autoload_register('autoload');
// Routes.php
<?php
namespace Furniture;
use Furniture\Conrollers\pageController;
class Routes implements \classes\Routes {
public function getRoutes(){
require '../classes/DatabaseConnection.php';
$furnitureTable = new \classes\databaseFunctions($pdo, 'furniture', 'id');
$categoryTable = new \classes\databaseFunctions($pdo, 'category', 'id');
$usersTable = new \classes\databaseFunctions($pdo, 'users', 'id');
$registerController = new \Furniture\Controllers\page($usersTable);
//$registerController = new \Furniture\Controllers\RegisterController($usersTable);
$routes = [
'' => [
'GET' => [
'controller' => $pageController,
'function' => 'home',
]
],
'about' => [
'GET' => [
'controller' => $pageController,
'function' => 'about',
]
]
];
return $routes;
}
}
//Pagecontroller.php
<?php
namespace Furniture\Controllers;
class page {
private $categoryTable;
public function __construct($usersTable)
{
$this->$usersTable = $usersTable;
}
public function home()
{
return ['template' => 'home-template.php',
'title' => 'This is the new home page',
'variables' => [
]
];
}
public function about()
{
return ['template' => 'about-template.php',
'title' => 'This is the new about page',
'variables' => [
]
];
}
public function faq()
{
return ['template' => 'faq-template.php',
'title' => 'This is the new faq page',
'variables' => [
]
];
}
public function contact()
{
return ['template' => 'contact-template.php',
'title' => 'This is the new contact page',
'variables' => [
]
];
}
public function furniture()
{
return ['template' => 'furniture-template.php',
'title' => 'This is the new furniture page',
'variables' => [
]
];
}
}
?>
//EntryPoint.php
<?php
namespace classes;
class EntryPoint {
private $routes;
public function __construct($routes){
$this->routes = $routes;
}
public function run() {
$route = strtolower(ltrim(explode('?', $_SERVER['REQUEST_URI'])[0], '/'));
$routes = $this->routes->getRoutes();
$method = $_SERVER['REQUEST_METHOD'];
$controller = $routes[$route][$method]['controller'];
$functionName = $routes[$route][$method]['function'];
$page = $controller->functionName();
$output = $this->loadTemplate('../templates/' . $page['template'], $page['variables']);
$title = $page['title'];
require '../templates/layout.html.php';
}
function loadTemplate($fileName, $templateVars) {
extract($templateVars);
ob_start();
require $fileName;
$content = ob_get_clean();
return $content;
}
}