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

可紊 提交于 2019-12-17 08:04:50

问题


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

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

Any suggestions?


回答1:


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

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

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

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

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




回答2:


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

Example: routes.php

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

View:

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



回答3:


Maybe you should try this:

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



回答4:


I'd do it this way:

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

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




回答5:


To get current url in blade view you can use following,

<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



回答6:


This is helped to me for bootstrap active nav class in 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>



回答7:


A little old but this works in L5:

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

This captures both /mycategory and /mycategory/slug




回答8:


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();.

One way of doing it anyway.

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




回答9:


Laravel 5.4

Global functions

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



回答10:


A simple navbar with bootstrap can be done as:

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



回答11:


The simplest way is to use: Request::url();

But here is a complex way:

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



回答12:


There are two ways to do that:

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

or

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



回答13:


Set this code to applied automatically for each <li> + you need to using HTMLBuilder library in your Laravel project

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



回答14:


You should try this:

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



回答15:


You can use this code to get current URL:

echo url()->current();

echo url()->full();

I get this from Laravel documents.




回答16:


Another way to write if and else in Laravel using path

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

Hope it helps




回答17:


instead of using the URL::path() to check your current path location, you may want to consider the Route::currentRouteName() so just in case you update your path, you don't need to explore all your pages to update the path name again.




回答18:


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



回答19:


For named routes, I use:

@if(url()->current() == route('routeName')) class="current" @endif



回答20:


class="nav-link {{ \Route::current()->getName() == 'panel' ? 'active' : ''}}"



回答21:


Try This:

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



回答22:


Try this:

@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



回答23:


There are many way to achieve, one from them I use always

 Request::url()



回答24:


The simplest way is

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

This colud capture the contacts/, contacts/create, contacts/edit...




回答25:


In Blade file

@if (Request::is('companies'))
   Companies name 
@endif



回答26:


Try this way :

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


来源:https://stackoverflow.com/questions/17591181/how-to-get-the-current-url-inside-if-statement-blade-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!