Kankuro
Kankuro

Reputation: 305

How to check if Request_URI is nested

I'm trying to set the title for my pages to be dynamic by using Blade if statements. Currently I have it working properly for urls such as "domain.com/home" and using an else to handle the root "domain.com/".

The question I have is how to check if the url will be nested such as "domain.com/profile/settings". I'm wanting pages like this to be displayed in various ways, such as the above example displaying like "Settings | Profile" as the title.

Here is the code I have currently.

<title>
    @if($_SERVER["REQUEST_URI"] != "/")
        {{ ucfirst(preg_replace('{/}', '', $_SERVER["REQUEST_URI"])) }}
    @else
        Home
    @endif
</title>

Upvotes: 0

Views: 135

Answers (1)

Mike Foxtech
Mike Foxtech

Reputation: 1651

You need to parse the string $_SERVER["REQUEST_URI"]. First you need to split into an array

<title>
    @if($_SERVER["REQUEST_URI"] != "/")
        <?php $dataTitles = [];
              $dataTitles = explode ("/", $_SERVER["REQUEST_URI"]);
              if (count($dataTitles) > 1) {
                   unset($dataTitles[0]);
              } 

              echo implode('|', $dataTitles);
         ?>
    @else
        Home
    @endif
</title>

Upvotes: 1

Related Questions