我正在尝试使用php在“wamp-server”上运行MVC概念
在本地目录中,我有index.php页面,用于使用alto-router包和routes.php文件进行基本路由:
<?php
$router->map('GET','/test', 'Acme\Controllers\PageController@test' ,'test');
composer.json文件是:
{
"name": "eren/eren",
"authors": [
{
"name": "Eren Ardahan",
"email": "???@???.com"
}
],
"require": {
"filp/whoops": "^1.1",
"altorouter/altorouter": "1.1.0"
},
"autoload":{
"psr-4":{"Acme\\":"src/"}
}
}
PageController.php是
<?php namespace Acme\Controllers;
use Acme\Testing\Test;
class PageController
{
public function test(){
include(__DIR__ . "/../../views/test.php");
//---Deleted lines
$test = new Test;
$test -> test();
/---
}
}
acme目录中的Test.php是:
<?php namespace Acme\Testing;
class Test{
public function test(){
echo "Working";
}
}
视图目录中的test.php在上面。当我删除PageController.php中注释的两行时,它工作
<?php
echo "TEST PAGE <br>";
但是这行有一个错误:
类'Acme \ Testing \ Test'未找到..
答案 0 :(得分:0)
正如我在评论中所说,问题是名称空间不正确。
我为将来可能会发现这个问题的人创建了这个答案。
您有一个名为source的目录,其名称空间为Acme。当您在名为acme的源文件夹中添加另一个目录时,命名空间将为:
Acme\acme
您必须将acme文件夹更改为Testing才能获得正确的命名空间。