Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set class based on current route name/controller

Trying to set a class based on my current controller or current route (URL Segment 1).

something like

<body class="{{controllerName}}"> 

That way in case I need to target separate pages for CSS specificity, it makes it easy.

like image 525
Christopher Marshall Avatar asked Jan 08 '13 17:01

Christopher Marshall


People also ask

How do I name my current route?

Accessing The Current Route The Route::current() method will return the route handling the current HTTP request, allowing you to inspect the full Illuminate\Routing\Route instance: $route = Route::current(); $name = $route->getName(); $actionName = $route->getActionName();

How do I get the route name in blade?

Example 1: Get current route name in Blade Files $route = Route::current(); dd($route); $name = $route->getName(); dd($name); $actionName = $route->getActionName(); dd($actionName); $name = Route::currentRouteName(); dd($name); $action = Route::currentRouteAction(); dd($action);

Where on route Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web. php file defines routes that are for your web interface.


2 Answers

My solution would be: subscribe to route changes at route scope and put name of the controller there:

app.run(function($rootScope) {    $rootScope.$on('$routeChangeSuccess', function(ev,data) {         if (data.$route && data.$route.controller)        $rootScope.controller = data.$route.controller;    }) }); 

Check Plunker solution

like image 152
Valentyn Shybanov Avatar answered Sep 28 '22 04:09

Valentyn Shybanov


You can use the $route service, it has current property which will give you current controller.

like image 45
pavelgj Avatar answered Sep 28 '22 04:09

pavelgj