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建议在主<?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
您设法将默认名称空间设置为功能显示的是,您成功地设法设法注册控制器的默认命名空间,这足以发现路由,但仅当控制器文件在app/Controllers
目录中为否则,路由才有效,除非路由无法解决(从此以后您会发现一个404文件错误)。
即可添加您实际要配置的路由:
$routes->group(
'', ['namespace' => 'App\Modules\Slink\Controllers'], function ($routes) {
$routes->get('/', 'Home::welcome');
}
);
或在自动装饰(Legacy)的地方记录并在工作地点编写您的控制器。
在additionditiondity上,我也建议您也遵循有关将模块放在
App\Modules\Slink\Controllers
目录旁而不是内部的建议:
Home::welcome
,这也应该帮助您不需要依靠路由的自写文件发现等。