0
votes

I am developing my own & small MVC framework for my portfolio and as barebone for future developments.

I encountered a problem which I couldn't solve for an good hour.

I am using composer to build autoload files as well as namespaces.

I have a baseController which is very simple at moment but the problem I got is that for some reason I cannot extend it from the default controller which I'm trying to create.

This is my welcomeController.php file (extends baseController):

<?php
namespace AppWorld\Controls;
use AppWorld\FrostHeart;

class landingController extends baseController {

public function index() {

    $data['foo'] = "Foo";
    $data['bar'] = "Bar";

    $this->View->show('landingpage', $data, $showTemplate = 0);
}

}

This is my baseController.php:

<?php
namespace AppWorld\FrostHeart;

class baseController {

    //@var::View - Instance of View object
    public $currentView;

    public function __construct() {

        //Start the session
        session_start();

        //Create new view object
        $this->currentView = new View();
    }
}

I have tried all combinations of name spaces because I was so helpless but I was still unable to solve the problem.

welcomeController.php is in root/application/controllers namespaced as AppWorld\Controls

baseController.php is in root/application/core namespaced as AppWorld\FrostHeart

@Edit: That's the error:

Fatal error: Class 'AppWorld\FrostHeart\baseController' not found in D:\Software\xampp\htdocs\cms\application\controller\landingController.php on line 5

@Edit2:

composer.json

{
    "name": "simplymvc",
    "description": "Simple MVC with options to expand upon",
    "license": "MIT",
    "version": "0.0.1-dev",
    "authors": [
        {
            "name": "Maciej Olborski",
            "email": "[email protected]",
            "homepage": "http://maciejolborski.net",
            "role": "Lead"
        }
    ],
        "require": {
            "php": ">=5.6.8"
        },
        "autoload": {
            "psr-4":{
                "AppWorld\\": "application",
                "AppWorld\\Conffy\\": "application/config",
                "AppWorld\\FrostHeart\\": "application/core",
                "AppWorld\\Controls\\": "application/controller"
            }
        }
}

@Edit3

So this is Application.php:

<?php
namespace AppWorld\FrostHeart;

class Application {

    // $var::Mixed - Instance of the controller
    private $controller;
    // $var::Array - Parameters passed to the URL
    private $parameters = array();  
    // $var::String - Current controller name
    private $controller_name;
    // $var::String - Current method name
    private $method_name;

    public function __construct() {
        //Prepare URL and set URL parameters
        $this->prepareURL();
        //Check if controller and/or method are not empty
        $this->checkControllerAndMethod();

        //Check if controller file exists?
        if(file_exists(CONTROLLER_DIR . $this->controller_name . ".php")) 
        {
            //Load controller file and and create this controller
            require(CONTROLLER_DIR . $this->controller_name . ".php");

            $this->controller = new $this->controller_name();

            //Check if the method exists within this controller
            if(method_exists($this->controller, $this->method_name)) 
            {                               
                // call method and pass parameters to this method
                call_user_func_array(array($this->controller, $this->method_name), $this->parameters);
            } 
            else 
            {
                //When no parameters are given just call method without parameters
                $this->controller->{$this->method_name}();
            }
        }
    }

    private function prepareURL() {

        //GET URL and split it to URL Segments
        $params = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
        $params = trim($params, '/');
        $params = filter_var($params, FILTER_SANITIZE_URL);
        $params = explode('/', $params);

        //Set controller name to first URL Segment within GET['action']
        $this->controller_name = $params[0];
        //Set method name to first URL Segment within GET['action']
        $this->method_name = $params[1];     

        unset($params[0], $params[1]);

        //Store array of parameters(URL Segments) to parameters array
        $this->parameters = array_values($params);
    }

    /**
     * 
     * This method checks if the controller and/or method was provided in the URL 
     * If the controller and/or method was not provided then defaults are loaded from config file.
     * Also renames the Controller name so it's landingController not just landing.
     * 
     */

    private function checkControllerAndMethod() {

        //Check whether the controller_name is set if not load default controller
        if(!$this->controller_name) 
        {
            $this->controller_name = DEFAULT_CONTROLLER;
        }

        //Check whether the method_name is set if not load default method
        if(!$this->method_name || strlen($this->method_name) === 0) 
        {
            $this->method_name = DEFAULT_METHOD;
        }

        //Rename the controller_name so that it contains "Controller" word after the name
        $this->controller_name = $this->controller_name . 'Controller';

        /**
        echo 'Controller: ' . $this->controller_name;
        echo '<br />';
        echo 'Method: ' . $this->method_name;
        **/
    }
}

If this line is commented:

$this->controller = new $this->controller_name();

and this line is commented:

$this->controller->{$this->method_name}();

then I can see all files being loaded properly including the base and landing controller but if I leave them uncommented the error is:

Fatal error: Class 'landingController' not found in D:\Software\xampp\htdocs\cms\application\core\Application.php on line 27

1
Please post your autoloading config from composer.json - Ian
I've now added it its @Edit2 - Maciej Olborski

1 Answers

0
votes

Try changing the use line to use AppWorld\FrostHeart\baseController;