1

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.

Oleh
  • 55
  • 9
  • 1
    Is your file named `Navbar.php` or `navbar.php`? – ceejayoz Apr 17 '23 at 20:17
  • @ceejayoz `navbar.php`. I created it with `php artisan make:component navbar` and everything works on the local machine. – Oleh Apr 17 '23 at 20:31
  • 1
    Try renaming the file to `Navbar.php` and the class to `Navbar`. – ceejayoz Apr 17 '23 at 20:37
  • @ceejayoz It didn't help. Directly on the server created a new component with a capital letter. – Oleh Apr 17 '23 at 20:40
  • 1
    What do you mean "directly on the server" and "new component"? I'm not asking you to rerun the Artisan command, just rename the file/class. You'll note in all the examples at https://laravel.com/docs/10.x/blade the components are `Cased` `Like` `This`; this is standard for Laravel. I strongly suspect you've moved from a case-insensitive OS like Windows to a case-sensitive one like Linux on the server, and that `` looks for `Navbar`, not `navbar`. – ceejayoz Apr 17 '23 at 20:44
  • @ceejayoz I ran the `php artisan make:component Navbar` command on the server via SSH, but I also manually renamed this file and that didn't help. – Oleh Apr 17 '23 at 20:48
  • 1
    @ceejayoz Thank you very much for your help. You were right. I redeployed the entire site to the server and everything worked. Strange that it didn't work on the server itself. – Oleh Apr 17 '23 at 21:01

1 Answers1

1

<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.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368