Is there some way to achieve simple API commands natively with Expression Engine 6?
There is the following module:
https://expressionengine.com/add-ons/reinos-webservice
But I don’t actually need a full API web service for multiple users. Just my own use.
One idea is to create an add-on and then execute it with the new CLI. My requirements are actually very simple.
Is there any tutorial, or should I just get the posted add-on?
If possible, I would always prefer native EE functionality over third-party code that requires extra work to maintain and support.
While are tutorials on the CLI there are not really any about making such simple plugins or extensions.
That is what I was asking, some tutorial might be helpful for a basic add-on or code examples.
While the documentation is detailed about every section, it does not really explain on how to tight all the things together from start to the end output.
One more idea for tutorials on the EE university.
By tutorials I mean, for example, you have the following example:
https://docs.expressionengine.com/latest/development/models/channel-entry.html
$entry = ee('Model')->make('ChannelEntry');
$entry->author_id = ee()->session->userdata('member_id'); // Returns currently Logged-in user ID.
$entry->channel_id = 1,
$entry->title = 'An Awesome Title';
$entry->url_title = ee('Format')->make('Text', 'An Awesome Title')->urlSlug()->compile(); // Returns an-awesome-title. Must be unique.
$entry->status = ee('Model')->get('Status', 1)->first()->status; // Returns 'open';
$entry->entry_date = ee()->localize->now; // Returns time in seconds: 1623945317
// Validate and Save.
$result = $entry->validate();
if ($result->isValid())
{
$entry->save();
}
$entry->entry_id; // Will now return the new Entry ID.
This is supposed to create a simple entry ID. But where is the rest of the code?
Assuming I’m not creating an add-on but just want to use this directly inside PHP in a EE template for testing purposes.
@vw000 I have an add-on I am testing that does this very thing. I can share a copy if you’d be willing to test!
That is straight from the documentation. I guess there is a typo in the code.
I don’t get any errors now, but putting that inside PHP in a template does not seem to do anything. Nothing is posted as an entry.
I will try to see what is missing. Is there something similar for comments?
The same as above, posted here inside PHP opening and closing tags. Nothing else.
I guess my body field is missing for the entry. My channel body field is called “message_body”
I suspect nothing is being posted for that reason, since it’s missing. Any examples on how this would work on a proper channel with the title and one body field?
The example would probably be better if it displayed the errors on posting.
I think the problem with doing this in the template is that it doesn’t parse the actual template, nor just return the data.
If you are trying to read an entry, I think there are two ways to go about this.
You could create an addon that gets the actual entry from a template parameter, and then parse and return as JSON.
So your template might be:
{exp:my_addon entry_id="{segment_2}"}
Which would then run your code above:
$entry = ee('Model')->get('ChannelEntry')
->filter('entry_id', ee()->TMPL->fetch_param('entry_id'))
->first();
// Do any manipulations here
// Then you'll need to manually assign an object to send back. EE ChannelEntry models
// Aren't JSONSerializable by default.
$output = [];
foreach ($entry as $key => $value) {
$output[$key] = $value;
}
return ee()->output->send_ajax_response($output);
What may be easier is just to utilize EE’s native functionality and create a JSON template.
{
"data": "{exp:channel:entries entry_id="{segment_2}"}{
"title": "{title}",
"entry_id": "{entry_id}"
...
}{/exp:channel:entries}
}
Creating an entry would be about the same, but I would definitely go the addon route for that:
public function createEntry()
{
$entry = ee('Model')->make('ChannelEntry');
$entry->author_id = ee()->session->userdata('member_id'); // Returns currently Logged-in user ID.
$entry->channel_id = ee()->input->get_post('channel'), // This will get your data and clean it, for security purposes
$entry->title = ee()->input->get_post('title');
...
// Validate and Save.
$result = $entry->validate();
if ($result->isValid())
{
$entry->save();
}
$output = [
'entry_id' => $entry->entry_id,
];
return ee()->output->send_ajax_response($output);
}
This will need some work for your specific needs, but should get you close.
Thanks for sharing @jaygreentree
Since I posted this, I actually purchased the Reinos Add-on:
https://expressionengine.com/add-ons/reinos-webservice
I only did some testing, but it works, not using it yet.
And there is now also Bones which does also JSON output:
https://expressionengine.com/add-ons/bones
I was going to just call the data with a PHP code and output in JSON, similar to the plugin you posted, but for the price of those add-ons I guess it’s just cheaper to purchase them and have a solution that works out of the box. My problem was a bit more complex since I need to create or edit channels (POST data) to EE.
If you just need a simple JSON for Ajax calls, that plugin would do the job.
If you need something more fancy, Bones would do it, but from checking the docs it’s mostly to get data out of EE or just read data.
In case you need to get data into Expression Engine which is hard because you can’t just do a POST request with CURL or similar without having to disable the XSS security check (which is not advisable) then the Reinos add-on seems to be the only option it seems.
It’s a bit more complex, but it basically runs a full REST API service for your installation. Some modules like comments and others are not updated to EE 6, but the developer is very responsive to fix things when you point them out. A bit more expensive than other add-ons, but seems to work nice so far in my dev installation.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.