Proper way of registering ServiceProviders in Sage 10?

Hi,

I’ve been using my own services in my themes for quite some time. However I am unsure about how to properly register my services.

I read the discussion at Confusion about Sage 10 Service Providers - #3 by philipp and I can make my services and service providers visible to my theme.

However the register() or boot() methods in my service provider are never called no matter if I register it in composer.json or config/app.php.

I’ve been just initializing all services using an ‘init’ action and creating them using app()->make(MyService::class) but that feels rather wrong and incomplete if there is a proper way using a service provider.

What am I doing wrong?

Thanks and best regards,
Flei

1 Like

You can add the provider in config/app.php. If that file is not there, you need to publish it.

Hello @tombro , thanks, I am aware of that but as mentioned I still wasn’t able to have the register() or boot() methods of my service provider to being called. Any idea?

perhaps your providers are cached? try running wp acorn optimize:clear

1 Like

@flei
Something similar happened to me, it drove me crazy, because none of the clear ones responded to me (wp acorn optimize:clear, etc) so I manually deleted all the content of the cache folder located in wp-content/cache (in bedrock web /app/cache ) and it worked. Due to lack of time I was not able to review much why Acorn’s clear cache was not working but it worked.

Seen weird issues with this as well so nowadays we just define all service provider explicitly in an AggregateServiceProvider.

<?php

namespace App\Providers;

use Illuminate\Support\AggregateServiceProvider;

class ThemeServiceProvider extends AggregateServiceProvider
{
    protected $providers = [
        \App\Providers\BlockPatternServiceProvider::class,
        \App\Providers\BlockServiceProvider::class,
        \App\Providers\ComponentServiceProvider::class,
        \App\Providers\SageServiceProvider::class,
        \App\Providers\PerformanceServiceProvider::class,
        \App\Providers\AsyncLoaderServiceProvider::class,
        \Spatie\Csp\CspServiceProvider::class,
        \App\Providers\CacheControlServiceProvider::class,
        \App\Providers\ContentSecurityPolicyServiceProvider::class,
        \Spatie\GoogleFonts\GoogleFontsServiceProvider::class,
        \Log1x\SageSvg\SageSvgServiceProvider::class,
        \App\Providers\WooCommerceServiceProvider::class,
        \Genero\Sage\CacheTags\CacheTagsServiceProvider::class,
        \Genero\Sage\WooCommerce\WooCommerceServiceProvider::class,
    ];
}

I typically use config/app.php but with Acorn v5, doing it in functions.php like my example here will probably be the best way moving forward.

This great but I think registering my services in the ThemeServiceProvider, keeps my code clean and helps me avoid cluttering functions.php which is among the a million reasons I choose to develop in sage.

sage10 make this very straight forward once you have a service provider just register it in the register method like this:
/**
* Register any application services.
*
* @return void
*/
public function register()
{
parent::register();
$this->app->register(TransactionsServiceProvider::class);
}

This method works but ins sage10 you will get the error Target class [sage.view] does not exist! which becomes a headache to solve since in Sage, view bindings are typically handled by Roots\Acorn\Sage\SageServiceProvider, am definitely sure you would not want to mess with that… it is the same problem @AntoineCabrol faced here Target class [sage.view] does not exist kindly refer to my fix recomendation here: Emmanuel-Odero reply to @Log1x

Can you please help me out here?
I keep getting the sage.view doesn’t exist error and really don’t know what to change or set-up anymore to fix this.

1 Like

Hello,
Are you using service providers, here are a couple of things to mark in your checklist:

  1. Ensure sage and bud are properly installed, run yarn install, and yarn dev or yarn build.
  2. Ensure you have registered your service providers ThemeServiceProvider

Thank you so much for this quick reply.
The yarn install and yarn dev/build are working properly fine.
Wordpress installed with Ubuntu works great as well with a standard theme.
When I activated Sage, I can still access the wp-admin of my localhost but I can’t reach my page because of Sage.view error.

I saw your previous post on how to register all the service providers. Thank you for sharing that. Not sure what service providers to put in my ThemeServiceProvider.php file tho? Can you tell me?

1 Like

mhhm… I would have loved if you provided a screenshot or a little detailed information of what you trying to achieve and the what is the stumbling block, i.e does the issue have to do with component, composers etc, just alittle of background.

I will share the error message here and my code in the next post.
Thanks for checking it out.



Ooh thank you I see the error is from vendor packages, kindly run composer install in you sage theme directory

Also publish the vendor packages:
wp acorn vendor:publish --tag="config"
You can also clear cache:
wp acorn cache:clear

Showing you the latests below

run yarn build, and yarn dev for hot reloads