Sourcerer

Cannot use namespaces?

3 months 4 days ago #91477

Joris Lange's Avatar Joris Lange

Hi Peter,

It seems that I cannot use namespaces within Sourcerer? Like this:

use Joomla\CMS\Uri\Uri;
$uri = Uri::getInstance();
$url = $uri->toString();

When I try this, I get an error message:
syntax error, unexpected 'use' (T_USE)

For now I used the old method:
$url = JURI::current();

Kind regards, Frits

3 months 4 days ago #91478

Peter van Westen's Avatar Peter van Westen Admin

Try this instead:

$uri = \Joomla\CMS\Uri\Uri::getInstance();
$url = $uri->toString();

3 months 4 days ago #91479

Joris Lange's Avatar Joris Lange

Hi Peter,

This works, thanks!

I tried something similar for Factory (formerly JFactory), and I came to this solution:

$app = \Joomla\CMS\Factory::getApplication();
$title = $app->getMenu()->getActive()->title;

Frits

3 months 4 days ago #91481

Peter van Westen's Avatar Peter van Westen Admin

You actually don't need to create the $app object. Sourcerer will already do that for you.
These are ready to use:

$app / $mainframe
$document / $doc
$database / $db
$user
$Itemid

3 months 4 days ago #91495

Joris Lange's Avatar Joris Lange

Super!

Thx,

Frits