CodeIgniter Forums
Illegal Offset in Route.php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Illegal Offset in Route.php (/thread-72302.html)



Illegal Offset in Route.php - purpleprawn - 12-01-2018

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.


RE: Illegal Offset in Route.php - Perhood - 12-03-2018

Which version of Codeigniter are you using?
3 or 4?


RE: Illegal Offset in Route.php - purpleprawn - 12-03-2018

Using V3 Smile


RE: Illegal Offset in Route.php - InsiteFX - 12-04-2018

So you have a login route that's going to two different places.

Remark out the first route and see what happens.


RE: Illegal Offset in Route.php - dave friend - 12-04-2018

(12-01-2018, 08: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.


RE: Illegal Offset in Route.php - purpleprawn - 02-11-2019

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