Search code examples
localhostlaravel-8absolute-path

__DIR__ only returns part of the path in Laravel - missing one folder


I am using Laradock/Docker with apache2 webserver.

localhost is pointing to /Users/paul/Sites (normal on Mac Monterey)

I have a laravel 8 project which is located on /Users/paul/Sites/ProjectX.

In /Users/paul/Sites/ProjectX/index.php, I have echo DIR;

In browser, localhost/ProjectX returns /var/www/ProjectX. Why is the "Sites" folder missing in the DIR constant? Is that normal?

I am pulling my hair off... this seems so trivial but I can not find the aqnswer... please help.


Solution

  • This is working exactly as expected.

    __DIR__ is meant to return the absolute directory path of the currently running script.

    Laradock makes use of bind mounts to mount your "app code path" (/Users/paul/Sites/ProjectX) on the host machine into the docker container at this "app code path" (/var/www/ProjectX). This allows you to preview your app code changes immediately rather than having to copy over source code on each change/edit.

    docker-compose.yml

    # ...
        workspace:
          build:
            context: ./workspace
            args:
    # ...
          volumes:
            - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
    

    With that in mind, it's evident that the two environments have separate file systems.

    When you make an HTTP request in your browser on the host machine, that HTTP request will be "forwarded" to a docker container listening on that specific port. I.e:

    # ...
          ports:
            - "${APACHE_HOST_HTTP_PORT}:80"
            - "${APACHE_HOST_HTTPS_PORT}:443"
    # ...
    

    Understanding that your HTTP request will be handled by a particular docker container (i.e. Apache/Nginx) rather than the host machine, performing an echo __DIR__; will result in a script's absolute mounted directory path based on the docker container's file system.