I would like to assign classes if a route match a certain pattern.
suppose i have these urls as below:
user-management/users
user-management/roles
user-management/role?user=andy
user-management/permissions
Now i would like to add active class to a link. So i have tried:
<li class="{{ (Request::path() == 'user-management/*') ? 'dropdown active ' : 'dropdown' }}">
But the above fails to add active class. Trying with:
<li class="{{ (Request::path() == 'user-management/users') ? 'dropdown active ' : 'dropdown' }}">
Which works when i visit /user-management/users
How can i make the other one work with all the user-management link urls
What else could be wrong?
Am using laravel 5.5 and my routes in web.php are
Route::group(["middleware"=>'auth', 'prefix'=>'user-management'], function (){
Route::get("users", "UsersController#ShowUsers")->name("user-management.users");
Route::get("roles", "UsersController#ShowRoles")->name("user-management.roles");
.....others follow
});
You can do it for all user-management URL. Like:
<li class="{{ Request::is('user-management/*') ? 'dropdown active' : 'dropdown' '' }}>
Hope this works for you!
Using Laravel 5.3
Have stripped this right back but basically I have a very simple list, I want to add class 'active' to the list item if Request::is('url') returns true.
<ul>
<li class="{{ Request::is('one') ? 'active' : '' }}">One</li>
<li class="{{ Request::is('two/sub') ? 'active' : '' }}">Two</li>
</ul>
This works perfectly fine for most of my requests, however if my request looks something like the below..
http://homestead.app/one?search=some_search_string
.. li 'one' would still be active, I do not want this, If there are extra parameters I do not want the class active applied.
How would I go about putting this behaviour into place?
I think you can do it by $_GET try like this
<li class="{{ (Request::is('one') && !count($_GET)) ? 'active' : '' }}">One</li>
Exactly #Rishi I agree with your solution.
<ul>
<li class="{{ (Request::is('one') && !count($request->query)) ? 'active' : '' }}">One</li>
<li class="{{ (Request::is('two/sub') !count($request->query)) ? 'active' : '' }}">Two</li>
</ul>
I have
a left menu , and I'm seeking the best practice to highlight the left menu using PHP.
Route
http://localhost:8888/000D6766F2F6/network/create
http://localhost:8888/000D6766F2F6/network
I've tried
create a function, base on my route, I grab the URL segment, and check for it's existing.
public static function customerTab($tab){
$url = Request::url();
if (strpos($url, $tab) !== FALSE){
return 'active';
}else{
return '';
}
}
I've called it
Network
<li class="{{ Helper::customerTab('network')}}"><i class="fa fa-cloud"></i><span>Network</span></li>
My Network
<li class="{{ Helper::customerTab('network')}}"><i class="fa fa-sitemap"></i> My Network</li>
Since both of the routes containing the word network, I don't think my approach is working. I'm opening to any advice at this moment.
You can use Request::is() to determine if the current URL path matches a simple glob. It defers to str_is() for matching (https://laravel.com/docs/5.1/helpers#method-str-is), so you can do very simple wildcard matching, e.g. Request::is('*/network') or Request::is('*/network/*').
<li class="{{ Request::is('*/network') ? 'active' : null }}">... Network ...</li>
<li class="{{ Request::is('*/network/*') ? 'active' : null }}">... My Network ...</li>
Hope that helps!
I have some navigation links and I want to get the current route in laravel 5 so that I can use the class active in my navigations. Here are the sample links:
<li class="active"> Home </li>
<li> Contact </li>
<li> About </li>
As you can see, in the Home link there is the active class which means that it is the current selected route. But since I am using laravel blade so that I would not repeat every navigation links in my blades, I can't use the active class because I only have the navigation links in my template.blade.php. How can I possibly get the current route and check if it is equal to the current link? Thank you in advance.
$uri = Request::path()
Check out the docs
You can use named routes like
Route::get('/', ['as'=>'home', 'uses'=>'PagesController#index']);
and then in the view you can do the following
<li {!! (Route::is('home') ? 'class="active"' : '') !!}>
{!! link_to_route('home', 'Home') !!}
</li>
So I got it working with some little tricks. For example the url in the address bar is http://localhost:8000/tourism/places.
If I will use the Request::path() method, I will get tourism/places. But what I wanted to get is the tourism part only so what I did is this:
$currentPath = Request::path();
$values = explode("/", $currentPath);
$final = $values[0];
Now the $final variable will have the value "tourism".
Now to make the variable load globally, I included the code in the AppServiceProvider.php inside the public function boot().
public function boot()
{
$currentPath = Request::path();
$values = explode("/", $currentPath);
$final = $values[0];
view()->share('final', $final);
}
And lastly in my blade template,
<li #if ($final== "tourism") class="active" #endif>
Places
</li>
Simple way is always better, just use the same function you use to generate links and it will give you all, including the flexibility to use wild cards
<a class="nav-link {{ Request::url() === route("products.list", [$group_key]) ? 'active' : '' }}"
href="{{ route("products.list", [$group_key]) }}"
>
LInk text
</a>
Plus, you still are going to have only one place to manage URL-s - your router files.
Is it possible to check into a blade view if #yield have content or not?
I am trying to assign the page titles in the views:
#section("title", "hi world")
So I would like to check in the main layout view... something like:
<title> Sitename.com {{ #yield('title') ? ' - '.#yield('title') : '' }} </title>
For those looking on it now (2018+), you can use :
#hasSection('name')
#yield('name')
#endif
See : https://laravel.com/docs/5.6/blade#control-structures
In Laravel 5 we now have a hasSection method we can call on a View facade.
You can use View::hasSection to check if #yeild is empty or not:
<title>
#if(View::hasSection('title'))
#yield('title')
#else
Static Website Title Here
#endif
</title>
This conditional is checking if a section with the name of title was set in our view.
Tip: I see a lot of new artisans set up their title sections like this:
#section('title')
Your Title Here
#stop
but you can simplify this by just passing in a default value as the second argument:
#section('title', 'Your Title Here')
The hasSectionmethod was added April 15, 2015.
There is probably a prettier way to do this. But this does the trick.
#if (trim($__env->yieldContent('title')))
<h1>#yield('title')</h1>
#endif
Given from the docs:
#yield('section', 'Default Content');
Type in your main layout e.g. "app.blade.php", "main.blade.php", or "master.blade.php"
<title>{{ config('app.name') }} - #yield('title', 'Otherwise, DEFAULT here')</title>
And in the specific view page (blade file) type as follows:
#section('title')
My custom title for a specific page
#endsection
#hasSection('content')
#yield('content')
#else
\\Something else
#endif
see "Section Directives" in If Statements - Laravel docs
You can simply check if the section exists:
if (isset($__env->getSections()['title'])) {
#yield('title');
}
And you can even go a step further and pack this little piece of code into a Blade extension: http://laravel.com/docs/templates#extending-blade
Complete simple answer
<title> Sitename.com #hasSection('title') - #yield('title') #endif </title>
I have a similar problem with the solution:
#section('bar', '')
#hasSection('bar')
<div>#yield('bar')</div>
#endif
//Output
<div></div>
The result will be the empty <div></div>
Now, my suggestion, to fix this, is
#if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
<div>#yield('bar')</div>
#endif
New in Laravel 7.x -- sectionMissing():
#hasSection('name')
#yield('name')
#else
#yield('alternative')
#endif
Check if section is missing:
#sectionMissing('name')
#yield('alternative')
#endif
#if (View::hasSection('my_section'))
<!--Do something-->
#endif
Use View::hasSection to check if a section is defined and View::getSection to get the section contents without using the #yield Blade directive.
<title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>
I don't think you can, but you have options, like using a view composer to always provide a $title to your views:
View::composer('*', function($view)
{
$title = Config::get('app.title');
$view->with('title', $title ? " - $title" : '');
});
why not pass the title as a variable View::make('home')->with('title', 'Your Title') this will make your title available in $title
Can you not do:
layout.blade.php
<title> Sitename.com #section("title") Default #show </title>
And in subtemplate.blade.php:
#extends("layout")
#section("title") My new title #stop
The way to check is to not use the shortcut '#' but to use the long form: Section.
<?php
$title = Section::yield('title');
if(empty($title))
{
$title = 'EMPTY';
}
echo '<h1>' . $title . '</h1>';
?>
Building on Collin Jame's answer, if it is not obvious, I would recommend something like this:
<title>
{{ Config::get('site.title') }}
#if (trim($__env->yieldContent('title')))
- #yield('title')
#endif
</title>
Sometimes you have an enclosing code, which you only want to have included in that section is not empty. For this problem I just found this solution:
#if (filled(View::yieldContent('sub-title')))
<h2>#yield('sub-title')</h2>
#endif
The title H2 gets only displayed it the section really contains any value. Otherwise it won't be printed...