This is an outdated version of PHPasap. Please visit http://phpasap.com/docs for the latest version.

Requests

The Request_Handler class provides methods that help fetch details of the current request. This class methods can be accessed via the Request alias loader.

Get $_GET data

Instead of using a $_GET["some_param"] you are better off with using

$some_val = Request::get("some_param");

get() method returns a boolean false if the request param doesn't exists in $_GET array.

Get $_POST data

$some_val = Request::post("some_param");

Just like get(), post() method returns a boolean false if the request param doesn't exists in $_POST array.

Check if current request is ajax

You can if the current request is an ajax request using the below code

$is_ajax = Request::ajax(); //returns true if ajax else returns false

The ajax() method returns a boolean true if current request is ajax request else it returns a boolean false.

Redirect to

You may want to redirect the current request to a different page. Typical use case is when someone fills a form and after successful validation you need to redirect him to a thank you page

Below is the code you will use in your controller.

return Request::redirect_to("thank-you");

Kindly take a note of below things

  • The first parameter provided is a relative url. PHPasap will append the base url to this before redirecting.
  • You have to return whatever is received from redirect_to(). It returns an Request_Handler object.

Redirect with data

Sometimes you may want to redirect to page with some data. Like say somebody, who is not logged in, tries to access an admin page. Then you may want to redirect him to login page with a message that says "You need to login to view this page".

Below is the code you will use in your controller.

return Request::redirect_to("login")->with(["error_msg"=>"You need to login to view this page", "flash_var_2"=>"val_2"]);

Then on login page, you may check if any flash data is set, if set then echo it.

if( Session::get("error_msg") ) {
    echo Session::get("error_msg");
}

//Similarly Session::get("flash_var_2")

The with() method of Request_Handler class flashes the data provided in first param.

Redirect with input data

What if user submits a form, fails validation and now you want to redirect the user back to the form page with all the data that he entered. Below is the code for the same

return Request::redirect_to("thank-you")->with_inputs();

The above is same as

return Request::redirect_to("thank-you")->with($_POST);
//OR
return Request::redirect_to("thank-you")->with($_GET);