Php psr-4 Class 使用 Composer 未发现错误
Php psr-4 Class not Found Error using Composer
我正尝试在 "wamp-server"
上使用 php 练习 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 是 above.And 它在我删除 PageController.php
中注释的两行时有效
<?php
echo "TEST PAGE <br>";
但是这行有一个错误:
Class 'Acme\Testing\Test' 未找到..
正如我在评论中所说,问题是名称空间不正确。
我为将来可能会发现这个问题的人创建了这个答案。
您有一个名为 source 的目录,其中包含命名空间 Acme。当您在名为 acme 的源文件夹中添加另一个目录时,命名空间将为:
Acme\acme
您必须将 acme 文件夹更改为 Testing 才能获得正确的命名空间。
我正尝试在 "wamp-server"
上使用 php 练习 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 是 above.And 它在我删除 PageController.php
中注释的两行时有效<?php
echo "TEST PAGE <br>";
但是这行有一个错误:
Class 'Acme\Testing\Test' 未找到..
正如我在评论中所说,问题是名称空间不正确。
我为将来可能会发现这个问题的人创建了这个答案。
您有一个名为 source 的目录,其中包含命名空间 Acme。当您在名为 acme 的源文件夹中添加另一个目录时,命名空间将为:
Acme\acme
您必须将 acme 文件夹更改为 Testing 才能获得正确的命名空间。