這是我的幾條路線;
這個有效:
$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');
如您所見,它正在呼叫相同的方法。傳入的值是一個類似 72 的用戶 ID。如果資料庫中的 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
撓我的頭!任何指標表示贊賞。
uj5u.com熱心網友回復:
命名空間使用反斜杠 ( \) 定義,而不是正斜杠 ( /)。
代替:
'Admin/User/User_Controller::toggle_user_is_active/$1'
用這個:
'Admin\User\User_Controller::toggle_user_is_active/$1'
名稱決議規則
合格名稱
這是一個帶有命名空間分隔符的識別符號,例如
Foo\Bar
設定自己的路由規則
控制器和方法應該以與使用靜態方法相同的方式列出,通過用雙冒號分隔完全命名空間的類及其方法,例如
Users::list.
uj5u.com熱心網友回復:
查看我的示例代碼代碼并像我一樣編輯你的
<?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/$1');
$routes->get('fast-food-list', 'Home::fastFood');
$routes->get('fast-food-comment', 'Home::fastFoodComment');
$routes->get('fast-food-show/(:num)', 'Home::fastFoodShow/$1');
$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');
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/431256.html
