# 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