This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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 ?
You could register your object as a singleton https://github.com/octobercms/library/tree/master/src/Support
thanks Scott ! thats an idea... wondering if it is the same as building one big component in the end ?
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
totally agree with you philipptempel. But the question remains also for our own custom object.
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