在Controller

时间:2017-01-10 20:59:01

标签: php model frameworks composer-php

目前我开发了一个小型的房屋框架,我遇到了一些名称空间的小问题,当我想加载一个模型时,该页面将在Controller中查找Table文件,尽管使用了名称空间。

我的建筑:

/app/
    /Controllers/
    /View/
    /Model/
        /Table/
        /Entity/
/config/
/src/
/Config/
    Config.php
/Controller/
    Controller.php
/Database/
    Database.php
/Network/
    Request.php
Application.php
/vendor/
    /composer/
    autoload.php
composer.json
index.php

下面链接到控制器的页面的代码,我认为问题是由组成而不是......

composer.json

{
    "name": "damien/framework",
    "authors": [
        {
            "name": "Damien aaa",
            "email": "email@gmail.com"
        }
    ],
    "require": {},
    "autoload": {
        "psr-4": {
            "App\\": "src"
        }
    }
}

PostsController.php(app / Controller)

namespace App\Controller;


use App\Table\PostsTable;

class PostsController extends Controller {

    public function index() {
       $table = new PostsTable();
    }
}

PostsTable.php(app / Model / Table)

namespace App\Model\Table;

class PostsTable {

}

Controller.php(src / Controller)

<?php
namespace App\Controller;
use App\Application;
use App\Network\Request;

class Controller extends Application {
    protected $viewPath = ROOT . VIEW;
    protected $template = ROOT . TEMPLATE;
    protected $layout = "default";
    public $request;


    public function __construct()
    {
        $this->request = new Request();
        $this->render($this->request->controller().'/'. $this->request->action());
    }



    /*
     * $template String Renvoie le layout demander, si celui-ci existe
     */
    public function layout($template) {
        if($template != $this->layout) {
            require $this->template . $template . '.php';
        }
    }

    /*
     * $view string Renvoie la vue correspondant au controller et à la vue demander
     * $params string Permet de transmettre des variables à la vue
     */
    public function render($view, $params = []) {
        extract($params, EXTR_OVERWRITE);
        ob_start();
        if($view != null) {
            require $this->viewPath . '/' . str_replace('.', '/', $view) . '.php';
        } else {
            require $this->viewPath . '/' . $this->request->controller() . '/' . $this->request->action() . '.php';
        }
        $content = ob_get_clean();
        require $this->template .$this->layout . '.php';
    }

}

在上面的这些代码中,当我创建一个新的PostsTable时,我从控制器调用Table Posts,错误。

  

(!)致命错误:未捕获错误:类&#39;表\帖子表&#39;在第10行的D:\ Sites \ Dev \ Framework \ app \ Controller \ PostsController.php中找不到       (!)错误:Class&#39; Table \ PostsTable&#39;在第10行的D:\ Sites \ Dev \ Framework \ app \ Controller \ PostsController.php中找不到

1 个答案:

答案 0 :(得分:0)

看起来你的作曲家配置是错误的。

以下行定义文件夹src中的命名空间App:
"App\\": "app"

将其更改为: use App\Table\PostsTable; 并更新作曲家

编辑:您还为模型使用了错误的命名空间:

[{ "question": "What is the full form of IP?", "choices": ["Internet Provider", "Internet Port", "Internet Protocol"], "correct": "Internet Protocol" }, { "question": "Who is the founder of Microsoft?", "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak"], "correct": "Bill Gates" }, { "question": "1 byte = ?", "choices": ["8 bits", "64 bits", "1024 bits"], "correct": "8 bits" }, { "question": "The C programming language was developed by?", "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"], "correct": "Dennis Ritchie" }, { "question": "What does CC mean in emails?", "choices": ["Carbon Copy", "Creative Commons", "other"], "correct": "Carbon Copy" }];

以后: PostsTable.php(app / Model / Table)