Download the PHP package diversified-design/viaphp without Composer
On this page you can find all versions of the php package diversified-design/viaphp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download diversified-design/viaphp
More information about diversified-design/viaphp
Files in diversified-design/viaphp
Package viaphp
Short Description PHP 8.3+ utility library for path aliases and arbitrary path joining with cross-platform canonicalization
License MIT
Informations about the package viaphp
ViaPHP
Via is a path alias management library.
It provides a static class for setting shorthand, dot-notation aliases to paths for easy access throughout your codebase, without hardcoding them all and passing around arrays or constants.
Given your project's full root path, and a hostname/IP, Via gives you access to system root, project root and URL root paths.
e.g.:
Paths are sanitized and canonicalized by Symfony's Filesystem component (Path::class), and dot-notation is enabled by
Installation
composer require diversified-design/viaphp
Usage
Setting & Assigning
Note: Internally we use / as the project-relative root path for constructing full filesystem and host paths. This package does not provide methods for handling arbitrary relative paths. All paths returned will always be absolute, based on the project root, local root or hosted root.
Local Path
The absolute local filesystem path of the project, used for absolute filesystem paths.
In real world usage, this would be populated dynamically, based on the system. I.e.: the local system's full path will be different on local development and various deployment systems.
So you would do something like:
You can retrieve the current local path with:
Host Domain
The domain/hostname/IP addreess, for absolute URL paths
Here too, you will want to set this dynamically somehow based on the context.
You can retrieve the current host with:
Set Base
Set Bases
Set mutliple bases at once with an array
(Calls self::setBase() internally)
Assign to Base
Assign a sub-path to a Base
Assign to Bases
Assign multiple sub-paths to Bases with an array
(Calls self::assignToBase() internally)
Init
Set and Assign a whole config of bases and assignments from a given array.
(Calls self::setLocal(), self::setHost(), self::setBases() and self:assignToBases() internally as needed)
Getting
Paths are accessed using dot-notation, and assembled at retrieval time (lazy loaded).
The accessing method is Via::get() but a Via::p() (read: "Via path") forwarder is provided for convenient shorthand notation. ( I use Via::p() myself. )
For even more convenience in templates and view files, a global via() function is also available:
Dynamic Path Appending
Both get(), p(), and the global via() function accept an optional second parameter to append additional path segments:
Global Functions
For ultimate convenience, especially in templates, four global functions are available:
These global functions are automatically available when you include the ViaPHP package via Composer's autoloader.
Path Joining Utility
ViaPHP includes a powerful path joining utility that works independently of the configured path system:
Via::j() Method
The Via::j() method provides arbitrary path joining with cross-platform canonicalization:
Global Function: via_join()
For maximum template convenience, use the global via_join() function:
Key Features:
- Independent Operation: Works without any Via configuration
- Cross-Platform: Handles various path separators (/, \, mixed)
- Path Canonicalization: Cleans up messy paths with
..,./,// - Null Safety: Gracefully handles null and empty additional paths
- Symfony Powered: Uses Symfony Path component for reliable canonicalization
This utility complements the configured path system by providing flexible arbitrary path joining for dynamic path construction scenarios.
Implemetation Notes
Under the hood we use
symfony/filesystem'sPath::classmethods for concatenating and cannonicalizing paths when assigning/setting and before returning them.dflydev/dot-access-dataDatamethods for setting an internal representation and parsing and resolvingVia::p()requests from it.
Testing
This package uses the Pest PHP testing framework.
Tests are located in ./tests