如果我在Laravel中提供链接路由,它们全部变为活动状态。

huangapple go评论58阅读模式
英文:

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

&lt;li class=&quot;{{ (strpos(Route::currentRouteName(), &#39;dashboard&#39;) == 0) ? &#39;active&#39; : &#39;&#39; }}&quot;&gt;
&lt;a href=&quot;{{ route(&#39;dashboard&#39;) }}&quot;&gt;
&lt;i class=&quot;uil uil-dashboard me-2 d-inline-block&quot;&gt;&lt;/i&gt;Dashboard&lt;/a&gt;&lt;/li&gt;

Can somebody explain me why is that happening?

答案1

得分: 1

strposRoute::currentRouteName()&#39;dashboard&#39; 时返回 int(0),否则返回 bool(false)

你正在使用 == 运算符,这不是严格比较。尝试使用 === 代替。

false == 0  // true
false === 0 // false
英文:

strpos returns int(0) when Route::currentRouteName() is &#39;dashboard&#39; 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()-&gt;route()-&gt;named() method :

https://laravel.com/docs/10.x/routing#inspecting-the-current-route

&lt;li class=&quot;{{ request()-&gt;route()-&gt;named(&#39;dashboard&#39;) ? &#39;active&#39; : &#39;&#39; }}&quot;&gt;
    &lt;a href=&quot;{{ route(&#39;dashboard&#39;) }}&quot;&gt;
          &lt;i class=&quot;uil uil-dashboard me-2 d-inline-block&quot;&gt;&lt;/i&gt;Dashboard
    &lt;/a&gt;
&lt;/li&gt;

huangapple
  • 本文由 发表于 2023年5月10日 22:48:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定