Trongate MX Docs
Introduction
Core HTTP Operations
Swapping Content
Events & Responses
Dynamic Form Handing
UI Enhancements
Data Management
Form Handling
Advanced Features
Trongate MX Security
Reference

Identifying Trongate MX Requests

When working with Trongate MX, it's often useful to know whether a request originated from Trongate MX or from another source. Trongate's PHP framework includes a function called in its engine directory that helps identify these requests.

Note: The function resides in Trongate's engine directory and is part of the PHP framework. While Trongate MX automatically sets the required header, the detection happens on the server side using this PHP function.

How It Works

Trongate MX automatically adds a special header (Trongate-MX-Request: true) to all requests it makes. The function checks for the presence of this header.

Scenario Description Result
Trongate MX Request Request made using mx-get, mx-post, etc. returns true
Regular HTTP Request Standard form submission, AJAX, etc. returns false
Page Load Direct URL visit or page refresh returns false

The code examples below use if (from_trongate_mx() === true) for explicit boolean checks. However, the simpler syntax if (from_trongate_mx()) will also work. Both approaches are valid, and you can choose the one that best suits your coding style or project requirements.

Common Use Cases

1. Differentiating Response Formats

The code sample below demonstrates a method (i.e., a function within a class) that could serve as an API endpoint for an HTTP request. In the example, we're using to determined if the inbound HTTP request was submitted using Trongate MX.

2. Handling Form Submissions

This next example demonstrates a method handles form submissions differently depending on the context of the request. Specifically, it uses to determine if the form submission originated from a Trongate MX request or a traditional form submission.

In the code example above, if the request is found to have originated from Trongate MX:

  • A 'thank you' message is rendered.

If the request is found to have not originated from Trongate MX:

  • The end user will be redirected to a different URL.

In the example above, we haven't actually processed any form data! So, please do keep in mind the fact that some of these examples have been deliberately simplified. In a real-world application, you'd probably have a requirement to do something with submitted form data.

3. API Response Formatting

The final example shows how to adjust API responses based on the request type. If the request comes from Trongate MX, the method returns HTML for seamless frontend updates. For other requests, it outputs JSON, making the endpoint suitable for both Trongate MX usage as well as more traditional API use.

  • Use when you need different response formats for dynamic updates versus direct visits
  • Consider using it for form submissions where you want different success behaviors
  • Leverage it to provide appropriate error responses based on the request source
  • Remember that the function is part of Trongate's PHP framework, not Trongate MX itself

Summary

Being able to identify Trongate MX requests enables you to create endpoints that can serve both dynamic updates and traditional page loads effectively. The function provides a simple way to detect these requests and adjust your response accordingly, making it easier to build applications that work seamlessly with both Trongate MX and traditional HTTP requests.