PhalconPHP项目Test1RouteService类未找到错误求助
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:
- Commented-out namespaces: All your namespace declarations are commented with
#, so PHP doesn't recognize your classes' correct namespace locations. - Incorrect loader paths: The
__DIR__references in your loader are pointing to non-existent subdirectories (e.g.,__DIR__ . '/app/models/'resolves toconfigurations/app/models/instead of the actualapp/models/). - 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
- Save all modified files
- Restart your XAMPP Apache server
- Visit
http://localhost/my-api/v1/test/db_version— you should seedoneprinted without any fatal errors.
内容的提问来源于stack exchange,提问作者Nii

