2

Here are a couple of my routes;

This one works:

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

This one doesn't work:

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

As you can see, it is calling the same method. The passed in value is a userid like 72. If active is 1 in the db it then sets it to 0 and vice verse, thus the name toggle_user_is_active($id).

If I put directly in URL as follows:

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

I get the following error:

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

The toggle in the view is as follows:

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

When clicked it produces:

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

Scratching my head! Any pointers appreciated.

spreaderman
  • 918
  • 2
  • 10
  • 39
  • This really is a long shot but I wonder does it help if you concentrate on the user ID to the end of the site_url() function and not within it? Like site_url('admin/users/toggle_user_is_active/').$user->id – J2112O Feb 12 '22 at 11:58
  • The url is correct though. I tried $routes->get('/admin/toggle_user_is_active', 'Admin/User/User_Controller::toggle_user_is_active/72'); and is works when hardcoded. Puzzling. – spreaderman Feb 13 '22 at 03:02
  • This is the solution. Notice the backslash. I cannot find where in the docs it says this. Any pointers helpful. $routes->add('/admin/toggle_user_is_active/(:any)', 'Admin\User\User_Controller::toggle_user_is_active/$1'); – spreaderman Feb 13 '22 at 03:35

2 Answers2

4

Namespaces are defined using backslashes (\), not forward slashes (/).

Instead of:
'Admin/User/User_Controller::toggle_user_is_active/$1'

Use this:
'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.

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
1

see my sample code code and edit your like me


<?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');

    });
});




paliz
  • 347
  • 2
  • 5