Codeigniter 4 - 路由现在工作 / 404 / 控制器或其方法未找到

Codeigniter 4 - route now working / 404 / Controller or its method is not found

这是我的几条路线;

这个有效:

$routes->get('/admin/users', 'Admin/User/User_Controller::user_index');

这个不行:

$routes->get('/admin/toggle_user_is_active/(:num)','Admin/User/User_Controller::toggle_user_is_active/');

如您所见,它正在调用相同的方法。传入的值是一个用户 ID,如 72。如果 active 在数据库中为 1,则将其设置为 0,反之亦然,因此名称为 toggle_user_is_active($id).

如果我直接输入URL如下:

https://example.com/admin/toggle_user_is_active/72

我收到以下错误:

404 file not found
Controller or its method is not found: \App\Controllers\Admin::index

视图中的切换如下:

<a href="<?= site_url('admin/users/toggle_user_is_active/'.$user->id)?>"> Toggle </a>  

点击后产生:

https://example.com/admin/toggle_user_is_active/72

挠头!任何指点表示赞赏。

命名空间是使用反斜杠 (\) 定义的,不是 正斜杠 (/)。

而不是:
'Admin/User/User_Controller::toggle_user_is_active/'

使用这个:
'Admin\User\User_Controller::toggle_user_is_active/'

Name resolution rules

Qualified name

This is an identifier with a namespace separator, such as Foo\Bar

Setting your own routing rules

The controller and method should be listed in the same way that you would use a static method, by separating the fully-namespaced class and its method with a double-colon, like Users::list.

查看我的示例代码并像我一样编辑你的代码


<?php

/*
 * Myth:Auth routes file.
 */

$routes->group('api', ['namespace' => 'Modules\Home\Controllers'], function ($routes) {


    $routes->group('home', function ($routes) {

        $routes->get('', 'Home::index');
        $routes->get('news-list', 'Home::news');
        $routes->get('news-comment', 'Home::newsComment');
        $routes->get('news-show/(:num)', 'Home::newsShow/');
        $routes->get('fast-food-list', 'Home::fastFood');
        $routes->get('fast-food-comment', 'Home::fastFoodComment');
        $routes->get('fast-food-show/(:num)', 'Home::fastFoodShow/');
        $routes->get('setting-list', 'Home::settings');
        $routes->get('view-list', 'Home::views');
        $routes->get('advertisement-list', 'Home::advertisements');

        $routes->get('visitor-save', 'Home::visitor');
        $routes->post('contact-save', 'Home::contact');

    });
});