首页 文章

如何在Laravel 4中获取@if语句(刀片)中的当前URL?

提问于
浏览
202

我正在使用Laravel 4.我想在视图中使用Laravel 's Blade templating engine but I don' t知道如何进行 @if 条件内的当前URL .

我知道可以使用像_1298702这样的东西来完成但是在 @if 刀片语句中是不可能的 .

有什么建议?

22 回答

  • 15

    另一种在Laravel中使用path编写if和else的方法

    <p class="@if(Request::is('path/anotherPath/*')) className @else anotherClassName @endif" >
     </p>
    

    希望能帮助到你

  • 0

    你应该试试这个:

    <b class="{{ Request::is('admin/login') ? 'active' : '' }}">Login Account Details</b>
    
  • 1

    而不是使用 URL::path() 检查当前路径位置,您可能需要考虑 Route::currentRouteName() ,以便万一您更新路径,您不需要浏览所有页面以再次更新路径名称 .

  • 15
    @if(request()->path()=='/path/another_path/*')
    @endif
    
  • 0

    有很多方法可以实现,我总是使用它们

    Request :: url()

  • 4

    试试这个:

    <li class="{{ Request::is('Dashboard') ? 'active' : '' }}">
        <a href="{{ url('/Dashboard') }}">
    	<i class="fa fa-dashboard"></i> <span>Dashboard</span>
        </a>
    </li>
    
  • 0

    试试这种方式:

    <a href="{{ URL::to('/registration') }}">registration </a>
    
  • 4

    试试这个:

    @if(collect(explode('/',\Illuminate\Http\Request::capture()->url()))->last() === 'yourURL')
           <li class="pull-right"><a class="intermitente"><i class="glyphicon glyphicon-alert"></i></a></li>
        @endif
    
  • 0

    您可以使用此代码获取当前URL:

    echo url()->current();

    echo url()->full();

    我从Laravel文档中得到了这个 .

  • 289

    最简单的方法是使用: Request::url();

    但这是一个复杂的方式:

    URL::to('/').'/'.Route::getCurrentRoute()->getPath();
    
  • 72

    有两种方法可以做到这一点:

    <li{!!(Request::is('your_url')) ? ' class="active"' : '' !!}>
    

    要么

    <li @if(Request::is('your_url'))class="active"@endif>
    
  • 7

    将此代码设置为自动应用于您需要在Laravel项目中使用 HTMLBuilder 库的每个 <li>

    <script type="text/javascript">
        $(document).ready(function(){
            $('.list-group a[href="/{{Request::path()}}"]').addClass('active');
        });
    </script>
    
  • 11

    您可以使用: Request::url() 获取当前URL,这是一个示例:

    @if(Request::url() === 'your url here')
        // code
    @endif
    

    Laravel提供了一种方法来查明URL是否与模式匹配

    if (Request::is('admin/*'))
    {
        // code
    }
    

    查看相关文档以获取不同的请求信息:http://laravel.com/docs/requests#request-information

  • 0

    您也可以使用 Route::current()->getName() 检查您的路线名称 .

    示例:routes.php

    Route::get('test', ['as'=>'testing', function() {
        return View::make('test');
    }]);
    

    视图:

    @if(Route::current()->getName() == 'testing')
        Hello This is testing
    @endif
    
  • 21

    也许你应该试试这个:

    <li class="{{ Request::is('admin/dashboard') ? 'active' : '' }}">Dashboard</li>
    
  • 0

    我这样做:

    @if (Request::path() == '/view')
        // code
    @endif
    

    其中'/ view'是routes.php中的视图名称 .

  • 8

    这对我在 Laravel 5.2 中的bootstrap活动导航类有帮助:

    <li class="{{ Request::path() == '/' ? 'active' : '' }}"><a href="/">Home</a></li>
    <li class="{{ Request::path() == 'about' ? 'active' : '' }}"><a href="/about">About</a></li>
    
  • 9

    有点老但这在L5中有效:

    <li class="{{ Request::is('mycategory/', '*') ? 'active' : ''}}">
    

    这会捕获/ mycategory和/ mycategory / slug

  • 54

    要在 blade 视图中获取 current url ,您可以使用以下内容,

    <a href="{{url()->current()}}">Current Url</a>
    

    因此,您可以使用以下代码进行比较,

    @if (url()->current() == 'you url')
        //stuff you want to perform
    @endif
    

    希望能帮助到你 :)

  • 8

    Laravel 5.4

    全球职能

    @if (request()->is('/'))
        <p>Is homepage</p>
    @endif
    
  • 37

    我个人认为Laravel并不令人惊讶,但我想你需要将你的路线发送到一个控制器,然后在控制器内,使用像 $url = Request::url(); 之类的东西将变量(通过数组)传递到你的视图中 .

    无论如何,这样做的一种方式 .

    编辑:实际上看看上面的方法,可能是一个更好的方法 .

  • 12

    带引导程序的简单导航栏可以完成:

    <li class="{{ Request::is('user/profile')? 'active': '' }}">
            <a href="{{ url('user/profile') }}">Profile </a>
        </li>
    

相关问题