在了解如何处理Alamofire发送到服务端的各种请求之前,我们通过这段视频向大家介绍如何基于Laravel和PHPStorm搭建一个便捷的服务端开发环境,它可以帮助我们更方便的观察Alamofire发来的各种请求。
下载Laravel
为了方便测试,我们新建一个Laravel项目来处理Alamofire发来的各种HTTP请求:
composer create-project --prefer-dist laravel/laravel apidemo
创建完成之后,我们把新项目添加到Homestead。
配置Homestead
打开~/.homestead/Homestead.yaml,在配置文件的对应部分,添加下面的内容:
folders:
- map: /Users/puretears/projects/apidemo
to: /var/www/apidemo/current
sites:
- map: apidemo.boxue.io
to: /var/www/apidemo/current/public
databases:
- apidemo
保存并退出之后,执行下面的命令把新增的Web应用添加到vagrant:
vagrant up
vagrant provision
完成之后,添加一条新的/etc/hosts纪录:
apidemo.boxue.io 192.168.10.10 # Change your local IP here
然后在浏览器里访问apidemo.boxue.io,能显示默认Laravel 5的页面,就表示基本的环境已经完成了。接下来,我们访问https://apidemo.boxue.io,就会看到一个安全提示。这是由于Homestead里配置的证书不是有一个可信的CA签发的造成的。由于iOS 9以后,所有的HTTP通信强制要求要基于HTTPS,因此我们需要给apidemo申请一个由可信CA签发的证书。

安装证书
网站有不少可以申请免费SSL证书的地方,在这里我们用WoSign(沃通)来举例,点击申请免费SSL证书:
填入基本资料后,点击"提交申请":

在"我的订单"页面里,点击"提交CSR":

简单起见,我们选择"直接由系统生成CSR",然后设置一个"证书保护密码"(接下来WoSign会把证书打包成一个ZIP文件,并且由你设置的"证书保护密码"作为解压密码)。确认后,点击"提交"按钮。

成功签发之后,我们就可以下载证书了。

用之前设置的"证书保护密码"解压之后,可以看到适用不用HTTP Server的压缩包:

由于Homestead使用的Nginx,我们进一步解压缩for Nginx.zip,得到公钥和私钥。接下来,在之前创建的apidemo项目目录里,新建一个目录,例如cert,然后把证书文件复制到cert目录,这样,它们就会被自动同步到vagrant里。之后,我们登录vagrant:

删除Homestead为apidemo默认签发的证书:
sudo rm /etc/nginx/ssl/apidemo*
进入apidemo项目目录,把同步进来的新证书复制到/etc/nginx/ssl目录:
cd /var/www/apidemo/current/cert
sudo cp 1_apidemo.boxue.io_bundle.crt /etc/nginx/ssl/apidemo.boxue.io.crt
sudo cp 2_apidemo.boxue.io.key /etc/nginx/ssl/apidemo.boxue.io.key
检查nginx配置无误之后,重新加载nginx配置文件:
sudo nginx -t
sudo service nginx reload
此时,再访问https://apidemo.boxue.io,浏览器就不会有安全告警了。

“简单起见,我们只是搭建了一个基本的可以用于iOS通信的HTTPS环境,但这样的配置并非是足够安全的,不要在生产环境直接这样配置。”
<small style="box-sizing: border-box; font-size: 14.399951934814453px; font-weight: 400;">特别提示</small>
设置PHPStorm
搭建好HTTPS环境之后,为了能"可视化"Alamofire和HTTPS服务器的交互,我们使用PhpStorm来做服务端开发工具。大家可以到这里下载免费版。
使用PHPStorm打开之前创建的apidemo项目目录,首次打开的话PhpStorm会检测名字空间和建立项目索引。
接下来,点击"Run",选择"Break at first line in PHP scripts"。

然后,在vagrant apidemo的项目目录中,执行下面的命令创建一个处理Alamofire请求的Controller,假设iOS客户端请求的URL是/alamofire:
php artisan make:controller AlamofireController --resource
这样,Laravel就会自动为我们创建一个AlamofireController,用来处理/alamofire的REST请求。我们在index()方法里,添加下面的代码:
class AlamofireController extends Controller
{
public function index(Request $request)
{
$data = $request->all();
return response()->json($data);
}
}
然后,在request->all()设置一个断点。此时,PhpStorm就已经准备好调试Alamofire的请求了。

调试
用Xcode打开我们在上一个视频中的例子,把viewDidLoad的代码改成下面这样:
let getRequest = "https://apidemo.boxue.io/alamofire?XDEBUG_SESSION_START=PHPSTORM"
Alamofire.request(.GET, getRequest)
.responseJSON(completionHandler: { response in
switch response.result {
case .Success(let json):
print("JSON: ================")
case .Failure(let error):
print("\(error)")
}
})
按Command + R编译执行,这时就会从Xcode跳转到PhpStorm,由于之前我们设置了"Break at first line in PHP scripts",于是PhpStorm停在了index.php的第一行,点击底部的绿色小三角,我们让它继续执行,但是此时PhpStorm并没有停在我们设置的断点上,这是为什么呢?
答案是我们忘记了一些事情 _...
首先,在routes.php里,添加一个处理/alamofire的路由:
Route::resource("alamofire", "AlamofireController");
然后,去掉Run -> Break at first line in PHP scripts这个选项,回到Xcode,按Command + R重新编译执行。这次,回到PhpStorm,仍旧没有断在我们设置的断点上,并且我们可以看到一个提示:

这是因为,我们还没有设置本地源代码到vagrant server的path mapping,点击提示中的PHP|Servers,在弹出的对话框中,设置好apidemo在Vagrant中的根目录:

点击OK按钮回到PhpStorm。然后,我们回到Xcode重新按Command + R编译执行(_这真的是最后一次了哈)。这次,我们会自动跳转到PhpStorm,然后,成功停在我们的断点上了。

然后,我们单步执行一次,点击窗口底部的debugger标签,在这里,可以看到PHP执行过程中,各种变量的值。点开$data,就能看到我们来自Alamofire请求的URL参数了:

这时,我们回到Xcode,就可以在控制台看到Alamofire成功收到响应之后的输出了:

网友评论