搭建Laravel项目集成开发环境简明教程

作者: 人可工作室 分类: 开发体验笔记,更新… 发布时间: 2018-05-03 09:12

phpRE+Composer+PhpStorm+Laravel

安装与配置

 

既然人生苦短,我们还是尽量不要浪费大家的宝贵时光。你可能体验了“著名的WordPress五分钟建站”,下面你即将体验到的是从没有搭建PHP运行环境的干净计算机上(当然搭建了也无妨)“5分钟一键安装创建Laravel项目”。先在官网下载phpRE完整一键安装包phpRE_setup.exe,再“下一步下一步傻瓜安装”成功后就见到“Laravel欢迎界面”了。

接下来为你揭开“5分钟一键安装创建Laravel项目”的神秘面纱……

安装Laravel项目开发运行环境方法步骤(要点):

一步下载安装phpRE→二步编辑PATH环境变量设置好两个全局搜索路径→三步初始化PhpStorm与Composerl勾搭配置→四步安装Laravel插件和Laravel IDE helper

安装完整版(最新版套餐):网盘下载phpRE(Ver1.3.8)提取码:zije

推荐自定义版本(手动解压完整版):

解压php+ apache+ mysql安装版直接快速下载phpRE2023.zip

(强烈推荐快速下载安装自定义版本)

官网下载:

httpd-2.4.57-win32-VS16.zip

php-8.2.9-Win32-vs16-x64.zip(线程安全版)

MySQL-5.7.27-win32.zip

phpMyAdmin-5.2.1-全语言

WordPress-6.2.2-zh_CN.zip

WampServer快速安装PHP开发环境

【开始前设置与变更】

必须确保php全局安装,事先将
d:\phpRE\php;d:\phpRE\php\ext;
路径放置于操作系统环境变量 PATH 里, Composer.phar依赖包已经在d:\phpRE\php子目录下,这样Composer 执行文件就会在你的系统中全局应用了,按上述操作步骤玩到这里,是完全可以在没有安装过phpRE的计算机里轻松勾搭配置PhpStorm+Composer弄出下面这个Composer管理器来的(在PhpStorm中跑Laravel框架之关键)

在PhpStorm中命令行处,先来弄个命令玩一把

composer config -g repo.packagist composer https://packagist.phpcomposer.com

切换国内服务器,一切准备就绪!等创建完laravel项目,试一下命令 composer install 来体验黑屏上飞一般的翻滚速度
更新Composer:v1.6.4

composer self-update

【安装laravel项目】

情有独钟的最现代最优雅的命令安装方法:在d:\phpRE\www中执行这个命令

composer global require "laravel/installer=~2.0"

使用独立安装命令会生成laravel.bat查看一下确保laravel.bat所在目录是否在C:\Users\计算机名\AppData\Roaming\Composer\vendor\bin\中,再
把 C:\Users\计算机名\AppData\Roaming\Composer\vendor\bin;(“计算机名”要修改成自己安装项目的计算机名) 路径放置于操作系统环境变量 PATH 里, 这样 laravel 执行文件就会在你的操作系统中被全局应用,否则将不能调用laravel命令或安装项目需要的包,也无法使用PhpStorm包管理器来方便更新或删除项目中的包。
[细节决定成败] 请注意:问题来了,在这个关键路径中如果找不到Composer\vendor请在上一级目录Composer中执行命令Composer install安装;如果找不到Composer\vendor\bin请在上一级目录Composer\vendor中执行命令composer global require “laravel/installer=~2.0″安装,必须确保laravel.bat文件在Composer\vendor\bin目录中才能往下走。
现在就先来编辑PATH系统环境变量,Win10建议用“新建→浏览”快捷法来编辑(实际上这个laravel.bat文件在没有执行过Laravel独立安装命令之前是不存在的,并且每执行一次命令都会重新生成一次laravel.bat文件。注意:执行Laravel独立安装命令的位置可以在“一键安装包”生成的test目录下,也可以删除test项目后,在用laravel new test重新实例化创建test项目之前执行)

接下来,就可以通过如下命令安装新的Laravel应用了,比如我们还是创建一个名为test的项目(Laravel实例化创建一个项目test):

laravel new test

