我有以下代码,它工作得很好。
我的setActive()函数:
function setActive($path, $nav = 'class=active')
{
return Request::fullUrl() === $path ? $nav : '';
}以及我怎么称呼它:
<li {{ setActive(route('home')) }}>
<a href="{{ route('mod.banned.index') }}">
Home
</a>
</li>因此,每当页面是主页时,它都会将其设置为活动的。
但是,如果我想让它工作,它对子页面就不起作用。例如,如果我有/articles/article-1、/articles/article-2,不管是什么文章,我都希望文章选项卡被标记为活动的。
我想做一些类似的事情:
setActive(route('articles.index') . '/*')
// 'articles.index' route = /articles/
// Should be active if -> /anything/after/the/slash/我希望这是有意义的。
我怎样才能最好地做到这一点呢?
谢谢。
发布于 2016-05-26 15:00:08
这对我很有效:
function setActive($path, $nav = 'class=active')
{
return preg_match("/".preg_quote($path, '/')."\S*/", Request::fullUrl()) ? $nav : '';
}发布于 2016-05-27 19:15:46
您可以使用https://github.com/letrunghieu/active,它是Larave4/5应用程序的助手类,用于根据当前路由获取活动类。
发布于 2016-08-04 19:26:57
我会做一些事情,比如将全局变量添加到视图中,并在任何地方使用它
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer('*', function ($view) {
$view->with('active_route', \Route::currentRouteName());
});
}
}在刀片视图中。这要简单得多。可能会对某人有所帮助;)
<ul>
<li class="{{ $active_route == "home" && "active" }}"> Home </li>
</ul>https://stackoverflow.com/questions/37450780
复制相似问题