Skip to main content

Develop custom action

In Fusio it is easily possible to develop a custom action in case you want to implement a specific logic. This page explains how to build a custom action.

Autoload

To build a custom action you first need to define autoloading in your composer.json file so that Fusio is able to load your class. Therefore you need to add the following config to the composer.json file:

{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}

More information about composer autoloading can be found at: https://getcomposer.org/doc/01-basic-usage.md#autoloading

Development

Now you can create a PHP file at src/Action/HelloWorld.php with the following content.

<?php

namespace App\Action;

use Fusio\Engine\ActionAbstract;
use Fusio\Engine\ContextInterface;
use Fusio\Engine\ParametersInterface;
use Fusio\Engine\RequestInterface;

class HelloWorld extends ActionAbstract
{
public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context): mixed
{
return $this->response->build(200, [], [
'hello' => 'world'
]);
}
}

This hello world action is completely functional and can be directly used in your API.

Integration

You can also add this class to the provider.php file under the action key:

<?php

return [
'action' => [
// ...
\App\Action\HelloWorld::class,
],
// ...
];

Then you can also select this class from the dropdown list of action classes in the backend. If you want to create an action which is reusable for other users, you can also create a Fusio adapter which allows other users to reuse your action. Please take a look at our website to see all available adapters.

API

Inside your action, you already have most tools available to complete many tasks. To see the complete action API please take a look at our PHP API. The following is an example which shows some interesting methods of the internal action API:

<?php

namespace App\Action;

use Fusio\Engine\ActionAbstract;
use Fusio\Engine\ContextInterface;
use Fusio\Engine\ParametersInterface;
use Fusio\Engine\RequestInterface;

class HelloWorld extends ActionAbstract
{
public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context): mixed
{
$myConnection = $this->connector->getConnection('My_Connection');

$this->dispatcher->dispatch('my_event', ['foo' => 'bar']);

$this->logger->info('A log message');

return $this->response->build(200, [], [
'hello' => 'world'
]);
}
}

Through the connector you can obtain a connection which is configured at Fusio. This means you can get a database connection or http client connection to work with a remote service. Which concrete instance the connector returns depends always on the configured connection. The following table provides a first overview:

NameReturnWebsiteClass
AMQPPhpAmqpLib\Connection\AMQPStreamConnectionhttps://github.com/php-amqplib/php-amqplibFusio\Adapter\Amqp\Connection\Amqp
BeanstalkPheanstalk\Pheanstalkhttps://github.com/pda/pheanstalkFusio\Adapter\Beanstalk\Connection\Beanstalk
ElasticsearchElasticsearch\Clienthttps://github.com/elastic/elasticsearch-phpFusio\Adapter\Elasticsearch\Connection\Elasticsearch
GraphQLFusio\Adapter\GraphQL\ClientInterfacehttps://github.com/apioo/fusio-adapter-graphql/Fusio\Adapter\GraphQL\Connection\GraphQL
HTTPGuzzleHttp\Clienthttp://docs.guzzlephp.org/en/latest/Fusio\Adapter\Http\Connection\Http
MemcacheMemcachehttps://www.php.net/manual/book.memcache.phpFusio\Adapter\Memcache\Connection\Memcache
MongoDBMongoDB\Databasehttps://github.com/mongodb/mongo-php-libraryFusio\Adapter\Mongodb\Connection\MongoDB
RedisPredis\Clienthttps://github.com/predis/predisFusio\Adapter\Redis\Connection\Redis
SMTPSymfony\Component\Mailer\Mailerhttps://symfony.com/doc/current/mailer.htmlFusio\Adapter\Smtp\Connection\Smtp
SOAPSoapClienthttps://www.php.net/manual/class.soapclient.phpFusio\Adapter\Soap\Connection\Soap
SqlDoctrine\DBAL\Connectionhttp://www.doctrine-project.org/projects/dbal.htmlFusio\Adapter\Sql\Connection\Sql

Dependency Injection

In case your action needs other external dependencies you can also simply declare the dependencies in the constructor. Fusio then tries to inject the correct service via autowiring. Please take a look at the dependency injection chapter to get more details how this works.