JURI::root() getting wrong path Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
neojw1505
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Wed Aug 11, 2021 12:44 pm

JURI::root() getting wrong path

Post by neojw1505 » Wed Sep 22, 2021 6:53 am

I have a link to my website "www.example/dev/index.php". But when I use JURI:root() . '/index.php' the link that I get back is "www.example/index.php" instead of "www.example/dev/index.php". The website is currently under development and it will break once the website is migrated and no longer contain "/dev" in the URL.

I would like to know how I can get the value for anything before the "/index.php" . Can any kind souls help me out here ?
Last edited by imanickam on Tue Sep 28, 2021 2:50 pm, edited 1 time in total.
Reason: Moved topic » from General Questions/New to Joomla! 4.x to Joomla! 4.x Coding

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2897
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: JURI::root() getting wrong path

Post by SharkyKZ » Wed Sep 22, 2021 7:15 am

Do you have live_site set in configuration.php? Any extensions installed? Is website working properly, are stylesheets loading properly?

neojw1505
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Wed Aug 11, 2021 12:44 pm

Re: JURI::root() getting wrong path

Post by neojw1505 » Wed Sep 22, 2021 7:48 am

SharkyKZ wrote:
Wed Sep 22, 2021 7:15 am
Do you have live_site set in configuration.php? Any extensions installed? Is website working properly, are stylesheets loading properly?
No I don't have live_site set in configuration.php, it's value is empty. Everything about the website is working fine. Just that when the user make a booking I would sent them an email with a link to cancel their booking such as 'https://example.com/dev/index.php?optio ... ancelorder' but the problem is I am getting 'https://example.com/index.php?option=ta ... ancelorder' without the /dev. I would like to know how can I get the path before the /index.php

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2654
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: JURI::root() getting wrong path

Post by ceford » Wed Sep 22, 2021 8:32 am

If you use URI::root(true); you get the path to your site without the domain - but you have to append '/' yourself. With URI::root(); you get the domain and path. I have different paths on localhost, a development site and a production site and I have never seen your problem. I doubt whether using JURI rather than URI makes a difference - but check.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2897
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: JURI::root() getting wrong path

Post by SharkyKZ » Wed Sep 22, 2021 8:41 am

I can't reproduce the issue either, JUri::root() should include the path.How about those extensions? Maybe a rogue JUri::root(false, '') call is made somewhere, setting an empty path. Although then things like stylesheets would be broken unless call was made very late.

Can you check if JRoute::link() is working? Like so:

Code: Select all

JRoute::link('site', 'index.php?option=com_foo&task=cancelorder', false, JRoute::TLS_IGNORE, true);

neojw1505
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Wed Aug 11, 2021 12:44 pm

Re: JURI::root() getting wrong path

Post by neojw1505 » Wed Sep 22, 2021 8:57 am

ceford wrote:
Wed Sep 22, 2021 8:32 am
If you use URI::root(true); you get the path to your site without the domain - but you have to append '/' yourself. With URI::root(); you get the domain and path. I have different paths on localhost, a development site and a production site and I have never seen your problem. I doubt whether using JURI rather than URI makes a difference - but check.
Hi, I tried changing it to URI::root(true) but it gives the same result as JURI::root()

neojw1505
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Wed Aug 11, 2021 12:44 pm

Re: JURI::root() getting wrong path

Post by neojw1505 » Wed Sep 22, 2021 9:02 am

I apologize, if I was not clear. I have attached my code in code.txt. You can see that I am setting $root_link = URI::root(true) (previously was JURI::root()). and then setting the $cancellink = $root_link . 'index.php....'

The expected value I want for $cancellink should be 'www.example.com/dev/index.php...' but I am getting
'www.example.com/index.php...' instead.
You do not have the required permissions to view the files attached to this post.

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2654
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: JURI::root() getting wrong path

Post by ceford » Wed Sep 22, 2021 11:36 am

This part looks wrong: task=default_cancelorder

It should be task=controller.function - so task=default.cancelorder but default looks wrong too - unless that is the actual name of a controller.

To be honest the whole piece of code looks unfamiliar to me - suspicious even.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2897
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: JURI::root() getting wrong path

Post by SharkyKZ » Wed Sep 22, 2021 2:17 pm

if($root_link == "") condition doesn't pass under normal circumstances (when site is accessed over webserver and server is configured properly). What you can do is remove the conditional and always add the path. Or simplify the code like this:

Code: Select all

$cancellink = JUri::root() . "index.php?option=com_osservicesbooking&task=default_cancelorder&id=" . $orderId . "&ref=" . md5($orderId);

neojw1505
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Wed Aug 11, 2021 12:44 pm

Re: JURI::root() getting wrong path

Post by neojw1505 » Thu Sep 23, 2021 8:07 am

Thanks everyone for trying to help, you guys helped to narrow down the scope of error. After looking at my code line by line carefully. Turns out the str_replace() 1st parameter value was not even in the url and the second parameter will change it to and empty string. Because of this, the "/dev" disappears. After removing the str_replace() line of code, everything started to work perfectly.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2897
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: JURI::root() getting wrong path

Post by SharkyKZ » Thu Sep 23, 2021 8:12 am

There was no str_replace() in your posted code :eek:.

neojw1505
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Wed Aug 11, 2021 12:44 pm

Re: JURI::root() getting wrong path

Post by neojw1505 » Thu Sep 23, 2021 8:27 am

SharkyKZ wrote:
Thu Sep 23, 2021 8:12 am
There was no str_replace() in your posted code :eek:.
Sorry about that. I only sent part of the code that I thought was causing the problem. But what you said about removing the conditionals helped me in solving my problem. :)


Locked

Return to “Joomla! 4.x Coding”