codezero/php-url-builder

Parse, manipulate and rebuild a URL.

1.0.0 2023-03-31 17:50 UTC

This package is auto-updated.

Last update: 2024-11-30 02:33:21 UTC


README

GitHub release License Build Status Code Coverage Code Quality Total Downloads

ko-fi

✅ Requirements

  • PHP >= 7.2

📦 Install

Install this package with Composer:

composer require codezero/php-url-builder

📘 Usage

You create a new UrlBuilder instance and pass it the URL you want to manipulate:

$urlBuilder = new \CodeZero\UrlBuilder\UrlBuilder('http://www.example.com/abc/def?foo=bar');
// or...
$urlBuilder = \CodeZero\UrlBuilder\UrlBuilder::make('http://www.example.com/abc/def?foo=bar');

When you are done, you can build the new URL:

$url = $urlBuilder->build(); //=> Returns 'http://www.example.com/abc/def?foo=bar'
$url = $urlBuilder->build(false); //=> Returns '/abc/def?foo=bar'

Updating URL Parts

There are setters and getters for the different URL parts:

$urlBuilder->setScheme('https');
$urlBuilder->getScheme(); //=> Returns 'https'

$urlBuilder->setHost('www.example.com');
$urlBuilder->getHost(); //=> Returns 'www.example.com'

$urlBuilder->setPort(8000);
$urlBuilder->getPort(); //=> Returns '8000'

$urlBuilder->setPath('/abc/def');
$urlBuilder->getPath(); //=> Returns '/abc/def'
$urlBuilder->getSlugs(); //=> Returns ['abc', 'def']

$urlBuilder->setSlugs(['abc', 'def']);
$urlBuilder->getPath(); //=> Returns '/abc/def'
$urlBuilder->getSlugs(); //=> Returns ['abc', 'def']

$urlBuilder->setQueryString('foo=bar');
$urlBuilder->getQueryString(); //=> Returns 'foo=bar'
$urlBuilder->getQuery(); //=> Returns ['foo' => 'bar']

$urlBuilder->setQuery(['foo' => 'bar']);
$urlBuilder->getQueryString(); //=> Returns 'foo=bar'
$urlBuilder->getQuery(); //=> Returns ['foo' => 'bar']

🚧 Testing

composer test

☕ Credits

🔒 Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

📑 Changelog

A complete list of all notable changes to this package can be found on the releases page.

📜 License

The MIT License (MIT). Please see License File for more information.