ZF3同一模块多控制器问题:Album模块加InfoController后404报错
Hey there! Let's break down your issue step by step. First off, yes, the expected URL for your InfoController should indeed be something like http://your-domain/album/info (assuming your base URL is set up correctly). Now let's dig into why you're hitting that 404 even after following the tutorial—here are the most common culprits to check:
1. Verify Route Configuration in Module.php
ZF3 relies heavily on routing configuration to map URLs to controllers. Make sure you've added the info route (either as a child route of the main album route or updated the parent route's constraints):
Option 1: Add as a Child Route
In your Album module's Module.php, update the getConfig() method's router section:
use Zend\Router\Http\Segment; use Album\Controller\{AlbumController, InfoController}; public function getConfig() { return [ 'router' => [ 'routes' => [ 'album' => [ 'type' => Segment::class, 'options' => [ 'route' => '/album[/:action[/:id]]', 'constraints' => [ 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[0-9]+', ], 'defaults' => [ 'controller' => AlbumController::class, 'action' => 'index', ], ], 'may_terminate' => true, 'child_routes' => [ 'info' => [ 'type' => Segment::class, 'options' => [ 'route' => '/info', 'defaults' => [ 'controller' => InfoController::class, 'action' => 'index', ], ], ], ], ], ], ], // Other config sections... ]; }
Option 2: Update Parent Route Constraints
If you want info to be a direct action under the album route, ensure the action constraint allows it (the default regex already does, but double-check):
'constraints' => [ 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', // This allows 'info' 'id' => '[0-9]+', ],
The child route approach is cleaner for separate controllers, though.
2. Confirm Controller Class & File Setup
ZF3 follows PSR-4 autoloading rules strictly—tiny mismatches here cause 404s:
- The controller file must be at
module/Album/src/Controller/InfoController.php - The class must use the correct namespace and extend
AbstractActionController:
namespace Album\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class InfoController extends AbstractActionController { public function indexAction() { return new ViewModel(); } }
Double-check that the class name is exactly InfoController (case-sensitive!) and the filename matches.
3. Check Controller Service Registration
If you've customized controller factories, ensure InfoController is registered in Module.php's getControllerConfig() method:
use Zend\ServiceManager\Factory\InvokableFactory; use Album\Controller\InfoController; public function getControllerConfig() { return [ 'factories' => [ InfoController::class => InvokableFactory::class, // Other controllers like AlbumController... ], ]; }
If you don't have this method, ZF3 will auto-register it by default—but adding it explicitly avoids autoloading issues.
4. Clear Configuration Cache
ZF3 caches config files for performance, so if you made changes but didn't clear the cache, the old config is still being used:
- Delete all files in the
data/cachedirectory of your project - Or disable config caching in development by editing
config/application.config.php:
return [ // ... 'config_cache_enabled' => false, // ... ];
5. Verify View Script Location (Secondary Check)
While a missing view won't cause a 404 (you'd get a "view not found" error instead), it's good to confirm your view file exists at module/Album/view/album/info/index.phtml—this ensures everything works end-to-end once the routing is fixed.
Go through each of these checks one by one—small oversights in routing or naming conventions are super common when starting out with ZF3. Let me know if any of these steps resolve your 404, or if you need to dive deeper into a specific part!
内容的提问来源于stack exchange,提问作者Linh La

