在PHPStorm中将一个目录路径映射到另一个目录路径 [英] Map one directory path to another in PHPStorm

查看:413
本文介绍了在PHPStorm中将一个目录路径映射到另一个目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将JetBrains PHPStorm与Vagrant一​​起使用.主机OS(运行PHPStorm的主机)是Windows,而来宾OS Vagrant设置的是Ubuntu Linux.该应用程序位于VM中的/var/www/,但是在主机上,该代码位于C:\code\.

I'm using JetBrains PHPStorm with Vagrant. The host OS (where PHPStorm runs) is Windows, while the guest OS Vagrant sets up is Ubuntu Linux. The app resides at /var/www/ in the VM, but on the host that code is at C:\code\.

因此,在PHPStorm中,我收到有关未解析目录的警告,实际上该路径是正确的,并且可以直接映射到C:\code.

Therefore, in PHPStorm, I get warnings about unresolved directories, when in fact, the path is correct, and can be mapped directly to C:\code.

如何告诉PHPStorm项目中以/var/www/开头的代码中的路径实际映射到C:\code\?我希望再次进行路径检查.

How can I tell PHPStorm that paths in the code that start with /var/www/ in my project actually map to C:\code\? I'd like path inspections to work again.

推荐答案

我从来没有找到解决方案,但是我认为我会发表一个答案,因为我认为这不是构建包括的好方法.路径放在首位.

I never found a solution to this, but I thought I would post an answer, as I've since decided this isn't a great way to build include paths in the first place.

在阅读Symfony框架(以及Composer生成的自动加载器)时,我遇到了一种更好的方法:

I came across a much better approach when reading through the Symfony framework (and Composer's generated autoloader):

<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInitd1d12712f9bc55fdf8d19536f1f9dd88::getLoader();

此处,PHP魔术常数__DIR__用作路径的起点.从魔术常数手册页:

Here, the PHP magic constant __DIR__ is used as a starting point for the path. From the magic constant manual page:

__DIR__文件的目录.如果在include中使用,则返回包含文件的目录.这等效于dirname(__FILE__).除非它是根目录,否则该目录名称不带斜杠.

__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.

此方法有很多好处:

  • 它是便携式的-可以在PHP可以在其上运行的任何操作系统上运行
  • 这是灵活的/动态的-您可以移动文件,而__DIR__常量将反映新路径而无需任何代码更改.
  • 这很熟悉-其他开发人员很可能会认识到这种方法,因为它使用了一种本地语言功能,而且他们以前可能会遇到过这种方法.
  • It's portable - it will work on any operating system that PHP will run on
  • It's flexible/dynamic - you can move your files around and the __DIR__ constant will reflect the new path without any code changes.
  • It's familiar - other developers are more likely to recognise this approach, as it uses a native language feature, and they are likely to have come across it before.

这篇关于在PHPStorm中将一个目录路径映射到另一个目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