laravel 架构解析
- 协议: http / https, 当然 https 是大势所趋, 有空再折腾 ( 应用层 )
- ip, 或者 域名, 这样才能在网络上找到你的服务器 ( ip + 端口 就是 网络层)
- 端口, http 默认 80, https 默认 443, 具体什么端口在 nginx中配置
- path(路径) 或者 route(路由), 用来查找到对应文件 或者 框架中对应的 controller/action (框架相关后面 再讲)
- get 参数
(假如我现在是url): 首先, 我看解析出来 应用层协议(http / https), 然后根据 ip 找到服务器, 再根据端口找到服务器上面的服务(这里是 nginx), 然后根据 nginx 的配置到项目的根目录, 然后根据 path 去找执行文件.
一个 nginx + php-fpm 的实例:
server { listen 80; # 端口 server_name laravel.dev laravel.dev; # 域名 root /data/web/laravel/public; # 项目根目录 location / { # 所有文件的执行规则 index index.html index.htm index.php; # 默认执行文件 try_files $uri $uri/ /index.php?$query_string; # 转换url, 不显示 index.php autoindex on; # 开启目录浏览功能 } location ~ \.php(.*)$ { # php 后缀文件的执行规则, 其实就是交给 php-fpm 来执行 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; }}这里有一点要注意: nginx 是先 执行文件 , 比如你有一个 url为 http://xxx.com/test, 根目录下刚好有一个名为 test 的文件, 那么nginx就直接去下载这个文件了, 而不会到你项目中 test 对应的 controller/action
请求 -> laravel 框架
通过上面可以知道 请求 被 public/index.php 文件来执行.index.php 这个文件干了 3 件事1:
- 实现 composer 的 类的自动加载
- 载入 laravel app, 核心是一个 service container, 这个后面慢慢分析
- return response, 返回 http response
- 关于类的自动加载, 可以参考下面2个教程:
- 站在巨人的肩膀上写代码—SPL: http://www.imooc.com/learn/150, 里面详细的讲了类的自动加载是如何 发展&实现 的
- PHP实现手机归属地查询: http://www.imooc.com/learn/604, 类的自动加载的 简单 实现