使用Composer从零开发一个简单的web框架(01)-项目初始化

最终源码

https://gitee.com/mirahs/phpweb

软件版本

  • Apache 2.4.53,安装参考 wamp apache
  • Php 8.1.5,安装参考 wamp php
  • Composer 2.3.5,安装参考 Composer安装配置和使用

创建项目

创建phpweb项目目录,并在其下创建public目录

$ mkdir phpweb/public -p

新建public/index.php项目入口文件,内容如下


echo 'Hello, World!';

apache 配置

编辑C:\Windows\System32\drivers\etc\hosts文件,添加一条记录

127.0.0.1		phpweb.com

编辑D:\apps\wamp\httpd-2.4.53\conf\extra\httpd-vhosts.conf,添加 web 站点配置


    DocumentRoot        "D:/apps/wamp/www/phpweb/public/"
    ServerName          phpweb.com

重启apache

浏览器访问 http://phpweb.com/

apache 重定向

框架是单一入口多应用模式,所有的请求都由index.php统一处理,当访问 http://phpweb.com/home/hello/world 链接时,框架会访问home应用下的hello控制器的world函数。但现在还需要配置一下apache的重定向规则,不然访问会返回Not Found

Not Found
The requested URL was not found on this server.

编辑D:\apps\wamp\httpd-2.4.53\conf\httpd.conf文件,找到#LoadModule rewrite_module modules/mod_rewrite.so取消注释开启重定向功能

重启apache

新建public/.htaccess文件,内容如下(如果创建不了,可以使用 Git Bash,执行 touch public/.htaccess 命令创建文件)


  Options +FollowSymlinks -Multiviews -Indexes

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
  RewriteRule .* /index.php [L] #如果这行配置不行,注释掉这行并取消上面行的注释

浏览器访问 http://phpweb.com/home/hello/world,这时不会再返回Not Found错误了

你可能感兴趣的:(composer,前端,php)