Search code examples
phplaravelgitdevelopment-environmentproduction-environment

Managing Two Different environments Laravel


Hello there i have two different environments one is production and one is dev and i am using laravel and git as version control the main issue between them is path of files so for example

at my dev enviro i can access an image stored in /public/categories/icons/imagename by doing this

  <img src="{{$current_category->icon}}">

the icon path in my local db is this /category/icon/1592520430.png so as you can see i don't have to put public before it so laravel is going to /public/ and then it looks for my path which is /category/...

but in my production enviro

if i want to access an image i will do the same like this

  <img src="{{$current_category->icon}}">

but the difference here is that this will output to /public/category/icon/1592520430.png as you can see in my dev the path is accessible without the public word but in my production i have made some changes to my controller to put the word public before the path then i can access it because it already has the public

and this is causing some problems in git because the dev enviro is different than the production enviro so that when ever i face a problem i should go to my dev and fix it there and after knowing the problem i go to production and fix it there and then push production to master

i have thought of some options but i don't know which one is the rightest or if there is another way to fix this problem

1 - make my dev enviro similar to production

2 - make production similar to dev

but maybe both of them are wrong so please tell me how should i handle this problem and keep in mind that my project in production is exactly as mine in dev (i mean the project structure and files location)


Solution

  • To access the image in the folder 'public/categories/icons/', we can use

    public_path('categories/icons/');
    

    This function can't be used to fetch image in the folder '/category/icon/1592520430.png'

    Recommedation:

    Please, use the same code and folders in local and production.

    Set up ENV file with APP_ENV=DEVELOP and APP_ENV=PRODUCTION

    Wherever needed, call the env() helper or App::environment() function to access it.