使用PhpStorm创建和配置Laravel项目

5

我对整个Homestead的东西以及它与IDE的相关性感到兴奋。假设我已经在~/Developer/PhpStorm中安装了我的PhpStorm。Homestead位于~/Developer/Homestead。这是我在Homestead中YAML文件的外观:

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Developer/Homestead/workspace
      to: /home/vagrant/Code

sites:
    - map: helloworld.app
      to: /home/vagrant/Code/Laravel/public

variables:
    - key: APP_ENV
      value: local

所以,你可以看到在Homestead目录下有一个工作区文件夹。我还有另一个目录:~/Developer/workspace/PHP,我计划将我的项目存储在这里,而不是在Homestead文件夹中。

我在PhpStorm中安装了Laravel插件。为了使PhpStorm中的Laravel插件正常工作,在PhpStorm中需要这个生成的文件。我的问题是:

  1. 我应该把_ide_helper.php文件放在哪里,才能使PhpStorm与Laravel正常工作?我应该把它粘贴到每个项目中还是只需要放一次就好了?
  2. 在YAML sites: map字段中,我是否需要为每个想要启动的项目编写不同的应用程序名称?
  3. 如何创建一个新的Laravel类型项目。因为当我在PhpStorm中创建一个新项目时,我可以选择不同的类型-我是否也应该在其中列出Laravel?因为现在,当我创建一个新的PHP项目时,它完全是空的。我想Laravel项目应该有一些架构和生成的文件。
  4. 请简要解释一下所有关于Laravel + Homestead和Vagrant的内容以及如何控制我的项目,因为我变得非常沮丧,我不得不很快在我的学士项目中使用这些技术。

我可以向您推荐使用 PhpStorm 的 Laravel 插件,而不是使用 helper:这样做更容易,并且它的创建者不断改进插件。https://plugins.jetbrains.com/plugin/7532?pr=phpStorm - Kootli
@Kootli - 那已经是我安装的了。但是还需要助手。这甚至在插件的安装指南中提到了。 :) - Milkncookiez
关于你的第一个问题:只需将辅助文件复制到项目根目录并同步即可。 - Kootli
2个回答

5
  1. You shouldn't need to put the _ide_helper.php file anywhere manually, it is automatically generated by the Artisan command. For each new project, include the IDE helper in that project's composer.json file:

    "require-dev": {
        "barryvdh/laravel-ide-helper": "1.*"
    }
    

    Add the service provider to the providers array in the config.php file of your Laravel project:

    'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider'
    

    Then use Artisan to generate the _ide_helper.php file for the project (run this command from the terminal in the root of your Laravel project directory):

    php artisan ide-helper:generate
    

    This is all paraphrased from the instructions on the IDE Helper GitHub page. I would recommend following those instructions to also set up your composer.json file to auto-generate a new _ide_helper.php whenever composer updates.


  1. Yes. For each individual Laravel project, you'll need to update your sites mapping in the YAML file. For my projects, I use this scheme (note that you are mapping to the location relative to your Vagrant box):

    sites:
        - map: local.project.example.com
          to: /home/vagrant/Projects/project/public
    

    Then in your Homestead directory, run:

    vagrant provision
    

    You will also need to update your hosts file to point to the Vagrant box.

    sudo nano /etc/hosts
    

    Add the line:

    127.0.0.1           local.project.example.com
    

    Now you should be able to access this Laravel project by hitting: local.project.example.com:8000 in your web browser.


  1. Assuming you followed the Laravel installation instructions, the easiest way is to use the Laravel command in the terminal. To create a new Laravel project called "blog", navigate to ~/Developer/workspace/PHP and run the command:

    laravel new blog
    
我希望以上答案能为你指明正确的方向。最重要的是仔细阅读 Laravel 文档,因为它覆盖了我刚刚讲到的所有内容,但是讲解得更加详细。

0

感谢Mike Andersen,你让我找到了正确的方向,但是你的解决方案1对我不起作用(使用Laravel 5)。

在修改composer.json文件后,您必须运行"composer update"。但是,当我这样做时,我遇到了这个错误:

barryvdh/laravel-ide-helper v1.2.1 requires phpdocumentor/reflection-docblock dev-master#6d705c1a0f9e2a6d73d2e9ec0e538b9dfaf4315f -> no matching package found.

我有另一种解决方案:

  1. 从composer.json文件的require数组中删除关于"barryvdh/laravel-ide-helper"的行。
  2. 运行下一行:composer require barryvdh/laravel-ide-helper

然后,您可以按照Mike Andersen提出的步骤进行操作:

  1. 将服务提供者添加到Laravel项目的config.php文件中的providers数组中:

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider

  2. 然后使用Artisan为项目生成_ide_helper.php文件(从Laravel项目目录的终端运行此命令):

    php artisan ide-helper:generate

  3. 在phpStorm中打开您的项目,然后转到:File|Synchronize

这样,您就可以使用barryvdh/laravel-ide-helper扩展的最新版本更新您的Laravel项目了。

(更多信息:https://github.com/barryvdh/laravel-ide-helper


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接