【发布时间】: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