如何使用 Codeigniter 4.2.6 修复“无法访问默认控制器”?

分享于2022年11月15日 codeigniter-4 php routes 问答
【问题标题】:How to fix 'Cannot access the default controller' using Codeigniter 4.2.6?如何使用 Codeigniter 4.2.6 修复“无法访问默认控制器”?
【发布时间】:2022-11-07 07:03:22
【问题描述】:

我在 Codeigniter 4.2.6 中使用 URI 路由时遇到问题。我有控制器名称 Home 和方法名称 getIndex 。访问 http://localhost:8080 时一切正常。当我尝试 http://localhost:8080/home/index 时,会出现一条消息 'Cannot access the default controller "Home" with the controller name URI path' 。我设置了 $routes->setAutoRoute(true); public bool $autoRoutesImproved = true; 。当我创建另一个方法 getAbout 时同样的问题。访问 http://localhost:8080/home/about 也会产生一条消息 Cannot accessing...

使用 Sub directory 分离逻辑时同样的问题。这是我的子目录名称代码 Admin

并尝试访问它得到相同的结果 Cannot access the default controller "Home" with the controller name URI path.

那么如何在 codeigniter 4 中使用 URI 路由,尤其是 4.2.6 使用 Auto Routing enable 和 Manual Routing 呢?

先感谢您。

更新

这是我的 Routes.php

setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// ...
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
$routes->setAutoRoute(true);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');


【解决方案1】:
  1. 文件: app/Config/Feature.php
    public bool $autoRoutesImproved = true;
    
    1. 文件: app/Config/Routes.php
    $routes->setDefaultNamespace('AppControllersAdmin');
    $routes->setDefaultController('AppControllersAdminHome');
    $routes->setDefaultMethod('index');
    $routes->setTranslateURIDashes(false);
    $routes->set404Override();
    // The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
    // where controller filters or CSRF protection are bypassed.
    // If you don't want to define all routes, please use the Auto Routing (Improved).
    // Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
    $routes->setAutoRoute(true);
    
    // We get a performance increase by specifying the default
    // route since we don't have to scan directories.
    $routes->addRedirect('/', '/home');
    

    所做的更改: https://github.com/steven7mwesigwa/ci4.2.7/commit/2ec6853716029d5704933ed95622875fb3fa6728

    示例回购: https://github.com/steven7mwesigwa/ci4.2.7

    附录

    A. 在您的网络浏览器中,而不是: http://localhost:8080/home/index ,使用 http://localhost:8080/home

    http://localhost:8080/home/about 。这个 URI 应该可以正常工作 没有 任何“用户定义的路线”。

【讨论】:

  • 谢谢你。我会先检查其他情况...
  • http://localhost:8080/home/about 在使用 $routes->setDefaultNamespace('AppControllersAdmin') 时显示消息 Controller or its method is not found: AppControllersAdminHomeAboutHome::getIndex 。在不更改 setDefaultNamespace 的情况下,抛出消息 Cannot access the default controller "Home" with the controller name URI path. 。我错过了什么吗?为什么无法通过 URI 访问默认控制器?
  • @steven7mwesigma 谢谢史蒂文,非常感谢您的帮助! +1 回购