【问题标题】:Error Loading Model Table in Controller在控制器中加载模型表时出错
【发布时间】:2017-01-11 05:49:02
【问题描述】:

目前我在开发一个小房子框架,遇到一些命名空间的小问题,当我想加载一个Model时,页面会在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(应用程序/控制器)

namespace App\Controller;


use App\Table\PostsTable;

class PostsController extends Controller {

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

PostsTable.php(应用程序/模型/表格)

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,错误。

(!) 致命错误:未捕获的错误:第 10 行的 D:\Sites\Dev\Framework\app\Controller\PostsController.php 中未找到类 'Table\PostsTable'
( ! ) 错误:在第 10 行的 D:\Sites\Dev\Framework\app\Controller\PostsController.php 中找不到类 'Table\PostsTable'

【问题讨论】:

    标签: php model frameworks composer-php


    【解决方案1】:

    您的作曲家配置似乎有误。

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

    将其更改为:"App\\": "app" 并更新作曲家

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

    use App\Table\PostsTable;

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

    【讨论】:

    • 感谢您的回复,但现在我有一个新错误,它不再找到控制器致命错误:D:\Sites\Dev\Framework\ 中找不到类'App\Controller\Controller' app\Controller\PostsController.php 在第 4 行
    • 你的文件夹真的叫“Controllers”吗,因为你的命名空间没有最后一个字母。这让我很困惑
    • 是的,控制器的名字在“s”结尾。
    • 那么命名空间也必须在末尾使用“s”。
    • 感谢 google ...我上一篇文章的翻译问题。我的课程是单数(不带 s)见下图)prntscr.com/du5pxt
    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2019-12-06
    • 2016-02-18
    • 2020-01-22
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多