简体   繁体   English

Laravel中用于Twitter Bootstrap导航的自动活动类

[英]Automatic Active Class For Twitter Bootstrap Navigation in Laravel

Like most people, I'm using Twitter Bootstrap for the site I'm currently developing in Laravel. 像大多数人一样,我正在使用Twitter Bootstrap来访问我目前正在Laravel中开发的网站。 So far I'm enjoying using Laravel as a PHP equivalent to Rails, but I'm wondering if there's a better way to do the navigation bars. 到目前为止,我很享受使用Laravel作为与Rails相当的PHP,但我想知道是否有更好的方法来做导航栏。 I'm attempting to make sure that my navbar li tags get the active class automatically, so I have this: 我正在尝试确保我的navbar li标签自动获得活动类,所以我有这个:

<ul class="nav nav-pills">
@if(URL::current() . "/" == URL::route('home'))
    <li class="active">
@else
    <li>
@endif
    <a href="{{ route('home') }}">Home</a>
</li>
@if(URL::current() == URL::route('about'))
    <li class="active">
@else
    <li>
@endif
    <a href="{{ route('about') }}">About</a>
</li>
</ul>

This'll be fine, for now. 现在这个没关系。 But when I've got menus, and more navigations it'll end up looking awful to debug and deal with. 但是当我有菜单和更多导航时,最终看起来很难调试和处理。

Is there anything akin to the Gem Tabulous for Laravel or a generally nicer way to deal with navigation in Laravel? 有什么类似于Laravel的Gem Tabulous或者在Laravel中处理导航的一般方法吗?

I use this: 我用这个:

<li class="{{Request::path() == 'home' ? 'active' : '';}}">
  <a href="/home"><i class="fa fa-home"></i>Home</a>
</li>

Take a look at the Bootstrapper package, it has lots of tools to help you with Twitter Bootstrap. 看一下Bootstrapper软件包,它有很多工具可以帮助你使用Twitter Bootstrap。 In your example, you could build the navigation like this: 在您的示例中,您可以像这样构建导航:

Navigation::pills(array(
    array(
        'label'  => 'Home',
        'url'    => URL::route('home'),
    ),
    array(
        'label'  => 'About',
        'url'    => URL::route('about'),
    )
));

There's also the shorthand method, less verbose, which I don't particularly like, but can be used: 还有简写方法,不那么详细,我不是特别喜欢,但可以使用:

Navigation::pills(
    Navigation::links(array(
        array('Home', URL::route('home')),
        array('About', URL::route('about')),
    ))
);

It's important to note that in both examples, Bootstrapper takes care of the active class automatically , comparing the current request URL against the one passed to the item. 值得注意的是,在两个示例中, Bootstrapper自动处理active ,将当前请求URL与传递给项目的URL进行比较。 If, however, you wish to specify a different condition for this to apply, simply pass an active value, or a third value in the array, for the shorter method. 但是,如果您希望为此应用指定不同的条件,则只需为较短的方法传递active值或数组中的第三个值。 Example: 例:

Navigation::pills(array(
    array(
        'label'  => 'Home',
        'url'    => URL::route('home'),
    ),
    array(
        'label'  => 'About',
        'url'    => URL::route('about'),
        'active' => true,
    )
));

or 要么

Navigation::pills(
    Navigation::links(array(
        array('Home', URL::route('home')),
        array('About', URL::route('about'), true),
    ))
);

I found a small snippet which helps with this issue. 我找到了一个帮助解决这个问题的小片段。 Hopefully this helps the next persons who search for this. 希望这有助于下一个搜索此问题的人。 http://mikekoro.com/blog/laravel-4-active-class-for-navigational-menu/ http://mikekoro.com/blog/laravel-4-active-class-for-navigational-menu/

I've created a simple helper for this task. 我为这个任务创建了一个简单的帮助器。 I saved it as /app/helpers/Menu.php . 我把它保存为/app/helpers/Menu.php Then in /app/start/global.php I added my helper file to the ClassLoader::addDirectories array like so: _app_path().'/helpers', (if the class is not loaded try running "composer dump-autoload" in the console, more info about adding helpers here ) 然后在/app/start/global.php中我将我的帮助文件添加到ClassLoader::addDirectories数组中,如下所示: _app_path().'/helpers', (如果未加载类,请尝试运行“composer dump-autoload”)控制台,有关添加助手的更多信息,请点击此处

