Laravel "Unable to locate a class or view for component" in production environment [duplicate]
Asked Answered
L

14

26

I develop on a Mac locally. Latest version of Big Sur.

Today I went to deploy my app to production via an Ubuntu server through Forge, and got greeted with an error I've never seen before, and can't find an answer to online. I can see MANY people complaining about it, but all anyone has said on other answers is link to issues that don't have solutions or even explanations really, so that's why I'm asking a new question.

The exact error is this; Unable to locate a class or view for component [layouts.base]. (View: /home/forge/default/releases/20201204084441/resources/views/layouts/app.blade.php)

In my app I have; app\View\Components\Layouts\App.php which looks like this;

<?php

namespace App\View\Components\Layouts;

use Illuminate\View\Component;

class App extends Component
{
    public function render()
    {
        return view('layouts.app');
    }
}

Then I also have; resources\views\layouts\app.blade.php

<x-layouts.base>
<!-- contents -->
</x-layouts.base>

(Also pretty much the same for base)

Works flawlessly on Mac. As soon as I deploy it on Ubuntu, I get the error above that it is "unable to locate a class or view" with those names.

Can someone please instruct me on how I can go about fixing this, since so far I have absolutely no idea and despite knowing that case sensitivity is probably the issue as per the other questions about this, I cannot find any actual solution or way to resolve this.

Leodora answered 4/12, 2020 at 9:4 Comment(6)
try composer dump-autoloadJeane
@KamleshPaul Thanks for the suggestion but same issue/error. Can also trigger the same error by running php artisan view:cache on my server though. But if I run the same command on my Mac it completes successfully.Leodora
check in your ubuntu views/layouts/app.blade.php is this present ?Jeane
@KamleshPaul Yes it is present.Leodora
in same folder /home/forge/default/releases/20201204084441?Jeane
@KamleshPaul Yeah that one also has the layout files present as well.Leodora
I
25

I had the same problem. Thanks to your question, I could find out how to solve it :D

In my case, I had created a component inside another folder, for better organization sake:

$ php artisan make:component Tutorial/channelName/Alert

So it created the view component inside the following directory:

views/components/tutorial/channel-name/alert.blade.php

Now, to call your component you do it this way:

<x-tutorial.channelName.alert />

That's pretty much it.

Interlocutory answered 15/12, 2020 at 1:16 Comment(0)
J
6

I was facing the same issue and I fixed it by cross-checking my folder name.

Please note that the folder name should be components and not component.

Refer the screenshot for a better idea.

enter image description here

Example:

In case you are following the convention, one more thing is that if you have a file at

views/components/admin/side-menu/side.blade.php

You can call your Component as:

<x-admin.side-menu.side></x-admin.side-menu.side>

Explained:

The x- used in the blade syntax basically tells that you are selecting folder or file from the Components folder.

The . (dot) used is for every directory you dig to the blade file you want to use.

Jojo answered 6/8, 2021 at 15:14 Comment(1)
Took me way too long to figure out I named it component instead of components, thanks :)Chyou
C
2

Well, I had the same problem but I realized that the error was the class name of the component. I didn't capitalize the class name and when I did capitalize the class name I stopped getting that error and my project worked well.

Please try capitalizing the class name that matches the component in the error message.

Celebrant answered 24/1, 2021 at 10:38 Comment(0)
K
2

The issue for the mentioned problem is case sensitive,

for local looks like its case insensitive reasons its works for local perfect but breaks on serve.

if you have create new component inside any new folder i.e

$ php artisan make:component Widgets/CustomAlert

it will create two files, class and blade file for the component. class file

app\Views\Components\\CustomAlert.php

blade file

resources\views\components\widgets.custom-alert.blade.php

you can render component with either class or with blade file. for class make sure folder, class name in case sensitive ie.

<x-Widgets.CustomAlert />

to render blade file it should be

<x-widgets.custom-alert />

Note if you have manually relocate the file, make sure your namespace in class file, filename and the component call syntax should be match(with case sensitive)

examples are just for reference try to compare syntax and names with your files,folder.

I hope it will help :)

Klehm answered 3/6, 2022 at 9:32 Comment(0)
C
1

In my Case, I didn't provide the namespace in the Component, it solved after providing namespace.

Circuit answered 23/3, 2022 at 13:6 Comment(0)
J
1

it seems like youre missing the Components folder that comes with laravel inside the views folder , make sure you have that folder inside your file structure but if you dont then simply run the

composer update

it will add the components folder that youre missing alongside with other files that your project is missing

Johnson answered 7/3, 2023 at 17:56 Comment(0)
O
0

when I was a beginner I got the same error But in my case, there is all ok but there is a small syntax error <x- card /> there is a gap between card and dash which is not correct so there should be no space between dash and card <x-card /> so try once maybe this is your problem.

Osmond answered 23/6, 2021 at 9:58 Comment(0)
R
0

I had the same problem - solution was to change: <x-forms.inputradio> to: <x-forms.inputRadio> just letter size..

Ruprecht answered 31/3, 2022 at 21:38 Comment(0)
B
0

In my case, I had created a class AppLogo and I had the blade generated as app-logo.blade.php.

However, in my view files, I referenced the component as x-applogo and this worked locally, but in production (a live hosting server), I got this error.

SOLUTION: I changed all occurrences of x-applogo in my blade/view files to x-app-logo.

This worked both in development and in production.

Hope this helps someone out there.

Berry answered 21/11, 2022 at 23:24 Comment(0)
I
0

I had the same problem in Ubuntu after it working perfectly on mac and windows. I managed to get it to work by in my case replacing

<x-header />

with

<?php
$header = new header;
echo $header->render();
?>

and including at the top

use App\Views\Components\header;
Inward answered 15/3, 2023 at 1:53 Comment(0)
P
0

My issue has been solved with composer autoload.So, if you recently updated composer then use this:

composer dump-autoload

It will solve your issue.

Peggy answered 30/10, 2023 at 6:24 Comment(0)
W
0

Have had this issue couple of times. Each time the reason was my inattentiveness. I.e. x-frontend-layouts-services instead of x-frontend.layouts.services. So be sure to check everything carefully

Whitleather answered 21/11, 2023 at 18:39 Comment(0)
F
0

In my case, I had another component in my component blade.php file which didn't exist.

Fenestrated answered 22/2 at 20:3 Comment(0)
T
-1

This happens at server. try not to make components inside folder.. i had the same issue at server but did not get any solution, the only solution worked is to keep your components outside any sub folder

Traumatism answered 10/1, 2022 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.