这是我的几条路线;
这个有效:
$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/$1');
正如您所看到的,它正在调用相同的方法。传入的值是一个 userid,如 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
View 中的切换如下:
<a href="<?= site_url('admin/users/toggle_user_is_active/'.$user->id)?>"> Toggle </a>
点击时会产生:
https://example.com/admin/toggle_user_is_active/72
挠我头!任何指示表示赞赏。
最佳答案
命名空间是使用反斜杠 (\
) 定义的,不是正斜杠 (/
)。
而不是:
'管理员/用户/User_Controller::toggle_user_is_active/$1'
使用这个:
'Admin\User\User_Controller::toggle_user_is_active/$1'
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 - Codeigniter 4 - 路线现在工作/404/ Controller 或其方法未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71088499/