![]() |
Is there a way of detecting POST data in URL requests before routing them? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Is there a way of detecting POST data in URL requests before routing them? (/showthread.php?tid=37936) Pages:
1
2
|
Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-25-2011 [eluser]sorenchr[/eluser] I have several routes that only exist because they are 'responses' to ajax POST requests: Code: $route['join/register'] = "ajax/join/register"; So I was thinking of something like this(I know it doesn't work but you get the idea): Code: if(isset($_POST)) Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-25-2011 [eluser]Cristian Gilè[/eluser] Quote: It should work. You can use the ternary operator for more elegant code: Code: $route['everything'] = isset($_POST) ? "ajax/".theroutehere : 'controller/method'; but, what doesn't work? Please, could you post some code? Cristian Gilè Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-25-2011 [eluser]Fabdrol[/eluser] I think you could make this work with a hook, firing before the POST and GET data is sanitized. Maybe you could do something like this: - is $_POST populated? - set a (global) variable $ajax_data to true - set a (global) variable $ajax_method with the method name - set a (global) variable $ajax_params with the parameters then, in the routes.php file, you could set a dynamic route, something like this: Code: if($ajax_data) { I've not tested it myself, but I suppose you could give it a shot... Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-26-2011 [eluser]sorenchr[/eluser] [quote author="Cristian Gilè" date="1296018541"] Quote: It should work. You can use the ternary operator for more elegant code: Code: $route['everything'] = isset($_POST) ? "ajax/".theroutehere : 'controller/method'; but, what doesn't work? Please, could you post some code? Cristian Gilè[/quote] The example was for illustration purposes only, I am quite sure it doesn't work. Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-26-2011 [eluser]sorenchr[/eluser] [quote author="Fabdrol" date="1296023360"]I think you could make this work with a hook, firing before the POST and GET data is sanitized. Maybe you could do something like this: - is $_POST populated? - set a (global) variable $ajax_data to true - set a (global) variable $ajax_method with the method name - set a (global) variable $ajax_params with the parameters then, in the routes.php file, you could set a dynamic route, something like this: Code: if($ajax_data) { I've not tested it myself, but I suppose you could give it a shot...[/quote] I guess this isn't going to be an easy one. Thanks for the help though. Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-26-2011 [eluser]Cristian Gilè[/eluser] [quote author="sorenchr" date="1296059864"][quote author="Cristian Gilè" date="1296018541"] Quote: It should work. You can use the ternary operator for more elegant code: Code: $route['everything'] = isset($_POST) ? "ajax/".theroutehere : 'controller/method'; but, what doesn't work? Please, could you post some code? Cristian Gilè[/quote] The example was for illustration purposes only, I am quite sure it doesn't work.[/quote] I'm sure it works. Cristian Gilè Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-26-2011 [eluser]Fabdrol[/eluser] [quote author="sorenchr" date="1296059930"][quote author="Fabdrol" date="1296023360"]I think you could make this work with a hook, firing before the POST and GET data is sanitized. Maybe you could do something like this: - is $_POST populated? - set a (global) variable $ajax_data to true - set a (global) variable $ajax_method with the method name - set a (global) variable $ajax_params with the parameters then, in the routes.php file, you could set a dynamic route, something like this: Code: if($ajax_data) { I've not tested it myself, but I suppose you could give it a shot...[/quote] I guess this isn't going to be an easy one. Thanks for the help though.[/quote] Well, not that hard as it sounds, but maybe you can combine my answer with Cristian's. Using: Code: $route['folder/(:any)'] = "controller/method/$1"; Code: if(isset($_POST)) { // or maybe: if($this->input->post('post_name') !== false), but i'm not sure if input is loaded already Fabian Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-26-2011 [eluser]kirkaracha[/eluser] In CodeIgniter 2 the Input Class has $this->input->is_ajax_request(). Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-26-2011 [eluser]cahva[/eluser] BTW, $_POST is a superglogal and is always defined so using isset($_POST) wont work. Better to check if its an ajax request. If you use CI2, you can use what kirkaracha told you or simply put this: Code: define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); With that constant its easy to know if its an ajax request or not. Is there a way of detecting POST data in URL requests before routing them? - El Forum - 01-27-2011 [eluser]rogerwaldrup[/eluser] I tried its working... |