I have created a Laravel Component and it works fine on my local machine, but when I deploy the project to the hosting it stops working there and the error Undefined variable $navbar
occurs.
app\View\Components
class navbar extends Component
{
/**
* Create a new component instance.
*/
public function __construct(public $navbar = [])
{
$this->navbar = config('navbar');
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.navbar');
}
}
When I use the component <x-navbar ></x-navbar>
an error occurs, but if I add the attribute <x-navbar :navbar="config('navbar')"></x- navbar>
the error disappears.
I have a feeling that the component class is ignored on the hosting, since even dd('test')
does not work if it is added to __construct
. There are no such problems on the local machine.
The project was deployed on www.hostinger.com according to their instructions.
<x-navbar>
will look for a Blade component named Navbar
in a file named Navbar.php
.
Unfortunately, Windows is case insensitive, but Linux is not. Naming the file navbar.php
and the class navbar
will work on Windows, but fail when deployed to a Linux webhost. (PHP's classes are case-insensitive, so navbar
, Navbar
, or nAvBaR
will all work, but the Composer autoloader's file paths are case-sensitive if on a case-sensitive system.)
When working with Laravel, it's important to follow their coding style to avoid this issue; Laravel expects classes to use PascalCase and for file names to match. Laravel Pint can automatically resolve these and other such issues for you.