我已经查找了一些资源,并且我很确定自己正在做应该做的事情。除非我真的精疲力尽,不能注意到确切的错误。
我正在研究一个可以使用PhalconPHP构建的简单REST API项目。我按照Micro教程进行了一些小的修改,在其中我将数据库调用分离到.service.php
文件夹下的单独\service
文件中,以减少路由实现上的代码。我似乎找不到任何原因将其抛出以下错误,但我一直得到它:
Fatal error: Uncaught Error: Class 'MyAPI\App\Routes\v1\Test\Test1RouteService' not found in C:\xampp\htdocs\my-api\app\routes\v1\test\test_1_route.php:26 Stack trace: #0 [internal function]: Closure->MyAPI\App\Routes\v1\Test\{closure}() #1 C:\xampp\htdocs\my-api\index.php(46): Phalcon\Mvc\Micro->handle() #2 {main} thrown in C:\xampp\htdocs\my-api\app\routes\v1\test\test_1_route.php on line 26
以下是我当前拥有的文件(.htaccess除外):
MyAPI\app\configurations\loader.php:
<?php
#
# Namespace Declaration
#
namespace MyAPI\App\Configurations;
#
# Use Namespaces
#
use Phalcon\Loader;
$loader = new Loader();
$loader->registerDirs(
array(
__DIR__ . '/app/configurations/',
__DIR__ . '/app/models/',
__DIR__ . '/app/routes/v1/test/',
__DIR__ . '/app/services/v1/test/'
)
)->register();
?>
MyAPI\app\configurations\database.php:
<?php
#
# Namespace Declaration
#
namespace MyAPI\App\Configurations;
#
# Use Namespaces
#
use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as MySqlPDO;
$di = new FactoryDefault();
$di->set('db', function() {
return new MySqlPDO(
[
'host' => '127.0.0.1',
'username' => 'root',
'password' => 'd0r3m1f4s0',
'dbname' => 'test_db'
]
);
});
?>
MyAPI\index.php:
<?php
#
# Namespace Declaration
#
namespace MyAPI;
#
# Required Files
#
require 'app/configurations/database.php';
require 'app/configurations/loader.php';
#
# Use Namespaces
#
use Phalcon\Mvc\Micro;
use Phalcon\Http\Response;
#
# Route declaration and implementation
#
$app = new Micro($di);
$app->notFound(function() use ($app) {
$response = new Response();
$response->setStatusCode(404, "Not Found");
$response->setJsonContent([
"status" => 404,
"message" => "The specified route was not found, or does not exist.",
"data" => null
]);
return $response;
});
#
# API Routes
#
require 'app/routes/v1/test/test_1_route.php';
$app->handle();
MyAPI\app\routes\v1\test\test_1_route.php:
<?php
#
# Namespace Declaration
#
namespace MyAPI\App\Routes\v1\Test;
#
# Required Files
#
require 'app/services/v1/test/test_1_route.service.php';
#
# Use Namespaces
#
use MyAPI\App\Services\v1\Test;
$app->get('/v1/test/what_is', function() {
echo "My API";
});
$app->get('/v1/test/db_version', function() {
// echo "called db_version route.";
$service = new Test1RouteService();
// echo $service->GetDbVersion();
});
?>
MyAPI\app\services\v1\test\test_1_route.service.php:
<?php
#
# Namespace Declaration
#
namespace MyAPI\App\Services\v1\Test;
#
# Use Namespaces
#
use Phalcon\Mvc\Model\Resultset\Simple as Resultset;
use Phalcon\Di\FactoryDefault;
class Test1RouteService
{
public function GetDbVersion()
{
// $sqlQry = "SELECT VERSION();";
// $conn = FactoryDefault::getDefault()->get("db");
return "done";
}
}
?>
我知道我可能会或可能不会实现应有的功能,我稍后再讲。现在,我想要一些我可以开始使用的东西。
编辑:我正在使用PhalconPHP 3.4.3和PHP 7.3.0
答案 0 :(得分:0)
看来我真的很精疲力尽。为了能够在另一个类中包含的命名空间下使用一个类,实际上您必须在声明中指定命名空间的末尾。
即:
使用App \ Namespace1 \ 服务;
然后
新的服务 \ ServiceClass();
我的坏,真的。