1

If I create Route for the API via

$router->createCRUDRoutes( 'v1/foos', 'foos', ['component' => 'com_foos'], true );

Like explainded in https://joomla.stackexchange.com/a/32322/7387 the Route is

JOOMLA/api/v1/foos

I like a Route without the api like

JOOMLA/v1/foos

How do I archive this?

2 Answers 2

2

I think I have found a solution myself. I use a redirect in .htaccess

Redirect 301 /myvalue /api/v1/foos/index.php/myvalue

Is there another better way in Joomla?

1
  • I do think it should be possible with .htaccess to mask the /api without having to do a redirect, however I couldn't provide you with a working example. Search for examples of rewriteCond and rewriteRule for ways to add /api to your incoming URI/URL. I use these two lines to append a folder to a URL so what you need to do is insert /api to the URL before /v1. rewriteCond %{http_HOST} ^www\mydomain\.com\.au$ and then follow it up with rewriteRule (.*) /site2/$1 [L]
    – Irata
    Commented Aug 13, 2022 at 10:18
0

I don't believe you can change or remove the /api within Joomla as it is hard coded into the processing of the URI found in libraries/src/Uri/Uri.php, see code below at the last line.

        if (trim($live_site) != '')
        {
            $uri = static::getInstance($live_site);
            static::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
            static::$base['path'] = rtrim($uri->toString(array('path')), '/\\');

            if (\defined('JPATH_BASE') && \defined('JPATH_ADMINISTRATOR') && JPATH_BASE == JPATH_ADMINISTRATOR)
            {
                static::$base['path'] .= '/administrator';
            }

            if (\defined('JPATH_BASE') && \defined('JPATH_API') && JPATH_BASE == JPATH_API)
            {
                static::$base['path'] .= '/api';
            }
        }

I do think it should be possible with .htaccess to insert the /api into the URL for Joomla without having to do a redirect, however I don't have a working example I can provide you with.

Search for examples of rewriteCond and rewriteRule for ways to insert /api to your incoming URI/URL.

rewriteCond %{http_HOST} ^www\mydomain\.com\.au$
rewriteRule (.*) /site2/$1 [L]

I use these two lines to append a folder to a URL so what you need to do is insert /api to the URL before /v1.

Your condition statement would have to be more specific to your API's URL so as not to interfere with other API's in your instance, i.e. you don't want to end up creating yourdomian/api/api/otherapi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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