首页 文章

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

提问于
浏览
0

我正在为我的投资组合开发自己的小型MVC框架,并作为未来发展的准系统 .

我遇到了一个问题,我无法解决好一个小时 .

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

我有一个baseController,它很简单,但我得到的问题是,由于某种原因,我不能从我正在尝试创建的默认控制器扩展它 .

这是我的welcomeController.php文件(扩展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);
}

}

这是我的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();
    }
}

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

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

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

@Edit:这是错误:

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": "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"
            }
        }
}

@ EDIT3

所以这是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 '
'; echo 'Method: ' . $this->method_name; **/ } }

如果这条线被评论:

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

这条线被评论道:

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

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

致命错误:第27行的D:\ Software \ xampp \ htdocs \ cms \ application \ core \ Application.php中找不到类“landingController”

1 回答

  • 0

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

相关问题