Click here to Skip to main content
15,891,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I think I might have a problem with my autoloader not working correctly.

I think the safest thing to do is show you my directories

Directory Layout

And here is my autoloader

PHP
<?php
    // simple autoloader
    spl_autoload_register(function ($className) {
        if (substr($className, 0, 4) !== 'Mvc\\') {         //' // [EDIT: Format fix]
            // not our business
            return;
        }

        $fileName = __DIR__.'/'.str_replace('\\', DIRECTORY_SEPARATOR, substr($className, 4)).'.php';                //' // [EDIT: Format fix]


        if (file_exists($fileName)) {
            include $fileName;
        }
    });

    // get the requested url
    $url      = (isset($_GET['_url']) ? $_GET['_url'] : '');
    $urlParts = explode('/', $url);

    // build the controller class
    $controllerName      = (isset($urlParts[0]) && $urlParts[0] ? $urlParts[0] : 'index');
    $controllerClassName = '\\Mvc\\Controller\\'.ucfirst($controllerName).'Controller';                 //' // [EDIT: Format fix]


    // build the action method
    $actionName       = (isset($urlParts[1]) && $urlParts[1] ? $urlParts[1] : 'index');
    $actionMethodName = $actionName.'Action';

    try {
        if (!class_exists($controllerClassName)) {
            throw new \Mvc\Library\NotFoundException();
        }

        $controller = new $controllerClassName();

        if (!$controller instanceof \Mvc\Controller\Controller || !method_exists($controller, $actionMethodName)) {
            throw new \Mvc\Library\NotFoundException();
        }

        $view = new \Mvc\Library\View(__DIR__.DIRECTORY_SEPARATOR.'views', $controllerName, $actionName);
        $controller->setView($view);

        $controller->$actionMethodName();
        $view->render();

    } catch (\Mvc\Library\NotFoundException $e) {
        http_response_code(404);
        echo 'Page not found: '.$controllerClassName.'::'.$actionMethodName;
    } catch (\Exception $e) {
        http_response_code(500);
        echo 'Exception: '.$e->getMessage().'<br><pre>'.$e->getTraceAsSt . ring().




The error is thrown on line 30 which would be:
throw new \Mvc\Library\NotFoundException();


It also says :

Stack trace: #0 {main}


What am I missing here?

Thank you in advance.

What I have tried:

I have tried moving the direcotires around thinking that is has something to do with my xxamp configuration.
Posted
Updated 28-Jul-18 10:14am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900