使用这种方法创建Laravel应用要比直接使用Composer安装要快得多,并一次性生成官方最新版本laravel 框架及依赖包,用laravel new test实例化创建测试项目test后,开发过程中修改自定义配置内容composer.json,再用composr update按自定义配置内容更新,比方说:升级或降级laravel版本,添加Laravel IDE helper包等等(童鞋们玩不来Composer不怕得,可用composer list 显示命令列表慢慢来学着玩,先步子迈小点,步子大了会扯着蛋)。 除了用命令composr update更新外,还可以用PhpStorm包管理器GUI友好用户界面来方便更新或删除当前项目中的包:

如果非要指定版本的话还是用官方推荐的下面这个多功能安装方法相对方便一些。具体操作步骤是先在www\中用命令创建laravel项目,然后在开发过程中按项目实际需要来修改配置内容,再用命令

composer update

按自定义配置文件composer.json更新vendor第三方应用或包。
执行命令创建项目test

composer create-project laravel/laravel=5.5 test --prefer-dist

应用密钥

为确保项目安全起见,每次安装Laravel项目都会自动给出密钥,待开发完成上线时再重装生成新的密钥即可。因此,安装 Laravel 项目之后,下一件应该做的事就是将应用程序的密钥设置为随机字符串。通常来说,这个字符串长度为 32 个字符。密钥可以在 .env 环境文件中设置。前提是你要将 .env.example 文件重命名为 .env。如果应用程序密钥没有被设置,就不能确保你的用户会话和其它加密数据的安全。

神奇的“Laravel一键安装包”

Laravel带有一个名为Artisan的优秀的命令行工具,可以用它来生成框架代码和数据库架构,Artisan能够处理从数据库架构迁移到资源和配置管理的一切事情。安装laravel项目另类玩法就是直接使用一个现成的、已安装好依赖的官方Laravel包,在这里强调的是,此安装包非同一般的普通安装包,笔者用心良苦,为童鞋们编写好了phpRE_setup.exe一键安装程序,也可当做“Laravel一键安装包”来使用,phpRE_setup.exe程序预装了Composer1.6.4依赖包及用Laravel5.5稳定版制作的test纯净项目,下载phpRE_setup.exe程序直接安装后,在d:\phpRE\www\test中执行命令

php artisan key:generate

重新生成应用APP_KEY加密(预装Laravel包在制作时已自动加密,使用时再重新加密即可)。

启动Laravel项目

使用本地虚拟主机目录test下的public目录策略,可以非常好的保护源代码不被访问,这正是Laravel团队提出更安全更合理的方式。phpRE运行环境已在d:\phpRE\www\test\public下创建项目入口文件index.php,只需在地址栏输入虚拟主机域名就可以与Laravel见面了,一键安装就是这么简单!只需“下载phpRE_setup.exe→安装phpRE_setup.exe”两小步就一切OK!

【PhpStorm对laravel的支持】

接下来登场的IDE是PhpStorm官网下载安装激活、汉化、配色,在PhpStorm中初始化Composer安装配置好后,再安装Laravel插件及Laravel IDE Helper并调试开发环境。

PhpStorm激活方法

1.在注册码官网下获得最新版IntelliJ IDEA 注册码2018年10月14日失效
2.License server选项中输入: https://jetlicense.nss.im/
通过激活服务器激活

PhpStorm汉化方法

1.下载PhpStorm最新版汉化包
2.将resources_cn.jar文件复制回C:\Program Files\JetBrains\PhpStorm\lib目录重启即可,使用过程中如果发现部份功能异常情况,请卸载重装回非汉化原版

PhpStorm配色方案

1.下载PhpStorm配置文件,提供黑色白色两色调配色方案,白色优雅,黑色高大上。下载链接:黑色方案 白色方案
2.导入配色方案settings.jar(建议先把已下载的配置文件settings.jar存放在PhpStorm的config配置目录中,然后直接“导入设置”)

 

[ 提示 ]童鞋们嫌英文版不习惯的话,可下载安装汉化包,但汉化后不能保证PhpStorm的稳定性,其实,常用菜单没几个单词,记着记着就熟了,建议不汉化,还是使用原版的好;程序员很不爽的一件事就是长时间盯着屏幕太损伤眼睛,为了保护童鞋们“心灵的窗户”,建议下载安装“经典黑”配色方案,既高端、大气、上档次,又展示了PhpStorm友好界面风格,既然用上了高逼格的Laravel框架,IDE编辑器也给它弄得逼格高点。当然你要选“亮白清爽”配色也不会输给“经典黑”配色,萝卜白菜,各有所爱,随便选。重要的是老何我已经给童鞋们准备好了,点击链接就可以下载使用了。

