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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17591181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:32:55  来源:igfitidea点击:

How to Get the Current URL Inside @if Statement (Blade) in Laravel 4?

laravellaravel-4laravel-blade

提问by user2443854

I am using Laravel 4. I would like to access the current URL inside an @ifcondition in a view using the Laravel's Blade templating engine but I don't know how to do it.

我正在使用 Laravel 4。我想@if使用 Laravel 的 Blade 模板引擎访问视图中条件内的当前 URL,但我不知道该怎么做。

I know that it can be done using something like <?php echo URL::current(); ?>but It's not possible inside an @ifblade statement.

我知道它可以使用类似的东西来完成,<?php echo URL::current(); ?>但在@if刀片语句中是不可能的。

Any suggestions?

有什么建议?

回答by Andreyco

You can use: Request::url()to obtain the current URL, here is an example:

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

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

Laravel offers a method to find out, whether the URL matches a pattern or not

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

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

Check the related documentation to obtain different request information: http://laravel.com/docs/requests#request-information

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

回答by Naing Win

You can also use Route::current()->getName()to check your route name.

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

Example: routes.php

示例:routes.php

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

View:

看法:

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

回答by Dean Gite

Maybe you should try this:

也许你应该试试这个:

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

回答by Jucaman

I'd do it this way:

我会这样做:

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

where '/view' is view name in routes.php.

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

回答by Sagar Naliyapara

To get current urlin bladeview you can use following,

为了获得current urlblade视图中,可以使用以下,

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

So as you can compare using following code,

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

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

回答by Abduhafiz

This is helped to me for bootstrap active nav class in Laravel 5.2:

这对我在Laravel 5.2 中引导活动导航类有帮助

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

回答by VineFreeman

A little old but this works in L5:

有点旧,但这适用于 L5:

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

This captures both /mycategory and /mycategory/slug

这会同时捕获 /mycategory 和 /mycategory/slug

回答by Rob

Laravel 5.4

Laravel 5.4

Global functions

全局函数

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

回答by Alias

I personally wouldn't try grabbing it inside of the view. I'm not amazing at Laravel, but I would imagine you'd need to send your route to a controller, and then within the controller, pass the variable (via an array) into your view, using something like $url = Request::url();.

我个人不会尝试在视图中抓取它。我对 Laravel 并不感兴趣,但我想你需要将路由发送到控制器,然后在控制器中,使用类似$url = Request::url();.

One way of doing it anyway.

无论如何,一种方法。

EDIT: Actually look at the method above, probably a better way.

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

回答by Alejandro Silva

A simple navbar with bootstrap can be done as:

一个带有引导程序的简单导航栏可以这样完成:

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