Reputation: 39
I have a MessageController and I want users to be able to visit the messages/add/ action, passing an id as a variable ie messages/add/5 but for this id not to be visible in the url.
From what I read I believe the following should work:
Router::connect('/messages/add/*', array('controller' => 'messages', 'action' => 'add'));
My understanding is that anywhere that there's a link to messages/add/5 it would appear as a link to messages/add/ and when the user visits messages/add/5 it should show in the address bar as messages/add/. This doesn't work for me.
Do I not understand properly or am I doing something wrong?
Thanks!
Upvotes: 0
Views: 147
Reputation: 4522
That's not how routes work. If you do
Router::connect('/messages/add/*', array('controller' => 'messages', 'action' => 'add'));
that means that any url with this pattern /messages/add/* will be redirected to your add action in MessagesController (without the variable).
If you want to pass the id variable without showing it in the address bar, your option is to pass it through POST. Or, you could use ajax to call that url, but anyone with a browser console could look that url and access it too (if you don't have the proper security).
The point of routes.php
is to match any url in the address bar to the set of patterns in the first parameter of the connect array and direct it to the designated action. In no case does the Router changes the address bar setting routes.
[EDIT]: other option is to pass a slug or hash instead of the plain id. It all depends on what do you want to do and how you rather do it. If you only want the id not to be so blatantly obvious when reading a message, then hash it, or encrypt it. If the add action is to add a message (that's what I think it is), then you shouldn't be passing form parameters as GET variables in the url in the first place.
Upvotes: 1