Laravel 5, Use URL for setting active class - php

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>

Related

how to create a switch language in Laravel?

I'm having a problem to build a translation switcher, this is what I have already did:
Step 1 : I have prefixed all the routes with the locale parameter {languge}
Route::redirect('/', '/en');
Route::group(['prefix' => '{language}'], function () {
//products routes
Route::get('/', 'ProductController#index' )->name('acceuil');
Route::get('/boutique/{slug}', 'ProductController#show' )->name('product.show');
Route::get('/search', 'ProductController#search' )->name('product.search');
});
step 2: then I created a middleware for setting the language of app depending on the request
public function handle($request, Closure $next)
{
App::setLocale($request->language);
return $next($request);
}
step 3: here I added Locale parameter {languge} to All Existing Links, like this:
<form method="POST" action="{{ route('register', app()->getLocale()) }}">
right here everything is fine but:
here I want to create a language switcher, this is what I did:
step 4: here I added Locale parameter {languge}to All Existing Links, like this:
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
{{ _('language') }}<span class="caret"></span>
<ul class="dropdown-menu">
<li>{{ _('Francais') }}</li>
<li role="separator" class="divider"></li>
<li>{{ _('anglais') }}</li>
</ul>
</li>
</ul>
The problem is that I have different routes, some of them require a parameters and then Step 4 doesn't work:
ANY ideas ?
This seems to work for me:
{{ route(Route::currentRouteName(), array_merge(['fr'], array_slice(($rp = Route::current()->parameters()), 1, count($rp)))) }}
First we need to array_slice the old language from the parameters then we can merge them with the new language. Since we merge the language with the current parameters, the order will always give us the language first and subsequent ordering of the current parameters.

Laravel assigning classes to blade files dynamically fails

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!

Adding a Variable to Laravel Request

I'm trying to make an Active-Navbar on my website, however in one category of my navbar it is dynamically generated and I can't seem to figure out how to put a variable in to check the request
Here's the code I have:
#foreach( Auth::user()->getTests() as $Test )
<li {{ (Request::is('/test/'.$Test->id.'/*') ? "class=nav-active" : '') }}>
<a href="{{URL("/test/$Test->id/view/")}}">
{{$Test->name}}
</a>
</li>
#endforeach
I've tried putting double quotes in (as PHP sees variables with double quotes), but it still doesn't seem to work. The desired outcome can be seen here
http://preview.oklerthemes.com/?theme=PortoAdmin
I just have a list of dynamically generated test names that I'd like to be the active item when they are selected.
Thanks!
Zach
EDIT: There seems to be some confusion, I was referencing this line:
<li {{ (Request::is('/test/'.$Test->id.'/*') ? "class=nav-active" : '') }}>
I've also tried
<li {{ (Request::is("/test/$Test->id/*") ? "class=nav-active" : '') }} >
Try removing the first slash
<li "{{ Request::is('test/'.$Test->id.'/*') ? "class=nav-active" : "" }}" >
I believe you are trying to append a variable to a string. You can use the String Operator
{{URL("/test/".$Test->id."/view/")}}

Best Practice to Highlight the Left menu using PHP

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!

Laravel get current route

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.

Categories