Download the PHP package ali-translator/url-template without Composer
On this page you can find all versions of the php package ali-translator/url-template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ali-translator/url-template
More information about ali-translator/url-template
Files in ali-translator/url-template
Package url-template
Short Description Helping on work with template url, example: "{country}.example.com/{language}/{city}"
License MIT
Informations about the package url-template
URL Template
Helps work with URLs using their "base" template.
For example, the base structure of your project URL is "gb.example.com/en/london/".
In this example, your template URL has the following parameters: "country," "language," and "city."
Let's create a template for this example: "{country}.example.com/{language}/{city}"
Installation
Code example:
Warning: Be careful with free-form regular expressions. For language parameters, '[a-z]{2}' is less safe than using an explicit list like '(en|de|ua)'.
Optional Default Values
You can set optional default values for parameters by providing a callable argument as the default value.
Your optional parameter must depend only on required arguments.
Example of use:
Parameter Decorators
Sometimes you need to apply decorations to your parameters in a URL.
For example, if you want the following path template to be "/{country}-{language}/" and decide to hide the default language.
In this case, without decorators, you would get the following compiled URL: "/country-/".
The excessive character "-" looks unappealing.
You can use decorators to solve this problem.
A decorator is a class that implements the ParameterDecoratorInterface
.
Example of use:
For the decorator to work correctly, use an array of requirements with available values, instead of a regular expression.
Validate ParsedTemplate Object
Additional Features
-
You can use templates where multiple parameters are placed in one "URL namespace," such as the host "{country}-{language}-{currency}.test.com" and the path "/{country}-{language}/".
- If you need to compile only the "host URL" or "path URL," you can use:
- If you need to skip only some default parameters in the URL, you can pass an array of parameter names to the $hideDefaultParametersFromUrl parameter of the UrlTemplateConfig class.
-
If you have an optional parameter that depends on another parameter, and this other parameter is in a different part of the URL (e.g., the optional parameter is in the "path URL part" and depends on a parameter in the "host URL part"), there may be an issue when processing a relative URL without the host.
To handle this scenario, pass a value to the function that determines the optional parameter: -
To create a new
UrlTemplateConfig
based on an existing one: - By default, the system allows the use of all subdomains for a given domain template.
For example, if we have a template{city}.test.com
, it will correctly handle the domainwww.lviv.test.com
.
However, if we need the system to restrict handling of subdomains, we can specify inUrlTemplateConfig
that subdomains should not be supported:
Tests
Included in the package is a docker-compose file, with an environment for testing.