We're excited to announce a significant enhancement to the Trongate framework's post() function. This update brings improved flexibility and modernizes our approach to handling POST data, all while maintaining our commitment to stability and backward compatibility.

What's New?

The post() function now seamlessly handles both traditional form-encoded data and JSON payloads. This means you can now use post() to retrieve data from API requests that send JSON, in addition to its existing capabilities with form submissions.

Key Benefits

  • Enhanced Flexibility: Work with both form data and JSON payloads using the same familiar function.
  • API-Ready: Easily handle data from modern, JSON-based API requests.
  • Simplified Development: No need to change your approach based on the incoming data type - post() handles it all.
  • Future-Proof: As web development trends towards more API-driven approaches, your Trongate applications are ready.
  • Maintained Security: Our existing sanitization and type conversion features now extend to JSON data as well.

Stability: Our Top Priority

We understand the importance of stability in your projects. That's why we've implemented this enhancement in a way that doesn't break existing code. If you're currently using post() with form-encoded data, your code will continue to work exactly as it did before.

How It Works

The enhanced post() function automatically detects whether the incoming request contains form-encoded data or a JSON payload. It then processes the data accordingly, allowing you to retrieve values using the same syntax you're already familiar with. Importantly, the optional second argument for sanitization continues to work for both form-encoded and JSON data.

// Works with both form data and JSON payloads
$user_name = post('user_name');               // Raw data
$user_age = post('user_age', true);           // Sanitized data

// Handling nested JSON data
$user_address = post('user_address');         // Raw nested data
$user_street = post('user_address.street', true);  // Sanitized nested data

// If JSON payload is:
// {
//   "user_name": "John Doe",
//   "user_age": "30",
//   "user_address": {
//     "street": "123 Main St",
//     "city": "Townsville"
//   }
// }

// $user_name will be "John Doe"
// $user_age will be 30 (integer)
// $user_address will be ['street' => '123 Main St', 'city' => 'Townsville']
// $user_street will be "123 Main St" (sanitized)

As demonstrated, the post() function maintains consistent behavior across data formats:

  • It retrieves values from both form-encoded and JSON data.
  • The optional second argument (true) triggers sanitization for both data types.
  • It handles nested structures in JSON, allowing you to access nested values using dot notation.
  • Type conversion (e.g., string to integer) is applied when appropriate, regardless of the data source.

This unified approach significantly simplifies data handling in your Trongate applications, providing a consistent interface regardless of how the data is sent to your endpoint.

What You Need To Do With Your Existing Code

Nothing. Trongate is the stability-first PHP framework. Your existing codebase will be fine.

Just know that, moving forward, all types of posted data can be easily handled with our new post() function. Hopefully, this will make your life a little bit easier!

Moving Forward

This update to the post() function is part of our ongoing commitment to keeping Trongate modern, flexible, and developer-friendly. We're excited to see how you'll use this enhanced functionality in your projects.

For full details on the updated post() function, including examples and best practices, please refer to our updated documentation.

As always, we welcome your feedback and are here to support you as you explore this new capability. Happy coding!