PHP命名空间在不同的文件中

时间:2015-05-06 04:18:41

标签: php model-view-controller namespaces

我正在开发自己的&我的投资组合的小型MVC框架,以及未来发展的准系统。

我遇到了一个问题,我无法解决这个问题。

我正在使用composer来构建自动加载文件以及命名空间。

我有一个非常简单的baseController但我遇到的问题是由于某些原因我无法从我试图创建的默认控制器扩展它。

这是我的welcomeController.php文件(扩展baseController):

<?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();
    }
}

这是我的baseController.php:

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

我尝试了所有名称空间的组合,因为我太无奈了,但我仍然无法解决问题。

welcomeController.php位于root / application / controllers中,命名为AppWorld \ Controls

baseController.php在root / application / core命名空间中作为AppWorld \ FrostHeart

@Edit: 那是错误:

{
    "name": "simplymvc",
    "description": "Simple MVC with options to expand upon",
    "license": "MIT",
    "version": "0.0.1-dev",
    "authors": [
        {
            "name": "Maciej Olborski",
            "email": "olbi123@gmail.com",
            "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"
            }
        }
}

@ EDIT2:

composer.json

<?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;
        **/
    }
}

@ EDIT3

所以这是Application.php:

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

如果此行已注释:

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

并注释了这一行:

for (i = 1; ; i++)

然后我可以看到正确加载的所有文件,包括基本和登陆控制器,但是如果我将它们取消注释,则错误是:

  

致命错误:Class&#39; landingController&#39;在第27行的D:\ Software \ xampp \ htdocs \ cms \ application \ core \ Application.php中找不到

1 个答案:

答案 0 :(得分:0)

尝试将use行更改为use AppWorld\FrostHeart\baseController;