class Menu {
 public static function item($route, $label='', $class='', $params=array()) {
    $class .= (Route::getCurrentRoute()->getAction()['as']==$route) ? ' active' : '';
    $tag = $class ? '<li class="'.trim($class).'">' : '<li>';
    return $tag.HTML::link(URL::route($route, $params),$label).'</li>'; 
  }
}

In your view. 在你看来。 just supply the route name and it will create a LI element with a link inside, the 'active' class is added to the LI element. 只提供路由名称,它将创建一个内部链接的LI元素,'active'类被添加到LI元素。 You can also add custom classes by using the third parameter. 您还可以使用第三个参数添加自定义类。

<ul class="menu">
  {{ Menu::item('index','Home') }}
  {{ Menu::item('pages.about','About Us') }}
  {{ Menu::item('pages.contact','Get In Touch','last') }}
  {{ Menu::item('profile-user','Profile','',array('username' => Auth::user()->username)) }}
</ul>

If you wouldn't mind that the solution is jQuery based, then you could try something like: 如果您不介意解决方案是基于jQuery的,那么您可以尝试以下方法:

$('#mainmenu li').removeClass('active');
$('#mainmenu li a[href="'+document.location.href+'"]').parents('li').addClass('active');

where mainmenu is the id of your navbar or any other element that contains the li items. 其中mainmenu是导航栏的id或包含li项的任何其他元素。

This is what I use: 这是我使用的:

<li class="{{ Request::is('/') ? 'active' : '' }}"><a href="/">Home</a></li>
<li class="{{ Request::is('about') ? 'active' : '' }}"><a href="/about">About</a></li>
<li class="{{ Request::is('contact') ? 'active' : '' }}"><a href="/contact">Contact</a></li>

For anyone reading this who's on Laravel 5.4: 对于读这篇关于Laravel 5.4的人来说:

<li class="{{Request::path() == 'home' ? 'active' : '';}}">

Needs to become (take out the ending ;): 需要变成(取出结局;):

<li class="{{Request::path() == 'home' ? 'active' : ''}}">

Another useful library could be here https://code.google.com/p/bootstrap-php/source/browse/#svn%2Ftrunk%2FNavbar Not complex, but very easy to use 另一个有用的库可以在这里https://code.google.com/p/bootstrap-php/source/browse/#svn%2Ftrunk%2FNavbar不复杂,但很容易使用

$oNavbar      = new Bootstrap_Navbar_Handler(array('class' => 'nav-pills'));
$oNavbar->addEntry('Home', array('href' => '/pageurlHome'));
$oNavbar->addEntry('About', array('href' => '/pageurlAbout'));

$oNavbar->setAutoActive(true);
echo $oNavbar->display();

This generates: 这会产生:

<div class="navbar">
  <div class="navbar-inner">
    <div class="container">
      <ul class="nav-pills nav">
        <li class="active"><a href="/pageurlHome" class="">Home</a></li>
        <li><a href="/pageurlAbout" class="">About</a></li>
      </ul>
    </div>
  </div>
</div>

by using 通过使用

$oNavbar->setAutoActive();

the class automatically activates the corresponding entry 该类自动激活相应的条目

Not a Laravel solution, but a simple client side javascript solution I wrote today: 不是Laravel解决方案,而是我今天写的一个简单的客户端JavaScript解决方案:

https://github.com/davidmars/bootstrapNavActive https://github.com/davidmars/bootstrapNavActive

<!--include our little script-->
<script src="https://cdn.rawgit.com/davidmars/bootstrapNavActive/master/BootstrapNavActive.min.js"></script>

<!--initialize the nav element-->
<script>
$( document ).ready(
function(){
    var $mySmartNav=$("#mySmartNav");
    new BootstrapNavActive($mySmartNav);
  }
);
</script>

Improvements on code are welcome ;) 欢迎改进代码;)

I used this working fine 我用这个工作正常

<li class="{{ Request::is('blogs*') ? 'active' : '' }}">
   <a href="{{ route('blogs.create') }}">Create</a>
</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM