Best Practice to Highlight the Left menu using PHP - 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!

Related

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!

Laravel 5 next and previous paging

Hello I have a list of max 5 articles in my homepage but I have many more articles and I would like to show these articles in other pages. So when I see more old posts, I would like to do a pagination like user home page / 1 when I get it as a result of posting it as url. But I do not follow the correct paths when I make directions.
I need to change the following lines of code to help you change the point, good day, good day
app/Http/Controller/HomeController:
public function deneme($page){
$url = url()->full();
$myUrl = explode('/', $url);
$uz= sizeof($myUrl);
$myUrl = $myUrl[$uz-1];
if ($myUrl == 'work.com'){
$yazilar = YaziModel::join('users as u','u.id','=', 'yazilar.kullaniciid')->select('yazilar.*','u.name','u.created_at')->orderBy('yazilar.id', 'DESC')->get();
$posts = array_slice($yazilar->getIterator()->getArrayCopy(),0,5);
return view('backend.pages.anasayfa')->with('yazilar', $posts);
}else{
$baslangic = $page*5;
$yazilar = YaziModel::join('users as u','u.id','=', 'yazilar.kullaniciid')->select('yazilar.*','u.name','u.created_at')->orderBy('yazilar.id', 'DESC')->get();
$posts = array_slice($yazilar->getIterator()->getArrayCopy(),$baslangic,5);
return view('backend.pages.anasayfa')->with('yazilar' ,$posts);
}
}
routes/web.php:
Route::get('/{page}', 'HomeController#deneme');
View :
<ul class="pager">
<li class="next">
Older Post →
</li>
</ul>
You can use this code in the blade (Laravel 5.7)
#if($news->previousPageUrl() != null)
<i class="fa fa-chevron-left"></i> NEWER
#endif
#if($news->nextPageUrl() != null)
OLDER <i class="fa fa-chevron-right"></i>
#endif
Use Pagination
Controller
$users = DB::table('users')->paginate(15);
View
{{ $users->links() }}
for More Details https://laravel.com/docs/5.2/pagination

Laravel 5, Use URL for setting active class

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>

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/")}}

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