# Web Servers
It is typical to use the front-controller pattern to funnel appropriate HTTP requests received by your web server to a single PHP file. The instructions below explain how to tell your web server to send HTTP requests to your PHP front-controller file.
> 通常使用前端控制器模式将web服务器接收到的适当HTTP请求传送到单个PHP文件。下面的说明说明了如何告诉web服务器将HTTP请求发送到PHP前端控制器文件。
## PHP内置服务器
Run the following command in terminal to start localhost web server, assuming`./public/`is public-accessible directory with`index.php`file:
> 假设./public/是可通过index.php文件访问的目录,在terminal中运行以下命令来启动localhost web服务器:
~~~bash
php -S localhost:8888 -t public public/index.php
~~~
If you are not using`index.php`as your entry point then change appropriately.
> 如果您没有使用index.php作为入口点,那么请进行适当的更改。
## Apache configuration
Ensure your`.htaccess`and`index.php`files are in the same public-accessible directory. The`.htaccess`file should contain this code:
> 确保.htaccess和index.php文件位于相同的公共可访问目录中。.htaccess文件应该包含以下代码:
~~~bash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
~~~
This`.htaccess`file requires URL rewriting. Make sure to enable Apache’s mod\_rewrite module and your virtual host is configured with the`AllowOverride`option so that the`.htaccess`rewrite rules can be used:
> 这种`.htaccess`的文件需要URL重写。请确保启用Apache的mod_rewrite模块,并且您的虚拟主机配置了`AllowOverride`选项,这样就可以使用。`htaccess`的重写规则可以使用:
~~~bash
AllowOverride All
~~~
## Nginx configuration
This is an example Nginx virtual host configuration for the domain`example.com`. It listens for inbound HTTP connections on port 80. It assumes a PHP-FPM server is running on port 9000. You should update the`server_name`,`error_log`,`access_log`, and`root`directives with your own values. The`root`directive is the path to your application’s public document root directory; your Slim app’s`index.php`front-controller file should be in this directory.
> 这是一个例子Nginx虚拟主机配置域`example.com`。它监听端口80上的入站HTTP连接。它假设PHP-FPM服务器运行在端口9000上。您应该使用自己的值更新`server_name`、`error_log`、`access_log`和`root`指令。“根”指令是应用程序的公共文档根目录的路径;你的Slim应用程序的`索引`。php的前端控制器文件应该在这个目录中。
~~~bash
server {
listen 80;
server_name example.com;
index index.php;
error_log /path/to/example.error.log;
access_log /path/to/example.access.log;
root /path/to/public;
location / {
try_files $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;
}
}
~~~
## HipHop虚拟机
Your HipHop Virtual Machine configuration file should contain this code (along with other settings you may need). Be sure you change the`SourceRoot`setting to point to your Slim app’s document root directory.
> 您的HipHop虚拟机配置文件应该包含此代码(以及您可能需要的其他设置)。请确保将`sourceroot`设置更改为指向Slim应用程序的文档根目录。
~~~bash
Server {
SourceRoot = /path/to/public/directory
}
ServerVariables {
SCRIPT_NAME = /index.php
}
VirtualHost {
* {
Pattern = .*
RewriteRules {
* {
pattern = ^(.*)$
to = index.php/$1
qsa = true
}
}
}
}
~~~
## IIS
Ensure the`Web.config`and`index.php`files are in the same public-accessible directory. The`Web.config`file should contain this code:
~~~bash
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="slim" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
~~~
## lighttpd
Your lighttpd configuration file should contain this code (along with other settings you may need). This code requires lighttpd >= 1.4.24.
~~~bash
url.rewrite-if-not-file = ("(.*)" => "/index.php/$0")
~~~
This assumes that Slim’s`index.php`is in the root folder of your project (www root).
## 运行于子目录
If you want to run your Slim Application from a sub-directory in your Server’s Root instead of creating a Virtual Host, you can configure`$app->setBasePath('path-to-your-app')`right after the`AppFactory::create()`. Assuming that your Server’s Root is`/var/www/html/`and path to your Slim Application is`/var/www/html/my-slim-app`you can set the base path to`$app->setBasePath('/my-slim-app')`.
> 您希望从服务器根目录中的子目录运行Slim应用程序,而不是创建虚拟主机,您可以在`AppFactory::create()`之后配置`$app->setBasePath(' pathto your-app')`。如果您的服务器的根是`/ var/www/html/`,那么您的Slim应用程序的路径是`/ var/www/html/my-slim-app`,那么您可以将基本路径设置为`$app->setBasePath('/my-slim-app')`。
~~~php
<?php
use Slim\Factory\AppFactory;
use Slim\Middleware\OutputBufferingMiddleware;
// ...
$app = AppFactory::create();
$app->setBasePath('/my-slim-app');
// ...
$app->run();
~~~
- 开始
- 安装
- 升级指南
- Web服务器
- 概念
- 生命周期
- PSR 7
- 中间件
- 依赖容器
- 实例 及通知和警告处理
- Request
- 请求方法
- 请求头信息
- 请求主体
- 上传的文件
- 请求帮助
- 路由对象
- Response
- 响应状态
- 响应标头
- 响应体
- 返回JSON
- 视图模板
- 路由
- 创建路由
- 路由回调
- 路由策略
- 路线占位符
- 路由名
- 路由组
- 路由中间件
- 路由表达式缓存
- 容器识别解析
- 封装中间件
- 路由的中间件
- 错误处理中间件
- 方法重写的中间件
- 输出缓冲中间件
- 内容长度中间件
- 扩展功能
- 以 / 结尾的路由模式
- 获取当前路由
- 设置CORS
- 使用POST表单上传文件
- 第三方组件
- slim-session
- auth
- slim-api-skeleton
- dir