1

If I do an API call like this:

curl --location --request GET "http://127.0.0.1:8000/api/index.php/v1/users" --header "X-Joomla-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

I get the desired result.

Now I activate one system plugin of mine and I get an 500, because the system plugin is doing some things that are only needed if processing as client or admin request.

Is there a way of identifying the request as an API request, so decisions can be made? This should also work in future Joomla updates?

2 Answers 2

1

The most common way is to use Joomla\CMS\Application\CMSApplicationInterface::isClient() to identify current application by name.

use Joomla\CMS\Plugin\CMSPlugin;

class PlgSystemExample extends CMSPlugin
{
    protected $app;

    public function onAfterSomething
    { 
        if ($this->app->isClient('site'))
        {
            // Do something in site application.
        }
        
        if ($this->app->isClient('administrator'))
        {
            // Do something in administrator application.
        }
        
        if ($this->app->isClient('api'))
        {
            // Do something in API application.
        }
    }
}
0

Simple Solution:

$app->isClient('api');
1
  • The system has auto-flagged this answer as low-quality due to its length. When posting answers, please be more generous about your recommended resolution. For instance, if the solution is self-describing and you've sufficiently demonstrated how to implement the technique, perhaps offer link(s) to the Joomla documentation or to other posts on Joomla Stack Exchange or Stack Overflow that are related. The more relevant content that is on a given page, the greater the probability that readers will learn something; also, more text is better for SEO too.
    – mickmackusa
    Sep 22 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.