エラー処理

全ての人々が知っている (べきだ) と思いますが、エラー処理 は、開発過程において非常に重要な部分をなしています。リクエストしたページがアクセス不能であることをユーザ (彼・彼女・あるいはそれ) に示すだけではなく、 HTTP エラーステータスを返すことで (ブラウザやそれに類する) マシンに対して、 今何が起きているのかを知らせる方法でもあります。

Code errors

FuelPHP's internal error handling is based on Exceptions. This allows you to catch them in your code, so your application can handle the exception that occured gracefully and the user can continue using the application.

FuelPHP also alters the default PHP behaviour when it comes to PHP errors (which are not exceptions) from the old procedural functions. Instead of continuing on all non-fatal errors, FuelPHP will throw a PhpErrorException on all of these errors. This will force you to solve all errors, even an E_NOTICE which in the old days you might have ignored. It will also allow you to catch PHP errors. For example to catch syntax errors in view files when you have non-programmers creating views.

You can alter this behaviour by adding PHP error types (like E_NOTICE or E_STRICT) to the errors.continue_on key in your applications config/config.php file. Errors defined here will allow your script to continue. No exception is being thrown for these errors, so you can no longer catch these.

It is strongly adviced not to continue on errors, as it might lead to bugs in your code that are very difficult to find once your application moves into production!

Application logic errors

404 エラー

404 のルートは app/config/routes.php で設定されます。そのルートは 404 のページ処理を行う controller/method を指示しています。 この点について、更にルーティングの節を参照ください

404 をスローする

ルーティング処理時のように、404 エラーをスローしたい時があるでしょう。 これは、HttpNotFoundException をスローすることで簡単に出来ます。404ページが動作すると、 Fuel は 終了 します。

throw new HttpNotFoundException;

この例外は、アプリケーションの index.php ファイル内で捕捉、処理されます。例外が捕捉されると、 設定されている 404 ルートを探し、見つかった場合、リクエストされた URI に対応する新しいリクエスト生成します。 つまりは、あたかも通常のリクエストと同じようにして、 その URI は 他のリクエストと同様のルーティング がなされるのです!

このような動作を望まないなら、index.php ファイルを下記のように変更してください。

// Generate the request, execute it and send the output.
try
{
	$response = Request::forge()->execute()->response();
}
catch (HttpNotFoundException $e)
{
	$route = array_key_exists('_404_', Router::$routes) ? Router::$routes['_404_']->translation : Config::get('routes._404_');
	if ($route)
	{
		// forge リクエストに 'false' を設定することで、ルーティングエンジンを無効にする
		$response = Request::forge($route, false)->execute()->response();
	}
	else
	{
		throw $e;
	}
}

404 処理

リクエストが生成され、そのルータがマッチしえるものを探し、その結果マッチするものがなかった場合、404 処理が開始されます。 デフォルトでは _404_ ルートは welcome/404 に向けられています。 以下、そのメソッドを一瞥することにしましょう:

// Inside Controller_Welcome

/**
 * The 404 action for the application.
 *
 * @access  public
 * @return  void
 */
public function action_404()
{
	$messages = array('Aw, crap!', 'Bloody Hell!', 'Uh Oh!', 'Nope, not here.', 'Huh?');
	$data['title'] = $messages[array_rand($messages)];

	// Set a HTTP 404 output header
	return Response::forge(View::forge('welcome/404', $data), 404);
}

上記引用した部分を見れば、404 処理がどのようになっているのかわかると思います。普通のコントローラのアクションと同じです。 この処理が適切な点は、好みの内容をページ表示することが出来るところにあります。 データベースから取得したデータをそのビューに読み込むことが出来るのです。

Fuel は 404 ステータスを設定していませんので、ご自分で HTTP レスポンスを設定する必要があることに留意ください。正しいステータスヘッダを送信するには、 Response::forge(ViewModel::forge('welcome/404'), 404); を返します。

Catch all

Fuel が 404 レスポンスステータスを設定していない点を利用して、同ステータスを catch all 機能として利用することが出来ます。 uri に基づいてページをデータベースから取得するようなページモデルを持つことも可能でしょう。 以下、そのようなことを可能とする例を示します:

// Inside your 404 controller

public function action_my404()
{
	$original_uri = \Input::uri();
	$result = \DB::select()->from('pages')->where('uri', $original_uri)->execute();
	if(count($result) === 1)
	{
		// お好みの (訳注: catch all によるデータベースからのデータ取得) ページを表示
	}
	else
	{
		// 一般的な 404 ページを表示
		$messages = array('Aw, crap!', 'Bloody Hell!', 'Uh Oh!', 'Nope, not here.', 'Huh?');
		$data['title'] = $messages[array_rand($messages)];
		return Response::forge(View::forge('welcome/404', $data), 404);
	}
}

500 をスローする

アプリケーションを単純に停止し、サーバの不適切な動作エラーを表示することが必要な場合があります。 通常それは 500 Internal Server Error です。

404 エラーをスローするのと同様、500 エラーをスローすることができます。

throw new HttpServerErrorException;