denabyte's avatar

Vanilo "Class "Cart" not found"

I am using Vanilo 3.x trying to create an ecommerce website.

Today when I tried accessing the Cart facade for the first time in the layout view of my web app like so - @if (Cart::doesNotExist()) // code @endif as detailed in the Vanilo tutorial Laravel threw a 'Class "Cart" not found' error. This makes me believe it's not recognizing Cart as a facade.

I checked out the demo site that shows an example of how to use the framework, and on it the layout view calls the Cart facade just as I do @if (Cart::isNotEmpty()) and it all works fine there.

The config/app.php file does not contain any aliases or providers entries for the Cart facade, but it's still usable. When I try to add the CartManager into providers and then into aliases, an error is thrown stating "vanilo.cart.session_key is empty, please provide key". In my understanding, according to the Vanilo Docs, a cart is only created if it is necessary, so it doesn't flood the database with empty carts for every single visit. But the facade should still be accessible even if a cart isn't created, so by registering the cart facade into config/app.php I think I'm forcing it to try and get the cart, even though it's not created. Or maybe I'm just rambling on about stuff I don't quite understand.

I need some help trying to figure out what is going wrong, I have compared the framework configuration files (config/vanilo.php and config/concord.php) from the demo with my app and there doesn't seem to be anything out of order. I can provide code from the app, I just don't know what code would be relevant to the issue at hand initially.

0 likes
12 replies
Sinnbeck's avatar

Link to the demo project that works?

1 like
denabyte's avatar

@Sinnbeck as a newly-registered user I cant paste links, but you should be able to find it by googling "vanilophp / demo"

1 like
vincent15000's avatar

Have you declared the class ?

use Vanilo\Cart\Facades\Cart;
denabyte's avatar

@vincent15000 Where should this be declared ?The issue I have is related to using it in a blade template

1 like
kokoshneta's avatar

@denabyte A Blade template is still just a PHP file, and PHP needs to know where to look for a class. If you just use Cart::method(), PHP will assume the Cart class exists in your current namespace, which is not the case for the Vanilo Cart facade – that exists in the namespace \Vanilo\Cart\Facades.

The PHP docs explain in more details.

The use keyword should go at the top of the file (I don’t know if it actually has to, but it’s good practice). If you’re in a Blade template, you can do either of these two things:

@php
	use Vanilo\Cart\Facades\Cart;
@endphp

// code

@if (Cart::doesNotExist())
	// code
@endif

– or:

@if (\Vanilo\Cart\Facades\Cart::doesNotExist())
	// code
@endif

The first option tells PHP that whenever you use Cart in the current file, you’re really referring to \Vanilo\Cart\Facades\Cart. The second explicitly tells PHP which class you’re looking for when asking for it.

If you only refer to the Cart class once, the second version is sometimes easier, but the first is cleaner and easier to read – and also a lot easier if you refer to the Cart class many times in the file.

1 like
vincent15000's avatar

@denabyte Oh sorry ... I didn't noticed .... You can try with the complete path.

Vanilo\Cart\Facades\Cart::isNotEmpty()
denabyte's avatar

@Sinnbeck @kokoshneta 's solution of declaring use Vanilo\Cart\Facades\Cart; in the blade template does work, but it doesnt feel like that should be the proper way to do it. Especially considering the demo site doesn't use this approach :/ But, credit where it's due - this does let me use the Cart facade and the methods I have in the view so far. So thank you for that kokoshneta.

denabyte's avatar

@Sinnbeck that seems to have done the trick, as other views are now able to use the Cart facade once I registered the alias. Thank you ! I have no idea how this works in the demo, without it being in the aliases in config/app.php but hey it works. Once again thank you !

1 like
kokoshneta's avatar

@denabyte Of course, @sinnbeck is right: in Laravel, there is a third way, which I didn’t think about before, a global registry of class names to fully qualified paths.

This works the same way as the use operator, except it’s executed at an early stage when the whole Laravel system is booting up, which means it’s always available without needing to be used manually in each file.

1 like

Please or to participate in this conversation.