Open your AppServiceProvider
class, boot()
method and insert this code:
View::composer('*', function ($view) {
$view_name = str_replace('.', ' ', $view->getName());
View::share('view_name', $view_name);
});
Then you can access $view_name
variable in your view.
Regarding @Stuart answer, use $view->getName()
will returns exactly view path with dot notation. For example, you view is under resources/views/settings/index
directory, your view name is: settings.index
.
So then? the class name will contains dot
character means that is little more confusing whilst writing CSS. See this thread:
Styling elements with a dot (.) in the class name
By replacing .
with space, the class name in above example will becomes settings index
and you can use .settings
selector, or .index
selector, or .setting.index
selector.
Custom Class Name
The above method is for automatically generate class name, you have many methods:
@yield()
and @section()
<body class="@yield('body-class')">
Then in your views:
@section('body-class', 'your-class-name')
@stack()
and @push()
Since Laravel 5.4, you can use @stack
and @push
, this allows you push as many time as you want.
<body class="@stack('body-class')">
Then in your views
@push('body-class', 'your-class-name')
@push('body-class', ' another-class-name')