Laravel Composer支持

Ctrl + Alt + S 搜索composer

初始化PhpStorm与Composerl勾搭配置如下图所示(使用一键安装包创建了test项目,这一步可以一开始启动PhpStorm选择rk项目后就搭配):

PhpStorm+Composer勾搭设置之后就可以很方便地直接在PhpStorm编辑器中使用Composer命令了,以后要安装和更新项目需要必须的composer依赖包都直接在PhpStorm中轻松完成了,请在命令末尾添加 -vvv 参数输出详细日志,例如:

composer install -vvv
composer update -vvv

【安装laravel项目扩展包】

〖法一〗
第一步:查找:上packagist搜索扩展包查询
第二步:安装:执行命令composer require 作者名/扩展包名
例如:composer require predis/predis
第三步:引入:require ‘vendor/autoload.php’
第四步:调用:按照文档实例化类调用方法
其中第三步只需要在php程序中require引入一次就可在下面的代码中调用依赖包了。
[提示] 这个方法也特别适合在非Laravel框架的PHP原生组件化开发项目中运用。
〖法二〗
PhpStorm+Composer勾搭设置之后可以方便你在编辑器中安装和更新composer包,只需在PhpStorm中选定当前项目,再点右键composer→Manage composer dependency打开包管理器来进行包管理 例如:predis/predis可以在PhpStorm中更新或移去

 

【安装Laravel IDE Helper及Laravel 插件】

使用Laravel插件和Laravel IDE helper能够更有效率的开发Laravel项目。成功安装Laravel插件和Laravel IDE helper后能够确保我们在使用PhpStorm开发Laravel项目时,得到最完整的辅助扩展功能。例如Laravel代码自动提醒、Composer支持、Artisan命令行支持以及额外的Blade语法支持等等。

安装Laravel IDE Helper

有时候你会发现Route::之类的没有代码提示或者自动补全,这时候需要安装使用composer命令安装 Laravel IDE Helper

composer require barryvdh/laravel-ide-helper
使用Artisan生成PHPDoc辅助文件

在安装Laravel IDE Helper之后,我们能够使用artisan生成PhpStorm 以及 Laravel用来提供代码补全和定位的PHPDoc辅助文件:在command line tool功能中开启对artisan的支持——>settings——>Tools | Command Line Tool Support——>artisan
这个工具基于Symfony Console,因此我们接下来需要提供artisan命令的路径:

保存之后, 我们即可在IDE内使用artisan命令时提供了对所有可以执行的artisan命令的自动补全。
用下面命令列出artisan命令用法:

php artisan list make

用下面命令生成必要的 PHPDoc信息:

php artisan ide-helper:generate

Laravel IDE Helper在每次改变服务或添加服务、控制器、模型以及视图的时候都要重新运行一下。Laravel IDE Helper的github主页上给出了一些运行建议。例如,在安装或更新Composer依赖之后,运行Laravel IDE Helper。另一种比较简便的方法就是使用PhpStorm软件的File Watchers文件监控,这可以自动化地在一些文件修改之后,重新生成Laravel IDE Helper文件,例如composer.json文件的自动修改。

安装Laravel Plugin

通过安装Laravel 插件来扩展编辑Laravel代码提示、调试功能ctrl+alt+s搜索plugin
在 Settings (Preferences) | Plugins 菜单下, 点击Browse repositories… 按钮,然后搜索“Laravel”,我们即可点击安装按钮进行安装

重启Phpstorm应用Laravel plugin到当前项目中
然后你再去写代码的时候就会提供controllers,views, routes, configuration, translations等的代码提示功能。

注:图中的rk目录改为test目录

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

25条评论
  • Daisy

    2019年7月22日 下午12:28

    Usually I don’t learn post on blogs, however I would like to say that this
    write-up very compelled me to check out and do it!
    Your writing taste has been amazed me. Thanks, quite great
    article.

  • Nathaniel

    2019年7月21日 下午11:57

    Hey there, I think your blog might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted to give you a quick
    heads up! Other then that, awesome blog!

  • Katja

    2019年7月21日 下午7:13

    Hi there! I know this is somewhat off topic but I
    was wondering which blog platform are you using
    for this website? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for
    another platform. I would be fantastic if you could point me
    in the direction of a good platform.

  • Niklas

    2019年7月21日 下午5:58

    It’s a shame you don’t have a donate button! I’d
    definitely donate to this superb blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google
    account. I look forward to brand new updates and will share this blog with my Facebook
    group. Chat soon!

  • Hwa

    2019年7月21日 上午2:32

    obviously like your website but you need to test
    the spelling on quite a few of your posts.
    Many of them are rife with spelling problems and I to find it very troublesome to tell the truth then again I will surely come back again.

  • Hattie

    2019年7月20日 上午9:45

    It’s very simple to find out any topic on net as compared to
    books, as I found this piece of writing at this website.

  • Gaye

    2019年7月20日 上午7:55

    you are really a good webmaster. The web site loading speed is incredible.
    It seems that you are doing any distinctive trick.

    Furthermore, The contents are masterwork. you have done a excellent job on this topic!

  • Jamison

    2019年7月19日 下午12:26

    Hi there to all, since I am truly eager of reading this web site’s post to
    be updated on a regular basis. It includes nice data.

  • Lynell

    2019年7月19日 下午12:09

    My brother suggested I might like this blog. He was once totally right.

    This submit actually made my day. You can not believe just how a lot time I had spent
    for this information! Thanks!

  • Mora

    2019年7月19日 上午11:49

    I like the valuable info you supply for your articles. I’ll
    bookmark your weblog and check again here frequently. I am moderately sure I’ll be told a lot of new stuff right right here!
    Best of luck for the next!

  • Aaron

    2019年7月19日 上午4:05

    Appreciation to my father who stated to me on the
    topic of this webpage, this blog is genuinely remarkable.

  • Jermaine

    2019年7月18日 上午6:49

    We stumbled over here from a different website and thought
    I may as well check things out. I like what I see so i am just following you.
    Look forward to looking into your web page yet again.

  • Jayden

    2019年7月18日 上午6:23

    It’s remarkable in favor of me to have a site, which is
    beneficial in favor of my know-how. thanks admin

  • Garfield

    2019年7月17日 上午2:00

    Hello to every , for the reason that I am truly keen of reading this web site’s post
    to be updated daily. It includes good stuff.

  • Iva

    2019年7月16日 上午3:55

    When someone writes an article he/she retains the plan of a user in his/her brain that how
    a user can be aware of it. So that’s why this piece of writing is
    amazing. Thanks!

  • Nina

    2019年7月13日 下午2:06

    My coder is trying to persuade me to move to .net from PHP.

    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am concerned about switching to another platform.
    I have heard fantastic things about blogengine.net.
    Is there a way I can transfer all my wordpress content
    into it? Any help would be greatly appreciated!

  • Audrea

    2019年7月13日 上午11:14

    This design is steller! You certainly know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Fantastic job. I really loved what
    you had to say, and more than that, how you presented it.
    Too cool!

  • Johnny

    2019年7月12日 下午2:29

    I simply could not leave your web site prior to suggesting that I really enjoyed the standard information an individual provide
    on your guests? Is gonna be again steadily to investigate cross-check new posts

  • Roosevelt

    2019年7月12日 下午1:51

    What’s up friends, pleasant post and good arguments commented at this place, I am
    actually enjoying by these.

  • Oren

    2019年7月12日 上午8:36

    Aw, this was an extremely nice post. Spending some time and actual effort to
    generate a top notch article… but what can I say…
    I put things off a lot and don’t seem to get anything done.

  • Benito

    2019年7月12日 上午12:35

    Amazing! Its in fact awesome paragraph, I have got much clear idea on the topic
    of from this post.

  • Janessa

    2019年7月11日 下午12:13

    Hi there would you mind stating which blog platform you’re working with?

    I’m going to start my own blog in the near future but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most
    blogs and I’m looking for something unique.
    P.S Sorry for being off-topic but I had to ask!

  • 何朱必

    2019年6月29日 上午11:29

    联手打造最好的web开发平台,让天下没有难做的web开发!

  • Cody

    2019年6月28日 下午7:30

    Magnificent goods from you, man. I have understand your stuff previous to and you’re just
    extremely fantastic. I actually like what you’ve acquired here,
    certainly like what you’re stating and the way in which you say it.

    You make it entertaining and you still care for to keep it sensible.

    I can’t wait to read far more from you. This is actually a terrific web
    site.

  • 何朱必

    2018年2月26日 下午12:36

    现代优雅Laravel框架入门经典解决方案之一

回复 何朱必 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注