url

The url service provider will let you traverse the URI Path.

Start with take a look at the router example bellow:

$routes->get("/{page:blog}/{slug:[^/]+}/{id:\d+}", ['Http\Controllers\Pages', "about"]);

The router pattern above expects 3 path arguments.

  1. page: expects string that matches "blog"

  2. slug: expects any string value

  3. id: expects any number

The Url provider will make it easy for you to extract the URI path value.

Get URI

The examples bellow will use the router above and the URI value: https://example.com/blog/my-post/10

Get full URL

echo $this->provider->url()->getUrl();

Result: https://example.com/blog/my-post/10

Get full Root URL

echo $this->provider->url()->getRoot();

Result: https://example.com

Get full URI Path

echo $this->provider->url()->getPath();

Result: /blog/my-post/10

Get current URI Path

The get method will return the current, e.g. the last URI path item.

echo $this->provider->url()->get();

Result: 10

Get URI path as array

echo $this->provider->url()->getVars();

Result: ["blog", "my-post", 10]

Get URI parts

echo $this->provider->url()->getParts();

Result: ["page" => ["blog"], "slug" => ["my-post"], "id" => [10]]

Travers the URI Path

You can Utilize the select method and combine it with one of the get method above in the method list.

Get current path

By utilizing the get method you can extract the current URI Path, the current path will count as the last value .

echo $this->provider->url()->select("slug")->get();
echo $this->provider->url()->select(["page", "id"])->getPath();
echo $this->provider->url()->getUrl(["page", "slug"]);
  • Result: my-post

  • Result: /blog/10

  • Result: https://example.com/blog/my-post

Get URL directories

Get full URL to public directory

echo $this->provider->url()->getPublic(string $path = "");

Result: https://example.com/public/

Get full URL to public directory

echo $this->provider->url()->getResource(string $path = "");

Result: https://example.com/resources/

Get full URL to public directory

echo $this->provider->url()->getJs("main.js", false);
echo $this->provider->url()->getJs("main.js", true);
  • Result: https://example.com/resources/js/main.js

  • Result: https://example.com/public/js/main.js

Get full URL to JavaScript directory

echo $this->provider->url()->getResource();

Result: https://example.com/resources/

Get full URL to CSS directory

echo $this->provider->url()->getCss("style.css");

Result: https://example.com/public/css/style.css

Get full URL to public directory

echo $this->provider->url()->getResource();

Result: https://example.com/resources/

Last updated