This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

chris10207
chris10207

Is there a way do share instance of any object between components ? right now for example, if you are using the component Session, it will provide a user object to the page. in your custom component, if you need the user instance, you will have to perform an Auth::check() and getUser() which will perform unnecessary database requests.

Another way to look at the question would be how to design and architecture our components proficiently ?

Scott
chris10207
chris10207

thanks Scott ! thats an idea... wondering if it is the same as building one big component in the end ?

philipptempel
philipptempel

As a matter of fact this seems to be a flaw in the code (and I just looked at the source code, didn't even set up a test bench). The query against the database (to check for existence of the user row) should be performed only once and from then on retrieved from a cached result).

Opened a ticket on GH

Last updated

chris10207
chris10207

totally agree with you philipptempel. But the question remains also for our own custom object.

philipptempel
philipptempel

That is correct, Chris. The only way I can think of sharing anything between components is to register it as a singleton with the Container. For a plugin, this can be done in the Plugin::boot() method. I don't have the exact syntax here because I'm on mobile, but Rainlab.User plugin does it an its code can be viewed on GitHub. For singletons from your app and not a plugin i.e., even "more global" singletons, it should b registered in config/services.php and the configured ServiceProvider a Facade created. If the October docs don't help I'm pretty sure there's something over on Laravel's docs.

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.