首页 > 解决方案 > CodeIgniter 4 只有默认路由 / 是可访问的,如果我在我的 Routes.php 中定义,则没有其他路由可用。我正在使用 ubuntu

问题描述

我在 app/.htaccess 和项目目录中的 .htaccess 文件在这里。两者都是一样的。我正在使用相同的 .htaccess 文件,该文件存在于应用程序和项目目录的公共目录中

Routes.php 在这里

<?php 
namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH . 'Config/Routes.php'))
{
    require SYSTEMPATH . 'Config/Routes.php';
}

/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$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('/test', 'Home::test');
$routes->get('/test', 'Home::test');
/**
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php'))
{
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}

开/它工作正常。但是在 /test 上它给了我错误。

在此服务器上找不到请求的 URL。我该如何解决这个问题。我的 index.php 工作正常。

标签: phpcodeigniterroutescodeigniter-4

解决方案


尝试这个

$routes->get('/test', 'App\Controllers\Home::test');

但要使其工作,您需要在 Controllers 文件夹中有一个名为 Home 的控制器,并且在控制器内还有一个名为 test 的方法。


推荐阅读