How to set env with laravel artisan to have two different database connections (local/remote)?
Asked Answered
Z

4

9

I looking for setup a multi-environment project using Laravel3 but I dont understand the command to set the environment.

I see here: http://laravel.com/docs/artisan/commands The command is:

php artisan foo --env=local

I already used artisan and bob with success, what I can't undertand the foo, I try to change to my project name but always the same output: "Sorry, I can't find that task."

If I try: php artisan --env=local

That will return: "You forgot to provide the task name."

Anybody can help? Thanks for your time.

[edit] With the answers now I can understand better and improve my question:

I have a project with those folders created: http://d.pr/i/5nZS With that in mind, I need to set my local env as development and production as production. So, I can do that with any variation of the command "php artisan --env=local" or I need to add on my public/.htaccess "SetEnv LARAVEL_ENV development"?

Thanks again.

Zetland answered 15/1, 2013 at 17:45 Comment(0)
Z
1

Here, how I solved my question:

First I didn't need the command php artisan migrate --env=local, I just need set on my virtualhost: SetEnv LARAVEL_ENV development.

Second, as William Cahill-Manley say, I need to work on application/paths.php, the $environments. I've used it before but the wrong way. In my case, I solve with that:

$environments = array(
    'development' => array('http://localhost/project*', '*project/*'),
    'production' => array('http://project.com', 'http://*.project.com')
);

My problem was because my code before was like that:

$environments = array(
    'development' => array('http://localhost/project*', '*project*'),
    'production' => array('http://project.com', 'http://*.project.com')
);

And because the second element of development array, in the production server always will be in development. Thats because the url on development be http://project/ and on production be http://project.com/ or http://user.project.com/

See, the project will force in all envonriments be development by the asterisk/wildcard.

Zetland answered 20/1, 2013 at 18:1 Comment(0)
A
14

"Foo" is whatever command you want to run. E.g. for migrations:

php artisan migrate --env=local

Another thing you can do is add your computers hostname to this array

For example, if my local computer name is 'Effinity.local' I could do

$environments = array(
    'local' => array('http://localhost*', 'Effinity.local'),
);

Then you do not need to specify the environment, just:

php artisan migrate

Hope that helps.

Apiarist answered 15/1, 2013 at 17:51 Comment(1)
So, on my local I need set: "$ php artisan migrate --env=development" And on production server: "$ php artisan migrate --env=production" With that and my array like this: gist.github.com/4557659 And also with my config folders configured like that: d.pr/i/5nZS This will work? I tried but still seems not work, what I also tried is with "SetEnv LARAVEL_ENV development" what's almost working but I need a little more time to try again. Thanks for the answer.Zetland
Z
1

Here, how I solved my question:

First I didn't need the command php artisan migrate --env=local, I just need set on my virtualhost: SetEnv LARAVEL_ENV development.

Second, as William Cahill-Manley say, I need to work on application/paths.php, the $environments. I've used it before but the wrong way. In my case, I solve with that:

$environments = array(
    'development' => array('http://localhost/project*', '*project/*'),
    'production' => array('http://project.com', 'http://*.project.com')
);

My problem was because my code before was like that:

$environments = array(
    'development' => array('http://localhost/project*', '*project*'),
    'production' => array('http://project.com', 'http://*.project.com')
);

And because the second element of development array, in the production server always will be in development. Thats because the url on development be http://project/ and on production be http://project.com/ or http://user.project.com/

See, the project will force in all envonriments be development by the asterisk/wildcard.

Zetland answered 20/1, 2013 at 18:1 Comment(0)
P
0

Foo is a task name , try to create this file in the task folder, there are some other task which is predefine for example migration etc.

<?php 
    class foo_task {
    public function run(){
            echo 'this is foo';
        }
    } 
?>

then when you run the command,it will run the code inside the run function.

php artisan foo --env=local
Pessimist answered 17/1, 2013 at 0:37 Comment(1)
Yeah, thats what I imaginate before but with "php artisan foo --env=local" this will run the task just when the env is local, right? What I really need is set the whole project as a local or development and with that make my config folder (d.pr/i/5nZS) work and connect the correct database host.Zetland
C
0

I'd recommend setting up a name-based virtual host for your web application first:

<VirtualHost *:80>
    ServerAdmin postmaster@localhost
    DocumentRoot "__PATH TO YOUR SERVER ROOT___/project/public/"
    ServerName project.dev
    ErrorLog "logs/project-error.log"
    CustomLog "logs/project-access.log" combined
</VirtualHost>

Then add project.dev to your hosts file /private/etc/hosts as follows:

127.0.0.1 project.dev

Don't forget to flush the DNS cache afterwards:

$ dscacheutil -flushcache

Then amend the $environments array found in [project root]/path.php (the applications/path.php file you mentioned does not exist) back to what it was. The original *.dev wildcard will pick up the .dev at the end of the url you provided.

$environments = array(

    'local' => array('http://localhost*', '*.dev'),

);

Then create a directory within applications/config/ called local, place within the new directory a file called application.php. The configuration given in this file will override the configuration set by the corresponding configuration files and settings/values given in the parent of the local directory you have created.

Cooee answered 5/2, 2013 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.