英文:
If I give route in links in Laravel all of them become active
问题
I have an issue. If I give my links routes, all of them become active.
<li class="{{ (strpos(Route::currentRouteName(), 'dashboard') == 0) ? 'active' : '' }}">
<a href="{{ route('dashboard') }}">
<i class="uil uil-dashboard me-2 d-inline-block"></i>Dashboard</a></li>
Can somebody explain why this is happening?
英文:
i have an issue. If i give to my links routes, all of them becomes active
<li class="{{ (strpos(Route::currentRouteName(), 'dashboard') == 0) ? 'active' : '' }}">
<a href="{{ route('dashboard') }}">
<i class="uil uil-dashboard me-2 d-inline-block"></i>Dashboard</a></li>
Can somebody explain me why is that happening?
答案1
得分: 1
strpos 在 Route::currentRouteName() 为 'dashboard' 时返回 int(0),否则返回 bool(false)。
你正在使用 == 运算符,这不是严格比较。尝试使用 === 代替。
false == 0 // true
false === 0 // false
英文:
strpos returns int(0) when Route::currentRouteName() is 'dashboard' and bool(false) otherwise.
You're using the == operator which is NOT strict comparison. Try using === instead.
false == 0 // true
false === 0 // false
答案2
得分: 0
你应该使用request()->route()->named()方法:
详细信息请参考:https://laravel.com/docs/10.x/routing#inspecting-the-current-route
<li class="{{ request()->route()->named('dashboard') ? 'active' : '' }}">
<a href="{{ route('dashboard') }}">
<i class="uil uil-dashboard me-2 d-inline-block"></i>仪表板
</a>
</li>
请注意,这是 Laravel 中用于检查当前路由是否有命名的方法。
英文:
You should use the request()->route()->named() method :
https://laravel.com/docs/10.x/routing#inspecting-the-current-route
<li class="{{ request()->route()->named('dashboard') ? 'active' : '' }}">
<a href="{{ route('dashboard') }}">
<i class="uil uil-dashboard me-2 d-inline-block"></i>Dashboard
</a>
</li>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论