Skip to content

Instantly share code, notes, and snippets.

@phegman
Created November 28, 2017 03:16
Show Gist options
  • Save phegman/58d674cec6368aa3ff6ad692000ba1d5 to your computer and use it in GitHub Desktop.
Save phegman/58d674cec6368aa3ff6ad692000ba1d5 to your computer and use it in GitHub Desktop.
Laravel - body class based on view name
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
@routes
</head>
<body class="{{ $viewName }}">
<div id="app">
@yield('content')
</div>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::composer('*', function ($view) {
View::share('viewName', str_replace('.', '-', $view->name()));
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment