Welcome Guest, Not a member yet? Register   Sign In
unexpected route problem
#1

(This post was last modified: 05-02-2023, 07:00 AM by anuragk.)

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
Reply
#2

Specify a class name with a namespace.
Reply
#3

(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']);
Reply
#4

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 
Reply
#5

Read https://www.php.net/manual/en/language.n...ionale.php
Namespaces was introduced in PHP 5.3.
Reply
#6

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB