Class 在“...”中未找到“...” | PHP

Class '...' not found in '...' | PHP

我已经查找了一些资源,而且我很确定我正在做它应该做的事情。除非我真的筋疲力尽而无法注意到确切的错误。

我正在开发一个可以使用 PhalconPHP 构建的简单 REST API 项目。我按照微教程进行了一些小的修改,其中我将数据库调用分离到 \service 文件夹下的单独 .service.php 文件中,以减少路由实现上的代码。我似乎找不到它抛出以下错误的任何原因,但我一直得到它:

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

看来我真的是精疲力尽了。为了能够在包含在另一个 class 中的命名空间下使用 class,您实际上必须在声明中指定命名空间的结尾。

即:

use App\Namespace1\Services;

然后

new Services\ServiceClass();

我的错,真的。