Get non-sef URL in Joomla 4 Topic is solved

General questions relating to Joomla! 4.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
drjjdub
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Jun 28, 2022 2:19 pm

Get non-sef URL in Joomla 4

Post by drjjdub » Tue Jun 28, 2022 2:21 pm

Can you tell me how to echo the non-sef URL in Joomla 4. The code below and other answers on here work only in Joomla 3:

Code: Select all

<?php
$router = JSite::getRouter();//      get router
$query = $router->parse(JUri::getInstance()); // Get the real joomla query as an array - parse current joomla link
$url = 'index.php?'.JUri::getInstance()->buildQuery($query);
echo $url;
?>
Thank-you,

Jordan

drjjdub
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Jun 28, 2022 2:19 pm

Re: Get non-sef URL in Joomla 4

Post by drjjdub » Tue Jun 28, 2022 2:24 pm

Solved.

Change JSite to JApplicationSite

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 23791
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Get non-sef URL in Joomla 4

Post by pe7er » Wed Jun 29, 2022 8:42 am

Welcome to Joomla forum!

Good to see that you've found an answer yourself. Thanks for sharing!

Due to the modern PHP name spacing, most Joomla Class Names don't have a J prefix anymore.
The old J prefixes are now aliases set in /libraries/classmap.php
You can use that file to find the new Class Names.

Your code can be improved to:

Code: Select all

// Put the Name Spaces at the top of the page
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Uri\Uri;

// Put the following just where you need it
$router = SiteApplication::getRouter(); // Get router
$query  = $router->parse(Uri::getInstance()); // Get the real joomla query as an array - parse current joomla link
$url    = 'index.php?' . Uri::getInstance()->buildQuery($query);
echo $url;
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com


Post Reply

Return to “General Questions/New to Joomla! 4.x”