Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com_ajax Stringable interface #274

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions migrations/52-53/new-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
sidebar_position: 1
---

# New features

#### com_ajax support Stringable result

Allows to customise the response for com_ajax, with help of `Joomla\CMS\String\StringableInterface`.

PR: https://github.com/joomla/joomla-cms/pull/43530

Example of Module helper getAjax() method, with `&format=json` request.
**Before:**
```php
function getAjax() {
echo json_encode(['title' => 'Foo', 'text' => 'Bar']);
exit;
}
```
**After:**
```php
function getAjax() {
$result = new class() implements \Joomla\CMS\String\StringableInterface {
public $title = '';
public $text = '';

public function __toString(): string
{
return json_encode($this);
}
};

$result->title = 'Foo';
$result->text = 'Bar';

return $result;
}
```

Example of customised `Joomla\CMS\Response\JsonResponse` response:

```php
function getAjax() {
$data = $this->getData(); //... code to load your data
$result = new class($data, null, false, false) extends \Joomla\CMS\Response\JsonResponse implements \Joomla\CMS\String\StringableInterface {};

return $result;
}
```