Skip to content

Latest commit

 

History

History
261 lines (173 loc) · 4.65 KB

requests.md

File metadata and controls

261 lines (173 loc) · 4.65 KB

Title: Requests Description: Requests are objects that represent the HTTP requests.


What Is A Request?

The Request object has several methods for working with the HTTP request.

! Note: WordPress automatically adds slashes to $_POST, $_GET, and other super globals. A Request object reflects unmodified super globals.

Make Request

You can make a \TypeRocket\Http\Request object 3 ways:

$request = new \TypeRocket\Http\Request();
$request = tr_request();
$request = \TypeRocket\Http\Request::new();

Get Fields

When TypeRocket fields are submitted a Request can access them using the getFields() method.

To get all the fields to call getFields() without an argument.

$fields = $request->getFields();

To get a specific field call getFields() with the name of the field as the argument.

$field = $request->getFields('field_name');

You can also do the same using the shorthand method:

$fields =  $request->fields();
$field = $request->fields('field_name');

Get Form Prefix

If you are using echo $form->open() you will be able to access the form prefix used by the form from the request.

$prefix = $request->getFormPrefix();

Get Path

Get the request path.

$path = $request->getPath();

Or, exploded path.

$path = $request->getPathExploded();

Get URI

Get the URI.

$uri = $request->getUri();

Get Full URI

$request->getUriFull();

Modified Full URI

Get the URL and modify the request query params.

$request->getModifiedUri(['page' => 1]);

Get Host

Get the Host.

$host = $request->gethost();

Get Protocol

Get the protocol.

$host = $request->getProtocol();

Get Form Method

Because forms can not submit anything other than GET and POST requests TypeRocket spoofs DELETE and PUT. To get the spoofed method use the getFormMethod() method.

$method = $request->getFormMethod();

Get Referer

$request->getReferer();

Get Current User

$request->getCurrentUser();

Is Method

$request->isGet();
$request->isPost();
$request->isPut();
$request->isDelete();

Is AJAX

$request->isAjax();

Or, you can see if the request is a TypeRocket AJAX request.

$request->isMarkedAjax();

Get Header

$request->getHeader('ACCEPT');

Get Accepts

$rquest->getAccepts();
// Returns and array

Accept Contains & Wants

If you need to search the content type, a request accepts in return.

$rquest->acceptContains('application/json');

Or, you can use the shorthand wants(). This method accepts the following options:

  • json
  • html
  • xml
  • plain
  • any
  • image
$rquest->wants('json');

GET & POST Data

$all = $rquest->getDataPost();
$single = $rquest->getDataPost('page');

$all = $rquest->getDataGet();
$single = $rquest->getDataGet('page');

Get Input

If you are working with a request that returns a JSON response body use the input() or getInput() method.

// returns an array of decoded JSON
$body = $request->input();

If no JSON body is found the $_POST data will get returned instead:

// When no JSON is sent then 
// $_POST is retrieved 
$post = $request->input();

If no post data is found any $_GET data will be returned instead.

// Only $_GET is set
$get = $request->input();

GET JSON

The getDataJson() works the same way as input() but does not look for $_GET data.

$body = $request->getDataJson();

Get Files

$rquest->getDataFiles();

Get Cookies

$all = $request->getDataCookies();
$single = $rquest->getDataCookies('my_cookie');

Check Nonce

The checkNonce() method is used by the \App\Http\Middleware\VerifyNonce middleware used by TypeRocket for all Kernel requests using the middleware class. It returns true if the check passes.

$request->checkNonce();

Axios

If you are using axios you can set a common header for the WordPress nonce TypeRocket uses.

axios.defaults.headers.common['X-WP-NONCE'] = window.trHelpers.nonce;

! Note: window.trHelpers.nonce is only available when front-end mode is enabled.

Check Honeypot

It returns true if the check passes. The check looks for any fields within the request group $_REQUEST['__hny'].

$request->checkHoneypot();

This method is used by the \TypeRocket\Http\Middleware\CheckSpamHoneypot middleware class. Also, you can add the honeypot fields to any HTML <form> by using the static method \TypeRocket\Elements\BaseForm::honeypotInputs().