2017-01-10 60 views
0

当前我开发了一个小型房屋框架,并且遇到一些名称空间的小问题,当我想加载模型时,页面将在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 protected]" 
     } 
    ], 
    "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 /型号/表)

namespace App\Model\Table; 

class PostsTable { 

} 

Controller.php这样(SRC /控制器)

<?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时,出错。

(!) Fatal error: Uncaught Error: Class 'Table\PostsTable' not found in D:\Sites\Dev\Framework\app\Controller\PostsController.php on line 10
(!) Error: Class 'Table\PostsTable' not found in D:\Sites\Dev\Framework\app\Controller\PostsController.php on line 10

回答

0

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

这下面一行在文件夹SRC定义命名空间应用:
"App\\": "src"

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

编辑:你也使用了错误的命名空间模型:

use App\Table\PostsTable;

及更高版本: PostsTable.php(app/Model/Table)

+0

感谢您的回复,但现在我有一个新的错误,它不再发现控制器 致命错误:类'App \ Controller \ Controller'找不到D:\ Sites \ Dev \ Framework \ app \第4行的Controller \ PostsController.php – Darkh

+0

你的文件夹真的叫做“Controller ** s **”,因为你的命名空间没有最后一个字母。它令我困惑 – Sysix

+0

是的,名字很好控制器在最后的“s”中。 – Darkh

相关问题