Welcome Guest, Not a member yet? Register   Sign In
Is there a way of detecting POST data in URL requests before routing them?
#1

[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";
$route['retrieve/lostpassword']  = "ajax/retrieve/lostpassword";
$route['retrieve/resetpassword'] = "ajax/retrieve/resetpassword";

So I was thinking of something like this(I know it doesn't work but you get the idea):

Code:
if(isset($_POST))
{
  $route['everything'] = "ajax/".theroutehere;
}
#2

[eluser]Cristian Gilè[/eluser]
Quote:
Code:
if(isset($_POST))
{
  $route['everything'] = "ajax/".theroutehere;
}

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è
#3

[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) {
  $params = implode('/', $ajax_params);
  $route["$ajax_controller/$ajax_method"] = "ajax/$ajax_method/$params";
}

I've not tested it myself, but I suppose you could give it a shot...
#4

[eluser]sorenchr[/eluser]
[quote author="Cristian Gilè" date="1296018541"]
Quote:
Code:
if(isset($_POST))
{
  $route['everything'] = "ajax/".theroutehere;
}

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.
#5

[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) {
  $params = implode('/', $ajax_params);
  $route["$ajax_controller/$ajax_method"] = "ajax/$ajax_method/$params";
}

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

[eluser]Cristian Gilè[/eluser]
[quote author="sorenchr" date="1296059864"][quote author="Cristian Gilè" date="1296018541"]
Quote:
Code:
if(isset($_POST))
{
  $route['everything'] = "ajax/".theroutehere;
}

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è
#7

[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) {
  $params = implode('/', $ajax_params);
  $route["$ajax_controller/$ajax_method"] = "ajax/$ajax_method/$params";
}

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";
makes it possible to pass as many variables to method as you want (:any can contain /param but also /param/param/param). Now, if you wrap it in a if block, you might be able to prevent the use of hooks:
Code:
if(isset($_POST)) { // or maybe: if($this->input->post('post_name') !== false), but i'm not sure if input is loaded already
    $route['retrieve/(:any)'] = "ajax/retrieve/$1";
    $route['lost/(:any)'] = "ajax/lost/$1";
    // etc
}
That way the route only works when CI gets POST data.

Fabian
#8

[eluser]kirkaracha[/eluser]
In CodeIgniter 2 the Input Class has $this->input->is_ajax_request().
#9

[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');
..to application/config/constants.php

With that constant its easy to know if its an ajax request or not.
#10

[eluser]rogerwaldrup[/eluser]
I tried its working...




Theme © iAndrew 2016 - Forum software by © MyBB