CodeIgniter Forums
unexpected route problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: unexpected route problem (/showthread.php?tid=87549)



unexpected route problem - anuragk - 05-02-2023

if I use

Code:
$routes->post('login', 'Admin::do_login');

everything works as expected

but when I use

Code:
$routes->match(['get', 'post'], 'login', [Admin::class, 'do_login']);


I get an error

404 Controller or its method is not found: \Config\Admin::do_login

am I doing anything wrong?

The class name is Admin and the method name is do_login
The controller file name is Admin.php


RE: unexpected route problem - iRedds - 05-02-2023

Specify a class name with a namespace.


RE: unexpected route problem - anuragk - 05-02-2023

(05-02-2023, 07:09 AM)iRedds Wrote: Specify a class name with a namespace.

can you explain how and why? i am new to this

because on the other hand, this works fine

Code:
$routes->match(['get', 'post'], 'appointment', [Appointment::class, 'appointmentForm']);



RE: unexpected route problem - iRedds - 05-02-2023

What are you new to? In php?

In the first example, the handler is specified as a string. In this case, the class will be checked for the existence of a namespace, and if it is not found, the default namespace will be added.

In the second example, you specify the handler as a string, where you declare the class name directly. If you don't specify a namespace or don't import, the current namespace will be used.
Current namespace is Config.

PHP Code:
namespace Config;

use Namespace\
Example2

$routes->get('example1', [Example1::class, 'index']); // \Config\Example1
$routes->get('example2', [Example2::class, 'index']); // \Namespace\Example2
$routes->get('example3', ['\Namespace\Example3''index']); // \Namespace\Example3

$routes->get('example4''\Namespace\Example4::index'); //  \Namespace\Example4
$routes->get('example5''Namespace\Example5::index'); //  \App\Controllers\Namespace\Example5 



RE: unexpected route problem - kenjis - 05-02-2023

Read https://www.php.net/manual/en/language.namespaces.rationale.php
Namespaces was introduced in PHP 5.3.


RE: unexpected route problem - anuragk - 05-02-2023

(05-02-2023, 03:42 PM)iRedds Wrote: What are you new to? In php?

In the first example, the handler is specified as a string. In this case, the class will be checked for the existence of a namespace, and if it is not found, the default namespace will be added.

In the second example, you specify the handler as a string, where you declare the class name directly. If you don't specify a namespace or don't import, the current namespace will be used.
Current namespace is Config.

PHP Code:
namespace Config;

use Namespace\
Example2

$routes->get('example1', [Example1::class, 'index']); // \Config\Example1
$routes->get('example2', [Example2::class, 'index']); // \Namespace\Example2
$routes->get('example3', ['\Namespace\Example3''index']); // \Namespace\Example3

$routes->get('example4''\Namespace\Example4::index'); //  \Namespace\Example4
$routes->get('example5''Namespace\Example5::index'); //  \App\Controllers\Namespace\Example5 

Thank you for explaining in such a manner.
the problem is solved