You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PhalconPHP项目Test1RouteService类未找到错误求助

Fatal Error: Class 'MyAPI\App\Routes\v1\Test\Test1RouteService' not found in PhalconPHP 3.4.3 + PHP 7.3.0

Let's break down why this error is happening and fix it step by step — I've dealt with similar Phalcon autoload headaches before, so this should get your API up and running quickly.

Root Causes

Your issue stems from three key problems:

  1. Commented-out namespaces: All your namespace declarations are commented with #, so PHP doesn't recognize your classes' correct namespace locations.
  2. Incorrect loader paths: The __DIR__ references in your loader are pointing to non-existent subdirectories (e.g., __DIR__ . '/app/models/' resolves to configurations/app/models/ instead of the actual app/models/).
  3. Misreferenced service class: Your route file isn't correctly importing the service class from its namespace.

Step 1: Fix the Phalcon Loader Paths (loader.php)

First, fix the directory paths in your loader to point to the correct locations, and enable the namespace declaration:

<?php
namespace MyAPI\App\Configurations;
use Phalcon\Loader;

$loader = new Loader();
// Use relative paths from the configurations directory to the app root
$loader->registerDirs(
    array(
        __DIR__ . '/../configurations/',
        __DIR__ . '/../models/',
        __DIR__ . '/../routes/v1/test/',
        __DIR__ . '/../services/v1/test/'
    )
)->register();
?>

For better long-term maintainability, I'd recommend switching to registerNamespaces (Phalcon's preferred method) instead of registerDirs — this maps namespaces directly to directories:

<?php
namespace MyAPI\App\Configurations;
use Phalcon\Loader;

$loader = new Loader();
$loader->registerNamespaces(
    array(
        'MyAPI\App\Models' => __DIR__ . '/../models/',
        'MyAPI\App\Routes\v1\Test' => __DIR__ . '/../routes/v1/test/',
        'MyAPI\App\Services\v1\Test' => __DIR__ . '/../services/v1/test/'
    )
)->register();
?>

Step 2: Enable Namespaces in All Files

Remove the # comments from namespace declarations in every file so PHP can track your classes correctly.

2.1 Update the Route File (test_1_route.php)

Fix the namespace, correctly import the service class, and remove the manual require (the loader will handle autoloading):

<?php
namespace MyAPI\App\Routes\v1\Test;
// Import the exact service class from its namespace
use MyAPI\App\Services\v1\Test\Test1RouteService;

$app->get('/v1/test/what_is', function() {
    echo "My API";
});
$app->get('/v1/test/db_version', function() {
    $service = new Test1RouteService();
    echo $service->GetDbVersion();
});
?>

2.2 Update the Service File (test_1_route.service.php)

Enable its namespace declaration:

<?php
namespace MyAPI\App\Services\v1\Test;
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";
    }
}
?>

2.3 Optional: Clean Up index.php and database.php

Enable their namespaces for consistency:
index.php:

<?php
namespace MyAPI;
require 'app/configurations/database.php';
require 'app/configurations/loader.php';
use Phalcon\Mvc\Micro;
use Phalcon\Http\Response;

$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;
});
require 'app/routes/v1/test/test_1_route.php';
$app->handle();
?>

database.php:

<?php
namespace MyAPI\App\Configurations;
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'
        ]
    );
});
?>

Step 3: Test Your Fix

  1. Save all modified files
  2. Restart your XAMPP Apache server
  3. Visit http://localhost/my-api/v1/test/db_version — you should see done printed without any fatal errors.

内容的提问来源于stack exchange,提问作者Nii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:09:57