Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

number6's avatar

Running Artisan Command Breaks my View Composer

I have a View Composer helper that is added to all views. It's for customizing a class on the <body> tag. This will be used for CSS if I need something custom for a specific page.

It's setup like this in a package routes.php

View::composer('*', function($view) {
  $name = str_replace('::', '_', $view->getName());
  $name = str_replace('.', '_', $name);
  View::share('view_name', $name);
});

In my master.blade.php:

<body class="with-navbar {{ $view_name }}">

Please note, I am loading this View::composer from a custom package I wrote. It works just fine until I try and go through my deploy process.

This is executed at the end of my deploy script:

php artisan clear-compiled && php artisan optimize
php artisan route:cache
php artisan queue:restart
service php7.1-fpm restart

As soon as I do that and refresh the page, I get Undefined variable: view_name in my blade template. I just saw that php artisan optimize is deprecated, but I can't get my app running properly again.

How do I go about making view_name work again?

0 likes
5 replies
Snapey's avatar

might sound a daft question, but you installed your package?

number6's avatar

Yes, the package is available and it all worked great until I ran the commands above.

jcmargentina's avatar

I dont know exactly all files in your code but with it a try to this ...

View::composer('*', function($view) {
  $name = str_replace('::', '_', $view->getName());
  $name = str_replace('.', '_', $name);
  $view->share('view_name', $name);
});
number6's avatar

I tried it with $view->share and it error'd out: "Method [share] does not exist on view."

Please or to participate in this conversation.