Support the ongoing development of Laravel.io →
IOC Views Architecture
Last updated 2 years ago.
0

One way would be to name your routes as the class you want to use in your body tag (i.e. users-show-all). Then, in a base controller constructor, you could get the current route and share it globally to all views:

In BaseController.php

public function __construct()
{
    $name = Route::current()->getName();
    View::share('routeName', $name);
}

Then $routeName will be available in all views (so long as all controllers extend BaseController and its constructor is being called, and your routes are named). I'd suggest not blindly using $routeName and instead checking if it's set before echoing it out in the body tag.

Last updated 2 years ago.
0

I found out even easier solution with View::composer() method. In my /bootstrap/start.php I put:

View::composer('layout', function ($view) {
    $action = Route::current()->getAction()['controller'];
    $action = class_basename($action);
    $action = snake_case($action);
    $action = str_replace(array('@', '_'), '-', $action);

    $view->withBodyClass($action);
});

And then in my layout.blade.php:

<body class="{{{ $body_class }}}">

Works like charm.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

kokokurak kokokurak Joined 21 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.