Welcome Guest, Not a member yet? Register   Sign In
Route with variable gives 404
#1

Hi all,

I'm new to CodeIgniter and have a routing issue.
What I try to do is add a variable to the URL to pass it into the method arguments.

I used this example from the CI manual:

PHP Code:
$routes->add('product/(:num)''Catalog::productLookupByID/$1'); 

The actual code to match my needs is:

PHP Code:
$routes->get('/backend/profile/''backend/Profile::index');
$routes->get('/backend/profile/(:num)''backend/Profile::index/$1'); 


The method in the controller is:

PHP Code:
public function index($var null): void
{
    echo $var;

    //some more stuff


When I run the URL http://localhost:8080/backend/profile/1

I expect it to echo "1".
But instead a 404 is thrown [Controller or its method is not found: \App\Controllers\backend::index]

This controller does exist, because when I remove the 1 from the URL, the page loads, however, it is echoing NULL obviously...

What is this beginner missing here???
Reply
#2

You have to capitalize the backend, and check your slashes
Reply
#3

I now have copied the exact syntax including slashes, and capitailized backend as you said, but sadly it doesn't work


PHP Code:
$routes->get('backend/brands/(:any)''Backend/Brands::search/$1'); 


When I remove the /$1 at the end, the routing comes to the right page, but without the variable...
Reply
#4

Hello,

could you try :
PHP Code:
$routes->add('backend/brands/(:any)''Backend\Brands::search/$1'); 


In the documentation you have this example :


PHP Code:
$routes->add('product/(:num)''App\Catalog::productLookup'); 

it's not a / but a \ I think and they use add instead of get.
If I have to use get and post i use match instead of add. with as first parameter ['get','post'].
Reply
#5

PHP Code:
$routes->get('/backend/profile''Backend\Profile::index');
$routes->get('/backend/profile/(:num)''Backend\Profile::index/$1'); 
Reply
#6

(This post was last modified: 12-17-2020, 08:14 PM by John_Betong.)

@sjender,

Try the most complicated route first followed by a simpler route:

// instead of this
$routes->get('/backend/profile/', 'backend/Profile::index');
$routes->get('/backend/profile/(:num)', 'backend/Profile::index/$1');

// try this
$routes->get('/backend/profile/(:num)', 'backend/Profile::index/$1');
$routes->get('/backend/profile/', 'backend/Profile::index');
Reply
#7

(12-17-2020, 08:57 AM)paulbalandan Wrote:
PHP Code:
$routes->get('/backend/profile''Backend\Profile::index');
$routes->get('/backend/profile/(:num)''Backend\Profile::index/$1'); 

Paulbalandan is right. The second parameter must match your namespace and use backslashes.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB