Joomla tip: Use the Joomla\Uri\Uri class to create a URL.
Jan 16, 2025 pm 08:17 PMWhen building URLs in code, you can use string concatenation to collect all strings:
$url = $domain.'/index.php?option='.$option.'&view='.$view.'¶m1='.$value1;
This approach is even convenient for short strings. However, it is not so convenient and intuitive if there are many parameters or need to be standardized/cleaned in the process. For example, part of the URL might contain a leading slash (the slash at the beginning of the URL fragment), and the incoming domain name of the request might also end in a slash, so we get a bad URL with a double slash somewhere in the middle ……
To standardize and unify URL retrieval tasks, Joomla provides the JoomlaUriUri class. In Joomla 1.6 and earlier, it was called JUri. This class handles URLs according to the RFC3986 standard and is responsible for parsing or assembling URLs from their various parts.
Example: Get specific parameters from URL
use Joomla\Uri\Uri; $url = 'https://web-tolk.ru/dev/biblioteki?param=value'; $uri = new Uri($url); // 此處輸出'value' echo $uri->getVar('param');
You might say, yes, there is a native PHP function parse_url
... but the Uri class ensures safe operations on UTF-8 characters in URLs, including Cyrillic domain names. To avoid writing various checks yourself, you can use Joomla core functionality.
How to build the required URL in Joomla code
It’s also very simple here:
use Joomla\Uri\Uri; $uri = new Uri; $uri->setHost('web-tolk.ru'); $uri->setScheme('https'); // setPath()以前導(dǎo)斜杠開(kāi)頭 $uri->setPath('/dev/biblioteki'); // GET參數(shù)可以作為數(shù)組傳遞 $vars = [ 'param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3', ]; $uri->setQuery($vars); // 將URL作為字符串輸出 echo $uri->toString();
The hierarchical structure of the Uri class in Joomla is designed so that the getter method is located in the AbstractUri class, and the setter method is located in the Uri class. You can view the setter method in the libraries/vendor/joomla/uri/src/Uri.php file. You can view the getter method in the libraries/vendor/joomla/uri/src/AbstractUri.php file.
If you use PHPStorm, it understands Joomla completely and will tell you everything you need.
You can refer to the old documentation page, which is still applicable to a large extent and has been adapted for the use of namespaces.
Uri structure:
<code> foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment</code>
Joomla Community Resources
- http://miracleart.cn/link/4e79e96ebb5671bdb50111f18f263003
- Joomla Community Chat on Mattermost
- WebTolk Joomla Extension
- This article in Russian
The above is the detailed content of Joomla tip: Use the Joomla\Uri\Uri class to create a URL.. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.
