Migrating code from joomla 3 to joomla 4 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.
Post Reply
Gibernet
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Jun 11, 2020 12:39 pm

Migrating code from joomla 3 to joomla 4

Post by Gibernet » Tue Apr 02, 2024 1:42 pm

I'm migrating a code from joomla 3 to joomla 4.

I used this php code to detect if the client is using a mobile device or not:

$appWeb = new JApplicationWeb;

if ($appWeb->client->mobile) {
echo "<BR>IS mobile<BR>";
}
else {
echo "<br>is NOT mobile<br>";
}

I have seen that the JApplicationWeb has changed, but I haven't found the new version of it.

Does anyone know how to make this call in joomla 4?

Thanks in advance.

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

Re: Migrating code from joomla 3 to joomla 4

Post by pe7er » Tue Apr 02, 2024 8:10 pm

Joomla 4 has a file /libraries/classmap.php with all old JClasses and the new names.
See https://github.com/joomla/joomla-cms/bl ... assmap.php

In Joomla 4 you can use:

Code: Select all

<?php 

use Joomla\CMS\Application\WebApplication;

$appWeb = new ApplicationWeb;
See also this topic: How to switch from the Legacy classes to the new ones?
viewtopic.php?t=982934#p3617914
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

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

Re: Migrating code from joomla 3 to joomla 4

Post by SharkyKZ » Wed Apr 03, 2024 5:17 am

You don't need to create a whole new application instance just to read client information. You can use Joomla\Application\Web\WebClient class directly. But in CMS context you can get it through current application instance:

Code: Select all

Joomla\CMS\Factory::getApplication()->client->mobile
There is also Joomla\CMS\Environment\Browser class:

Code: Select all

Joomla\CMS\Environment\Browser::getInstance()->isMobile()
Not sure which one is more reliable in terms of detection.

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

Re: Migrating code from joomla 3 to joomla 4

Post by pe7er » Wed Apr 03, 2024 6:33 am

Thanks for that Joomla\CMS\Environment\Browser::getInstance()->isMobile() tip!
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

Gibernet
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Jun 11, 2020 12:39 pm

Re: Migrating code from joomla 3 to joomla 4

Post by Gibernet » Wed Apr 03, 2024 8:37 am

Thanks for your help. It has been very useful, I really apreciate.


Post Reply

Return to “Joomla! 4.x Coding”