userfrosting / UserFrosting

Modern PHP user login and management framework
https://www.userfrosting.com
Other
1.64k stars 366 forks source link

Load custom "sprinkles.json" file #813

Closed mgpro94 closed 11 months ago

mgpro94 commented 7 years ago

@alexweissman Follow the conversation here: https://forums.userfrosting.com/t/can-i-load-different-sprinkle-for-each-domain/97

we agreed to add a way to load custom "sprinkles.json"..

as i suggested:

file: public/index.php

switch($_S ERVER[‘HTTP_HOST’]) {
      case “domain_1.com”:        $sprinkles_file = “sprinkles_domain1.json”;       break;
      case “domain_2.com”:        $sprinkles_file = “sprinkles_domain2.json”;       break;
      default:                    $sprinkles_file = “sprinkles.json”;               break;
}

$uf = new UserFrosting($sprinkles_file);

file: app/system/UserFrosting.php

class UserFrosting
{
      private $sprinkles_file;

      public function __construct($sprinkles_file = "sprinkles.json")
      {
            $this->sprinkles_file = $sprinkles_file;
            /* old code */
      }

      public function setupSprinkles($isWeb = true)
      {
            /* old code */

            $schemaPath = \UserFrosting\APP_DIR . $this->sprinkles_file;

            /* old code */
      }
}
Silic0nS0ldier commented 6 years ago

An interesting scenario. Would require more than just conditional loading of sprinkles.json though. At least if this functionality is to land in UF in any reliable fashion.

Off the top of my head, for this to be done we need:

  1. A way for PHP and JS to know the possible sprinkle configurations.
  2. Each configuration to have its assets built, and placed in some folder inside public.
  3. The generated asset URLs need to consider the extra folder.
alexweissman commented 6 years ago

I think, at the very least, we should remove the hardcoded sprinkles.json string and make it a constant defined in defines.php.

lcharette commented 6 years ago

+1

The constants can be overwritten by sprinkle defined one? But then I guess it would be loaded after that info is require. Or maybe it could it be overwrittable by the env settings?

alexweissman commented 6 years ago

Nothing that fancy. People who need to change the default sprinkles.json will likely be manually instantiating UserFrosting anyway. So, we just need to factor the default name as a constant, and add a UserFrosting::setSprinkleSchemaFile method, or something like that.

Could even use loaders to support JSON or YAML.

lcharette commented 6 years ago

It could also be changed using Bakery somehow...

mgpro94 commented 6 years ago

i think to give the option to change the default definition by UserFrosting::setSprinkleSchemaFile method would be the best

if you defined this by env settings or bakery this would be the same as static file name you should be able to change the file in run time

lcharette commented 6 years ago

Problem is if you have to edit app/system/UserFrosting.php, you are editing core code, which is bad. And we can't have it in a sprinkle, since it needs to be done before the sprinkles are actually loaded.

Nevertheless, your issue is different than the fact the sprinkle file is hardcoded. Having a different set of sprinkle depending of the domain is nonsense for me. If there's really a difference between the two domains, it should be handled inside a sprinkle. Even better, I would go for two different install, one for each domain, even if they have to share the same db. What is the reason you need to load a different sprinkle based on the domain name?

mgpro94 commented 6 years ago

i made two base sprinkles of my system (one is template and another is functionality) each domain has a different land page but with the same functions and management.. only differences is settings and minor changes on template

when this will scale up with more domain.. this will be hard for me to update every host..

if i can manage with sprinkles will be loaded by domain (or any other way) in the index.php file (and this is before the sprinkles loaded) this will be much more easier for me

I dont see why my example (in the post) is wrong or not good ?

alexweissman commented 6 years ago

I actually did originally envision a multi-tenant situation (what @mgpro94 is describing) as one possible use case for Sprinkles. In fact, I actually designed the Sprinkle system based on some "proto-sprinkles" I was using in a multi-tenant project of my own, where each site had a separate "proto-sprinkle" in the same installation.

I ultimately ditched that approach in favor of using a common Sprinkle shared among separate installations of UF, but I don't think @mgpro94's use case is so far-fetched.

lcharette commented 6 years ago

Actually, this could also be resolve and/or be in conflict with #747 & #728.

lcharette commented 11 months ago

This is now possible with UF5. Simply create two Recipe, and load the correct one in index/bakery.