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

jeroenvip's avatar

Creating a component with kebab-case naming conventions producing an error ?

Hi All,

I am new to leaning laravel and after following the Laravel in 30 days course I started to build my first Laravel project. I came across this situation:

Naming conventions inside the views/components/ is kebab-case. So a navigation link component should be named nav-link.blade.php

php artisan make:component nav-link
Component [app/View/Components/nav-link.php] created successfully.

View [resources/views/components/nav-link.blade.php] created successfully.  

Where the component itself creates this class.

<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class nav-link extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.nav-link');
    }
}

Why is it creating a class name with a hyphen ? And not using camelCase

0 likes
3 replies
LaryAI's avatar
Level 58

The issue you're encountering is due to the naming conventions used by Laravel when creating components. When you use the php artisan make:component command with a kebab-case name, Laravel creates the Blade view file with the same kebab-case name, but the PHP class should follow the PSR-4 naming convention, which uses PascalCase (also known as StudlyCase).

Here's how you can resolve this:

  1. Rename the Component Class: Change the class name from nav-link to NavLink in your nav-link.php file.

  2. Update the File Name: Rename the file nav-link.php to NavLink.php to match the class name.

  3. Correct the Namespace and Class Declaration: Ensure the class declaration matches the new class name.

Here's how your component class should look:

<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class NavLink extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.nav-link');
    }
}
  1. Blade Component Usage: You can still use the component in your Blade files with the kebab-case syntax:
<x-nav-link />

By following these steps, you ensure that your component class adheres to PHP naming conventions while still allowing you to use the kebab-case syntax in your Blade templates.

martinbean's avatar

@jeroenvip You should be using StudlyCase when creating components:

php artisan make:component NavLink

This should create a PHP class named NavLink but then a Blade template named nav-link.blade.php.

Snapey's avatar
Snapey
Best Answer
Level 122

You can also create the component without the class at all by adding --view to the artisan command.

1 like

Please or to participate in this conversation.