0

I have problems with routing and I just can't figure out what is wrong.

When I need to take parameter from URI I just can't make my route works.

So this is what I have in route.php

$routes->add('admin', 'Admin/Login::index');
$routes->add('admin/login', 'Admin/Login::login');
$routes->add('admin/gUP', 'Admin/AdminGlavni::g_obrada');
$routes->add('admin/cam', 'Admin/AdminGlavni::cam_prikaz');
$routes->add('admin/cam/edit/(:any)', 'Admin/AdminGlavni::cam_edit_show/$1');

but this is not working (all other routes works as they should)

$routes->add('admin/cam/edit/(:any)', 'Admin/AdminGlavni::cam_edit_show/$1');

When I try to reach mydmain.com/admin/cam/edit/1 I get:

404 - File Not Found

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

and cam_edit_show in AdminGlavni Class is defined like this:

public function cam_edit_show($id) {
                ......

            }

What is wrong whit my route? Please help.

FOUND ANSWER:

Slash in handler was wrong. It supposed to be \ and not /

Snowman
  • 21
  • 4
  • Disable auto routing in `app\Config\Routes.php` I.e: `$routes->setAutoRoute(false);` – steven7mwesigwa Apr 08 '22 at 13:11
  • But then my other routs not working. Edit: Ok I see that I have to set routes to work, so it is not brakeing other routes but also not fixing my original problem – Snowman Apr 08 '22 at 13:58
  • Use `(:num)` instead of `(:any)` Also prefer to use actual relevant HTTP verb methods i.e `->get(...)` instead of `->add(...)`. – steven7mwesigwa Apr 08 '22 at 14:16
  • Tnx, these are all good sugestions but non of them are fixing my problem. Still getting error telling me that \App\Controllers\Admin::index is not found. And I don't get it where it gets Admin::index as I don't have this route nor controler – Snowman Apr 08 '22 at 15:23
  • 2
    Does this answer your question? [Codeigniter 4 - route now working / 404 / Controller or its method is not found](https://stackoverflow.com/questions/71088499/codeigniter-4-route-now-working-404-controller-or-its-method-is-not-found) – steven7mwesigwa Apr 08 '22 at 17:19

1 Answers1

0

I think you have a directory called admin and then your controller called AdminGlavni. I solved same issue by just change the **

I changed / to ** where route call controller with the directory.i think it should require namespace pattern

your code:

Admin/AdminGlavni::cam_edit_show/$1

Try with this:

Admin\AdminGlavni::cam_edit_show/$1

find the below as:

$routes->add('admin/cam/edit/(:any)', 'Admin\AdminGlavni::cam_edit_show/$1');
jps
  • 20,041
  • 15
  • 75
  • 79
  • The question was already flagged as a **duplicate**. In addition, your answer is **no** different from the [accepted answer in the other question](https://stackoverflow.com/a/71099352/7376590). You may consider reflagging it as a duplicate or voting to close it. – steven7mwesigwa Apr 12 '22 at 06:56