Welcome Guest, Not a member yet? Register   Sign In
Illegal Offset in Route.php
#1

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

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

Using V3 Smile
Reply
#4

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

Remark out the first route and see what happens.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 12-04-2018, 12:38 PM by dave friend.)

(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.
Reply
#6

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB