0

So we have been developing a content management system on laravel and everything is finished. Our plan was to ship it as downloadable solution for customers but now things have changed and we have decided that we will provide it as a SAAS. As it will be used by multiple customers and they will be either using their own domain or our subdomain, I figured out the following solution.

  1. Detect the domain or sub domain and query it against the database
  2. Get the relevant id from a table storing the domains
  3. Set the id in a session or a global variable
  4. Get the settings of the site using that key
  5. For all the subsequent queries to database, add a default where('domain_id',$idOfDomain) so that to avoid mixing up of the data by users of different clients

I don't know if that's the best approach but currently i feel like it's the solution. Now the problem is that I don't know

  1. How to load the domain id and settings from database before code in controller is executed. Just like the configuration file in laravel
  2. I tried to set a default key on models but it was not much successful and I want to know how to set and retrieve domain id in all models as editing all the codes in controller will be too much time consuming

Besides if someone has better and secure solution then the above one then please share it.

Thanks :)

3
  • it's my third question without answer :( Commented Mar 20, 2016 at 22:16
  • 1
    Did you solve it? Commented Jul 18, 2021 at 10:51
  • @sta Yes, unfortunately at that time I didn't know the proper terminology so it was hard finding a solution. Such a system is called "Multi Tenant" system. I used a package called landlord/multitenancy with some custom middlewares. You can check spatie.be/docs/laravel-multitenancy/v2/introduction , tenancyforlaravel.com and tenancy.dev for latest packages. Commented Jul 18, 2021 at 15:45

1 Answer 1

1

Have a look at the package delatbabel / site-config, it sounds like it does most of what you want. Search on packagist or github. It will pre populate the laravel config data based on database contents, depending on what domain The browser is pointing to.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks :) I think it's your package. So can you give me a bit more details? I checked the page on github and it do a lot of what I want like loading config into models but how can I configure it per domain basis.
I have added a fair bit of documentation in the README.md file in that package. I have also just recently added some example seeders in database/seeds in the source code of the package, you can check those out yourself. These show how to create configuration on a per domain basis.
If you have further questions or comments about the package then probably worth raising those on github as issues in the package itself. The only thing that I don't have is this: "For all the subsequent queries to database, add a default where('domain_id',$idOfDomain)" -- that's the sort of thing that's probably best done in middleware in your application, or possibly in a trait that you can attach to any relevant models.
Yeah the default query is a separate thing, gonna try the library thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.