Title: How would you get any segment of the current url?
I’ve been using something like this:
$segments = explode('/', $page->uri());
$target = $segments[0] . '/' . $segments[1];
Any alternative solution you like to share?
Thanks
Title: How would you get any segment of the current url?
I’ve been using something like this:
$segments = explode('/', $page->uri());
$target = $segments[0] . '/' . $segments[1];
Any alternative solution you like to share?
Thanks
How do I get a value from the path array?
Have you inspected the result? Do you know how to get indices from an array?
This is an example result:
Array
(
[fragment] =>
[host] => kirby.test
[password] =>
[params] => Array
(
)
[path] => Array
(
[0] => notes
[1] => exploring-the-universe
)
[port] =>
[query] => Array
(
)
[scheme] => http
[slash] =>
[username] =>
)
Yes, I have looked at the result but I got an Object.
This seems to work for me:
($uri = new Kirby\Http\Uri($page->url()));
dump($uri->toArray()); // gives an Array like your example
dump($uri->path->data()[2]) // displays the third segment
When calling a method directly on the newly instantiated object (instead of on two lines like you are doing it now), you have to wrap the new Something()
in parentheses, see my code example above.
So
$uriArray = (new Kirby\Http\Uri($page->url()))->toArray();
Whereas here
the outer parentheses are superfluous.
Ok, thank you
Just to be complete and as a reference:
$uri = (new Kirby\Http\Uri($page->url()))->toArray();
dump($uri) // get an overview of the Array
echo $uri['path'][1] // output the second segment