Laravel 5, I want to change the location of a config file.

Hi,

I want to change the location of the database.php file which is a configuration file, I want to put it in another location in the system.

How can I achieve that?
Ramy MohsenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
To quote the Laravel docs...
All of the configuration files for the Laravel framework are stored in the config directory.
 The configuration includes the database.  Since Laravel favors convention over configuration, we don't usually change things like this.  Maybe if you can tell us why you want to change the location we can suggest a useful approach.
0
Ramy MohsenAuthor Commented:
My case is that I want users to subscribe to some service that I will provide.
When a user subscribe to a service, a new Database will be created to store his data.
So I will provide SAAS, many databases, with the same code.

So for each user there will be a config file congaing the connection credentials for his database,  and I will tell laravel to use one of these config files based on some data saved related his user account in a central DB.


I think laravel must be more flexible than obligating me to use some approach.
0
Ray PaseurCommented:
Laravel has a way to use different database connections.  It's part of the "basic database usage" for the Eloquent ORM.  You specify the database connection by name.  To quote the Eloquent docs
... you may define a connection property to override the name of the database connection that should be used when utilizing the model.
See if this helps make sense of it:
http://laravel.com/docs/5.0/database#accessing-connections
http://laravel.com/docs/5.0/eloquent#basic-usage
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Ramy MohsenAuthor Commented:
Thank you for your help.

But, I still don't want to implement this due to many reasons:

1- I don't want to open the database.php config file manually to add a new connection for each new database created( which means for each new subscription),
nor automatically doing that using PHP, because I think that opening file, reading its contents then changing them by addition will not be efficient, and this may occur while another request is being done to the site, which may reqiure use of the file is will be edited in that time.

2- If there is a good way to add these connections automatically, then I think that laravel may load the connections array and then
use the connection that I require from inside it, which means memory load in case that I have 1000 connection ( Note, My project is supposed to get more subscriptions than 1000
So my solution is to find some way to create new database.php file for each database, these files will be but in a folder named 'databases/{subscriber_name}'
So I will be able to access only the required connection credentials if I could tell laravel to use the needed path which will contain a variable portion.
0
Ray PaseurCommented:
I have some experience with Laravel and I can tell you that if you do things "the Laravel way" it will be a lot easier than if you start with the premise that Laravel cannot do what you want and must be modified before you can start your project.  Let's look at the essential knowledge elements you need.

1. You need separate database connection information for each client.  I would not recommend using the database.php config file for this task.  Adding a thousand sets of credentials to the file is a trivial task (efficiency does not matter when adding, and file collisions are readily preventable).  But doing a thousand-row lookup every time you want to use a database connection feels awkward to me.  Better to establish the database credentials at the time your client registers for your site, and store this information as part of the client's permanent record.  Then, by the simple act of the client login, you will have the correct database credentials at your fingertips.

2. Your app must authenticate each client who makes a request to the site.  Laravel offers the Active Record design pattern that gives you access to a complete data model for the client.  During the authentication process you will be able to detect the name and access credentials of the database that you assigned to this client during initial registration.  You will be able to cache the database information (along with the authentication credentials) for the duration of the client's session.

3. You've designed your app to use a separate database connection for each client.  Please see the previous comment about using a connection property to override the name of the database.

Summary: If you can't see a way to use the Laravel features for your needs, maybe Laravel is not the right framework for your work.  But that said, why not join LaraCasts.com and pose your design question to Jeffrey Way and his team?  LaraCasts is almost free -- or at most, it is embarrassingly inexpensive for all the up-to-date knowledge you will find there.  A multi-user web site sounds like something that the Laravel community has considered before.  They might have some good ideas for you.
1
Ramy MohsenAuthor Commented:
Thank you for your help.

About my actual need, I found how to do it:

- in the file : app/http/kernel.php,  I added the following :

protected $bootstrappers = [
    'Illuminate\Foundation\Bootstrap\DetectEnvironment',
    'Illuminate\Foundation\Bootstrap\LoadConfiguration',
    'Illuminate\Foundation\Bootstrap\ConfigureLogging',
    'Illuminate\Foundation\Bootstrap\HandleExceptions',
    'Illuminate\Foundation\Bootstrap\RegisterFacades',
    'Illuminate\Foundation\Bootstrap\RegisterProviders',
    'Illuminate\Foundation\Bootstrap\BootProviders',
   'App\CustomLoadConfiguration',
];

- I then created a new class named "CustomLoadConfiguration" which extend laravel boostrap class "LoadConfiguration" to override the getConfigurationFiles method in it as follows:

<?php
/*
 * Hacking laravel core
 * Class CustomLoadConfiguration wade for modifying database.php file location
 */

namespace App;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
use Symfony\Component\Finder\Finder;
use Illuminate\Contracts\Foundation\Application;

class CustomLoadConfiguration extends LoadConfiguration
{

    protected function getConfigurationFiles( Application $app)
    {
        $files = [];

        foreach (Finder::create()->files()->name('*.php')->in($app->configPath()) as $file) {
            $nesting = $this->getConfigurationNesting($file);

            $files[$nesting . basename($file->getRealPath(), '.php')] = $file->getRealPath();
        }


        $files['database'] = new path;


        return $files;
    }

}

Open in new window


this solved my issue and it's now working fine, but I want to know your opinion implementing this code.

I posted my question in laracasts as you suggested to get more opinions.
And thank you again for your help.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
I don't have any way to test the code, but if it's working fine for you, that's a very good sign!
0
Ramy MohsenAuthor Commented:
Thanks, but I'm not asking that you test it, because I tested it and it's working fine.

I'm asking about your opinion of that solution, and if you think it's not good to take this way for any consideration.

Thanks again :)
0
Ray PaseurCommented:
I don't really have anything else to add.  I think the LaraCasts team will be a good source of ideas.

Best of luck with the project, ~Ray
0
Ramy MohsenAuthor Commented:
Because I deeply investigated the problem and  was able to solve it. then I wrote my solution here and hence accepted it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Laravel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.