Функция автозапуска не может загрузить функцию контроллера

#php #function #controller #stream

#php #функция #контроллер #поток

Вопрос:

Использование пространств имен для создания экземпляров таблиц базы данных и контроллеров. Когда я пытаюсь загрузить ‘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 FurnitureConrollerspageController;

        class Routes implements classesRoutes {

            public function getRoutes(){
            require '../classes/DatabaseConnection.php';

            $furnitureTable = new classesdatabaseFunctions($pdo, 'furniture', 'id');
            $categoryTable = new classesdatabaseFunctions($pdo, 'category', 'id');
            $usersTable = new classesdatabaseFunctions($pdo, 'users', 'id');


            $registerController = new FurnitureControllerspage($usersTable);

            //$registerController = new FurnitureControllersRegisterController($usersTable);



            $routes = [
                '' => [
                    'GET' => [
                    'controller' => $pageController,
                    'function' => 'home',
                ]
                ],
                'about' => [
                    'GET' => [
                        'controller' => $pageController,
                        'function' => 'about',
                    ]
                ]
            ];

            return $routes;

            }


    }

//Pagecontroller.php
    <?php
    namespace FurnitureControllers;

    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;
        }


    }
  

Ответ №1:

Как я понимаю, имя файла Pagecontroller.php поэтому, пожалуйста, назовите свой класс соответственно Pagecontroller

 //Pagecontroller.php
    <?php
    namespace FurnitureControllers;

    class Pagecontroller {
  

Альтернативой было бы переименовать ваш файл в page.php как следует из сообщения об ошибке, он ищет такой файл