为什么AutorOuting(Legacy)在Codeigniter 4中不起作用?

问题描述 投票:0回答:1
i我有一个项目,其中包含在“ routes.php”文件中的get路由以及自动路线生成的其余路由,也就是说,从URL访问控制器的方法。这些控制器包含在模块中。我创建了以下目录结构:

app ├── Config │ └── Routes.php (refers to the routes created in each module folder /Config/Routes.php) ├── Modules │ └── Slink | ├── Config | └── Routes.php (Added a group with a single route) │ ├── Controllers │ ├── Home.php | └── Shortener.php │ ├── Models │ └── Slink.php │ └── Views │ ├── not_found.php | └── welcome_message.php └── ...
中的代码是:

app\Config\Routes.php

以及
<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

 $modules_path = APPPATH . 'Modules/';
 $modules = scandir($modules_path);
 
 foreach ($modules as $module) {
     if ($module === '.' || $module === '..') {
         continue;
     }
 
     if (is_dir($modules_path) . '/' . $module) {
         $routes_path = $modules_path . $module . '/Config/Routes.php';
         if (file_exists($routes_path)) {
             require $routes_path;
         } else {
             continue;
         }
     }
 }
中的代码如下:

app\Modules\Slink\Config\Routes.php

当我尝试从浏览器(
<?php

namespace App\Modules\Slink\Config;

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

 $routes->group(
    '', ['namespace' => 'App\Modules\Slink\Controllers'], function ($routes) {
        
        $routes->get('/', 'Home::welcome');
    }
);
)访问Get路由时,它返回预期的结果(来自控制器中的方法

http://localhost:8080/

的内容),但是当我使用类/方法路由时,返回404错误(例如:
welcome()
)。你知道会发生什么吗?
控制器的内容
app\Modules\Slink\Controllers\Home.php
是:

http://localhost:8080/shortener/prueba

Controller的含量是以下内容:

app\Modules\Slink\Controllers\Shortener.php

试图在浏览器中访问类/方法路由:

Attached是

<?php

namespace App\Modules\Slink\Controllers;

use App\Controllers\BaseController;

class Shortener extends BaseController
{
    public function prueba()
    {
        echo "HOLA MUNDO";
    }
}
命令的结果(一切看起来都正确)。

app\Modules\Slink\Controllers\Home.php
    

auserguide建议在主404 page<?php namespace App\Modules\Slink\Controllers; use App\Controllers\BaseController; class Home extends BaseController { public function welcome(): string { return view('../Modules/Slink/Views/welcome_message'); } }

目录之外为

控制者(github.io),它们不能通过URI检测来自动将其路由。

为您的路线注册:

php spark routes
您设法将默认名称空间设置为
php codeigniter codeigniter-4
1个回答
0
投票
,以便可以解决to to to to to to condectey to condects to,以注册该路由的路由,但是您不添加当前缺少的控制器。

功能显示的是,您成功地设法设法注册控制器的默认命名空间,这足以发现路由,但仅当控制器文件在app/Controllers目录中为否则,路由才有效,除非路由无法解决(从此以后您会发现一个404文件错误)。 即可添加您实际要配置的路由:

$routes->group(
    '', ['namespace' => 'App\Modules\Slink\Controllers'], function ($routes) {
        
        $routes->get('/', 'Home::welcome');
    }
);

或在自动装饰(Legacy)的地方记录并在工作地点编写您的控制器。

在additionditiondity上,我也建议您也遵循有关将模块放在

App\Modules\Slink\Controllers
目录旁而不是内部的建议:

Home::welcome

请参阅
代码模块(github.io)
,这也应该帮助您不需要依靠路由的自写文件发现等。
    

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.