# 404 Not Found 處理器
[Edit This Page](https://github.com/slimphp/Slim-Website/tree/gh-pages/docs/handlers/not-found.md)
如果你的 Slim 應用程序沒有可以匹配到當前 HTTP 請求 URI 的路由,程序會調用 Not Found 處理器,并返回一個 `HTTP/1.1 404 Not Found` 響應到 HTTP 客戶端。
## 默認的 Not Found 處理器
每個 Slim 框架應用程序都有一個默認的 Not Found 處理器。這個處理器將響應狀態設置為 `404`,并將內容類型設置為 `text/html` ,它將寫入一個簡單的異常信息到響應體。
## 自定義 Not Found 處理器
Slim 框架應用程序的 Not Found 處理器是一個 Pimple 服務。 你可以通過應用程序容器對自定義 Pimple factory 方法進行定義,來創建自定義的 Not Found 處理器取代默認的。
```
$c = new \Slim\Container(); //Create Your container
//Override the default Not Found Handler
$c['notFoundHandler'] = function ($c) {
return function ($request, $response) use ($c) {
return $c['response']
->withStatus(404)
->withHeader('Content-Type', 'text/html')
->write('Page not found');
};
};
//Create Slim
$app = new \Slim\App($c);
//... Your code
```
在這個例子中,我們定義了一個新的 `notFoundHandler` factory ,它將返回一個 callable 。返回的 callable 接收2個參數:
1. 一個 `\Psr\Http\Message\ServerRequestInterface` 實例
2. 一個 `\Psr\Http\Message\ResponseInterface` 實例
這個 callable **必須** 返回一個恰當的 `\Psr\Http\Message\ResponseInterface` 實例。