Skip to content
Advertisement

PhpStorm: Change URL after changing DocumentRoot

I am currently running a Laravel 8 project and my DocumentRoot points to the public folder.

So if I type http://localhost/ in my browser it shows me the public folder because I have set up my webserver in this way.

In PhpStorm I added the folder as a Resource-Folder and hoped that it would “understand” that this is the new DocumentRoot.

see this image

However, I noticed that this is not the case because if I press Alt + 2 (to show in Firefox) it opens http://localhost/public/index.php instead of http://localhost/index.php

Does anybody know how to change the DocumentRoot for PhpStorm so debugging and opening pages in a browser actually works with another DocumentRoot on the webserver?

Advertisement

Answer

You need to configure the Deployment entry for that, because that’s where IDE takes URL info from when you are using “Open in Browser” kind of functionality.

  1. Settings/Preferences | Build, Execution, Deployment | Deployment

  2. Select your deployment entry. If you do not have any: create a new one (e.g. “In place” will work — no files will be copied) and configure it.

  3. Mark such an entry as Default for this project. If you do not have default entry then IDE will build URLs against the PhpStorm’s built-in simple web server.

  4. Now the important part: on the “Mappings” tab, create an additional mapping entry. It should link your local public folder to your web root in URL:

    • “Local Path” = PROJECT_ROOT/public
    • “Deployment Path” = /public/
    • “Web Path” = /

enter image description here

Now if you use “Open in Browser” on public/index.php file the IDE will open http://YOUR_DOMAIN/index.php URL and will omit the not needed public/ part from such URLs.


Please note that this will not help you much with debugging your PHP controllers (if you are using Laravel as designed).

Because it’s Laravel, you will be using controllers and “nice URLs”… and such mapping will do almost nothing for you. For example:

  • The file for handling http://localhost/user/profile URL will NOT be located at PROJECT_ROOT/public/user/profile file but most likely will be in PROJECT_ROOT/App/Http/Controllers/User.php or alike).
  • You may have more than one action inside 1 controller file.
  • You would need to create a new mapping for every URL (which is not convenient, at all).
  • And the MOST important: if you will be calling your controllers this way then you will simply see PHP errors .. as you will be bypassing the whole framework bootstrap code.

If you want to initiate the debugger from inside the IDE you will need to create Run/Debug Configuration of the appropriate type (e.g. “PHP Web Page”), specify the URL there and then use it with Run or Debug buttons:

Alternatively just use plain-old bookmarks in your browser of choice and initiate debugging from there (e.g. Xdebug helper extension: available for every modern browser) using what JetBrains called Zero Config approach: https://www.jetbrains.com/help/phpstorm/zero-configuration-debugging.html

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement