Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉威尔得到当前路线_Php_Html_Laravel - Fatal编程技术网

Php 拉威尔得到当前路线

Php 拉威尔得到当前路线,php,html,laravel,Php,Html,Laravel,我有一些导航链接,我想获取laravel 5中的当前路线,以便在导航中使用类活动。以下是示例链接: <li class="active"> <a href="/">Home</a> </li> <li> <a href="/contact">Contact</a> </li> <li> <a href="/about">About</a> </li>

我有一些导航链接,我想获取laravel 5中的当前路线,以便在导航中使用
活动
。以下是示例链接:

<li class="active"> <a href="/">Home</a> </li>
<li> <a href="/contact">Contact</a> </li>
<li> <a href="/about">About</a> </li>
  • 如您所见,在
    主页
    链接中有
    活动类
    ,这意味着它是当前选定的路线。但是因为我使用的是
    laravelblade
    ,所以我不会重复我的blade中的每个导航链接,所以我不能使用active类,因为我的
    template.blade.php
    中只有导航链接。如何获取当前路由并检查它是否等于当前链接?提前感谢您。

    $uri=Request::path()


    查看

    您可以使用以下命名路由

    Route::get('/', ['as'=>'home', 'uses'=>'PagesController@index']);
    
    然后在视图中,您可以执行以下操作

     <li {!! (Route::is('home') ? 'class="active"' : '') !!}>
             {!! link_to_route('home', 'Home') !!}
            </li>
    
    
    {!!链接到路由('home','home')
    
    
    所以我用了一些小技巧让它工作起来。例如,地址栏中的url是
    http://localhost:8000/tourism/places

    如果我使用
    Request::path()
    方法,我将获得
    tourism/places
    。但我想得到的是
    旅游
    部分,所以我做的是:

    $currentPath = Request::path();
    
    $values = explode("/", $currentPath);
    
    $final = $values[0];
    
    现在,
    $final
    变量将具有值
    “tourism”

    现在,为了全局加载变量,我将代码包含在
    AppServiceProvider.php
    中的
    public function boot()

    最后在我的刀片模板中

    <li @if ($final== "tourism") class="active" @endif>
    <a href="/tourism/places">Places</a>
    </li>
    
    
    
    
    简单的方法总是更好,只需使用与生成链接相同的功能,它将为您提供所有功能,包括
    使用通配符的灵活性

    <a class="nav-link {{ Request::url() === route("products.list", [$group_key]) ? 'active' : '' }}" 
        href="{{ route("products.list", [$group_key]) }}"
        >
        LInk text
    </a>
    
    
    

    另外,你仍然只有一个管理URL的地方——你的路由器文件。

    你是否考虑使用JS来改变DOM?@ MDAMIa,我已经解决了它,并发布了我的答案。
    <a class="nav-link {{ Request::url() === route("products.list", [$group_key]) ? 'active' : '' }}" 
        href="{{ route("products.list", [$group_key]) }}"
        >
        LInk text
    </a>