Changing the dynamic value of a div, navigation bar on a tack blade

I am currently using the laravel blade, and currently I am using bootstrap along with the blade server template.

It is desirable that the navigation button can dynamically interact with the selected pages.

<div class="navbar">
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                  <a class="navbar-brand" href="#"><b>iMakan</b></a>
           </div>
    <div>
  <ul class="nav navbar-nav navbar-right">
    <li><a class="active "href="/">Home</a></li>
    <li><a href="about">About Us</a></li>
    <li><a href="contact">Contact Us</a></li>
    <li><a href="auth/login">Login</a></li>
    <li><a href="503">Cart</a></li>
  </ul>
</div>
</div>
</nav>
</div>
+4
source share
3 answers

Laravel provides a built-in feature that you can use: Request::is().

From the API docs :

Determine if the current request URI matches the pattern.

You use like this:

Request::is('about'); // returns a boolean

<a href="about" @if(Request::is('about')) class="active" @endif>

You can write a helper function to take care of this:

function isActive($path, $class = 'active')
{
    return (Request::is($path)) ? $class : '';
}

helpers.php app composer.json :

"autoload": {
    "files": [
        "app/helpers.php"
    ]
},

, composer dump-autoload .


, , :

<a href="about" class="{{ isActive('about') }}">About</a>
+5

,

URL-, home , aboutus About Us,

1:

Uri

$_SERVER['REQUEST_URI']

2:

class

<?php if ($_SERVER['REQUEST_URI']=='/') { echo 'active'; } ?>

,

<li><a href="about" class='<?php if ($_SERVER['REQUEST_URI']=='/') { echo 'active'; } ?>' >About Us</a></li>

active uri

, -

<ul class="nav navbar-nav navbar-right">
    <li><a class="<?php if ($_SERVER['REQUEST_URI']=='/') { echo 'active'; } ?>" href="/">Home</a></li>
    <li><a class='<?php if ($_SERVER['REQUEST_URI']=='/aboutus') { echo 'active'; } ?>'href="about" >About Us</a></li>
    <li><a class='<?php if ($_SERVER['REQUEST_URI']=='/contactus') { echo 'active'; } ?>'href="contact">Contact Us</a></li>
    <li><a class='<?php if ($_SERVER['REQUEST_URI']=='/login') { echo 'active'; } ?>' href="auth/login">Login</a></li>
    <li><a class='<?php if ($_SERVER['REQUEST_URI']=='/cart') { echo 'active'; } ?>' href="503">Cart</a></li>
  </ul>

class='active' URL.

, .

0

, , laravel, :

<a href="{{ URL::route('home') }}" 
   class="{{ Route::current()->getName() === 'home' ? : 'active' : ''">Home</a>

Usage URL::route(...)will create a URL based on the route name, and you can check to see if that name is currently used in collateral validation. To name a route, read the laravel routing documentation.

0
source

Source: https://habr.com/ru/post/1614839/


All Articles