php - Codeigniter 4 - 路线现在工作/404/ Controller 或其方法未找到

标签 php codeigniter codeigniter-4

这是我的几条路线;

这个有效:

$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'

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 - Codeigniter 4 - 路线现在工作/404/ Controller 或其方法未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71088499/

相关文章:

php - 根据条件更新表(While 循环)

php - 是否可以先使用INSERT INTO,然后再使用SELECT?

php - Codeigniter - 收到 404 Not Found 错误

php - MySQL 使用 codeigniter 插入库内

php - 在CodeIgniter 4中保存到数据库后如何获取插入ID

php - 在 .html 扩展名中解析 php 是否有任何问题

php - 避免在 cakephp 中与两个不同域进行 session 匹配?

php - 在添加进一步的限制语句 CodeIgniter 之前计算行数?

通过 Codeigniter 4 连接 Oracle 数据库