php框架slim使用(1)

123

最简单的办法是使用命令安装初始框架。
假设项目叫做slim1

windows下cmd窗口。
composer create-project slim/slim-skeleton slim1

只要composer安装正确,很快就装好了,然后启动,用php自带的web启动方式
cd slim1
php -S localhost:80 -t public public/index.php


现在启动就绪。
打开浏览器输入网址
http://localhost:8080/


=======
如果喜欢apache

修改本机host
127.0.0.1 www.t3.com

配置apache虚拟主机例如
<VirtualHost *:80>
  # 配置文档根目录   
  DocumentRoot "D:/workspace_utf8/t3/public"
  # 这个
  ServerName www.t3.com
  # 这个很重要,当用户输入目录时,寻找目录下的什么文件
  DirectoryIndex index.php 
  # 对这个目录的访问权限进行一些设置
  <Directory "D:/workspace_utf8/t3/public">
    AllowOverride All
    Options   FollowSymLinks  
    Order Allow,Deny
    Allow from all
    Require all granted
  </Directory>
  
  # 当php未设置时,默认utf-8输出文件
  AddDefaultCharset utf-8
  # 重要,开启重定向,这样才可以使用slim框架
  RewriteEngine on
</VirtualHost>


其实还需要public下有一个.htaccess,只是框架已经在安装时自动帮你加到项目下了,能看到,所以不需自己搞。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]



现在打开浏览器直接输入www.t3.com
搞定。

==========================
如果喜欢nginx,
你需要将 server_name, error_log, access_log, 和 root 这些指令修改成你自己的值。其中 root 指令是你的应用程序公共文件根目录的路径;你的 Slim 应用的 index.php 前端控制器文件应该放在这个目录中。

配置示例
server {
    listen 80;
    server_name www.t3.com;
    index index.php;
    error_log /path/to/example.error.log;
    access_log /path/to/example.access.log;
    root "D:/workspace_utf8/t3/public";

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
    }
}

另外,nginx是不需要.htaccess文件的,当然,放了也没关系,如果需经常切换服务器,不如就放着。



猜你喜欢

转载自xieye.iteye.com/blog/2382474