CodeIgniter 4中传统自动路由(autorouting)失效问题排查
CodeIgniter 4自动路由404问题排查方案
检查全局自动路由配置
打开app/Config/Routing.php,确认:$enabled设为true,确保自动路由功能开启- 若
$autoRoutesImproved为true,需遵循严格命名规则:控制器类名用大驼峰(如ShortenerController对应URL的shortener),方法用小驼峰且为公共方法(public);如果不想加Controller后缀,需将$controllerSuffix设为空字符串。
确认模块注册与配置
打开app/Config/Modules.php,确保Slink模块已正确注册:public $modules = [ 'Slink' => ROOTPATH . 'modules/Slink/', ];同时检查模块内的
modules/Slink/Config/Routes.php,不要添加禁用自动路由的配置。核对控制器路径与命名
控制器文件必须放在modules/Slink/Controllers/下,例如Shortener.php,类名需与文件名一致(大驼峰),示例代码:<?php namespace Slink\Controllers; use CodeIgniter\Controller; class Shortener extends Controller { public function prueba() { return 'Prueba方法返回内容'; } }注意
prueba方法必须是public权限,否则无法通过自动路由访问。验证路由实际路径
执行php spark routes命令,查看自动路由的完整路径:- 如果显示的路由是
/shortener/prueba,则访问http://localhost:8080/shortener/prueba - 如果路由带模块前缀(如
/slink/shortener/prueba),则需要调整URL路径。
- 如果显示的路由是
清除缓存并重启服务
执行php spark cache:clear清除路由缓存,然后重启php spark serve开发服务器,确保配置生效。
内容的提问来源于stack exchange,提问作者raulizbu
相关产品推荐
相关产品推荐

