Welcome Guest, Not a member yet? Register   Sign In
Routing by POST values
#1

[eluser]dop3[/eluser]
Hi all...

I'll be quick!

I know that the routing process will "choose" the right controller/action by the values that are in the query string/URI (URL). In fact, GET parameters...

But what if I want my app to be driven by POST parameters?

Should I write a new router/dispatcher?

I hope I was clear enough...

Thanks

Marcello
#2

[eluser]Jamie Rumbelow[/eluser]
CI's routes.php can be hacked quite extensively, so I think it's possible.

Something like this (not tested!):

Code:
//Convert all post values to "url segments"
$uri = "";
$post = $_POST;

foreach ($post as $seg)
{
$uri .= $seg."/";
}

$route[$uri] = "/class/method/id";

Try something like that...
#3

[eluser]Mirage[/eluser]
[quote author="dop3" date="1221942604"]Should I write a new router/dispatcher?[quote]
You could extend the core router...

-m
#4

[eluser]dop3[/eluser]
Perfect...
Now, let me say:

What I would like to do is this kind of controller's mapping:
Code:
Value passed via POST (or via GET too, should be the same)

"contr" => "controllerlName"
"task" => "controllerFunctionName"

"var" => "someValue"
...
"foo" => "otherValue"
With the convention that the request (no matter if POST or GET) has to have at least two params (pair key => value), one with key="contr" (that map the controller class) and the other with key="task" (that map the function to call on the controller)...

What would be the best way to do something like this with CI?

Thanks
#5

[eluser]Mirage[/eluser]
Best way would be to extend the Router.php core library with MY_Router, run your own evaluation from the constructor. If your own evaluation fails, call the parent constructor to proceed with default CI behavior.

Your evaluation should set the class, method and directory vars which will be fetched by CodeIgniter.php to process the dispatch.

HTH,
-m
#6

[eluser]dop3[/eluser]
thanks Mirage... I appreciate!

BTW, is it me or it's "strange" that a so discussed behaviour is not implemented "by default"... i didn't even find no posts talking about this! Is it so unusual???

Marcello
#7

[eluser]Mirage[/eluser]
I'd say it is not only unusual, but plain wrong. I consider POST/GET 'user input' and not part of the URI. The router handles mapping of the URI to controllers/methods. This shouldn't be influenced by user input. I'd think that routing based on user input can wreak havoc on application behavior.

Cheers -
-m
#8

[eluser]llbbl[/eluser]
umm silly question but maybe you are trying to do something really complicated because you don't understand how to work with POST variables.

This function will make things a lot easier. Smile


foreach($_POST AS $key => $value) {
${$key} = mysql_real_escape_string($value);
}


also if you are wanting to pass data between pages without post your better off using sessions. if its sensitive data, don't forget to encrypt!
#9

[eluser]dop3[/eluser]
Thanks Mirage and llbbl for your quick and clear reply...

Now, I can understand your (Mirage) point of view... and, in fact, it's plenty right! POST/GET is for user input and URI for representing a resource! Well, from my point of view...
why should any app access to a specific resource?
becouse, for example, the user clicked on a link, right?
And, in fact, what's a "click on a link"?
No way, it's a user input!!! Wink

I know... it's so philosophical and abstruse! But think that I come not from web development... so maybe there are some "best practices" that i simply don't know! So, In Mirage I trust!!!

And, llbbl, I'm not passing data between pages... I have a little javascript framework that handles just "ajax" calls and, even known that I could do this task using URI, I would to pass the params that specify the controller by POST... just becouse I think (I repeat, I think) it's more clear like this...
If you have another explanation that can tell me why I "must" use URI instead... please, let me know!!!!! Just becouse I'm curious and I want to know!!!

Thanks a lot

Marcello
#10

[eluser]xwero[/eluser]
A simple way to use post values to route to controllers is by using a controller as a router
Code:
class Dispatcher extends Controller (Router can't be used as that is the name of the CI class)
{
   function index()
   {
      // post based router
   }
}
Then you can use http://site.com/dispatcher to get to the controllers.

The only problem i see with using POST based urls is that surfers will get a message about resending the post data if they use their browser back button.




Theme © iAndrew 2016 - Forum software by © MyBB