CodeIgniter Forums

Full Version: Illegal Offset in Route.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am currently trying to make things clean and neat and using the same endpoints for different things such as GET to /login - shows login page but POST to /login starts the login process. Going by the docs you can do this: 

PHP Code:
$route['login'] = 'auth/login';
$route['login']['POST'] = 'auth/loginProcess'

BUT this then throws the following error:

Quote:A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'POST'
Filename: config/routes.php
Line Number: 57

I tried putting ['GET'] against the first line but didn't seem to make any difference. Any help much appreciated.
Which version of Codeigniter are you using?
3 or 4?
Using V3 Smile
So you have a login route that's going to two different places.

Remark out the first route and see what happens.
(12-01-2018, 07:16 PM)purpleprawn Wrote: [ -> ]I am currently trying to make things clean and neat and using the same endpoints for different things such as GET to /login - shows login page but POST to /login starts the login process. Going by the docs you can do this: 

PHP Code:
$route['login'] = 'auth/login';
$route['login']['POST'] = 'auth/loginProcess'

BUT this then throws the following error:

Quote:A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'POST'
Filename: config/routes.php
Line Number: 57

I tried putting ['GET'] against the first line but didn't seem to make any difference. Any help much appreciated.

I stepped through with a debugger and figured out the source of the error message. OK, these two lines are evaluated as when the file is "included".

PHP Code:
$route['login'] = 'auth/login';
$route['login']['POST'] = 'auth/loginProcess'

The first line sets a value at $route['login'] which is a string.
The second line tries to add an index to an array at $route['login'], but as just discussed, that value is a string. Bonk!

OK, reverse the lines, eg.

PHP Code:
$route['login']['POST'] = 'auth/loginProcess'
$route['login'] = 'auth/login'

No error because with this because the second line overwrites the array the previous line assigned at $route['login'] meaning $route['login']['POST'] entry is gone.

The next logical thing to try is using a verb for both routes.

PHP Code:
$route['login']['GET'] = 'auth/login';
$route['login']['POST'] = 'auth/loginProcess'

And this actually worked. BUT - I had to clear the browser cache to get the smarty-pants browser to do what it was told. I had originally set a test form's action to "auth/login", later to realize I really just wanted "login". But the browser kept going to the old "action" url. Had me going for a minute. Moral of the story - 1) use verbs on all associated routes and 2) clear the cache.

Hope this works for you.
Apologies for the delay!

What I think I was getting mixed up with is that if you start adding ['POST'], ['GET'] etc to the arrays then you have to carry on doing so. You can't mix and match (makes sense really!)

Thanks for the help Smile