Request class

RequestクラスはURIリクエストを処理します。Fuelはindex.php内で、ユーザーからのURIリクエストを処理するために同クラスを使用しています。また、HMVCコンテクストの中でリクエストを生成するためには、このクラスを使う必要があります。

forge($uri = null, $route = true)

forge メソッドは新しいリクエストオブジェクトを返します。

Static Yes
パラメタ
パラメタ 初期値 説明
$uri
null
リクエストされているURI。このパラメタが存在しない場合は、URIを見つけるためにURIクラスが使われます。
$route
true
もしtrueであれば、渡された(passed)あるいは見つけだされたURIはRoutingクラスで処理されます。もし経路(ルート)が存在しないのであれば404が生成されます。もし非公開のURIをリクエストするのであれば、このパラメタにはfalseを設定してください。
返り値 Fuel\Core\Request オブジェクト
// 管理コントローラーのログインメソッドのリクエストオブジェクトを生成する。
$request = Request::forge('admin/login');

// 注意:オブジェクトを生成するだけであって、リクエストを実行するわけではない!。

execute()

executeメソッドはリクエストを実行し、そのレスポンス結果を利用するために結果をリクエストオブジェクトに格納します。

Static No
パラメタ No
返り値 Fuel\Core\Request オブジェクト
// 管理コントローラー、ログインメソッドのリクエストオブジェクトを生成し実行する。
$request = Request::forge('admin/login')->execute();

response()

responseメソッドは、実行されたリクエストのレスポンスオブジェクトを取得します。

Static No
パラメタ No
返り値 Fuel\Core\Response オブジェクト
// リクエストを実行し、レスポンスを取得し、それを表示する。
$response = Request::forge('admin/login')->execute()->response();
echo $response;

// リクエストオブジェクトには、出力取得の別の方法として __toString()メソッドが含まれている。
$output = Request::forge('admin/login')->execute();
echo $output;

params()

Returns an array of all named parameters in the route that triggered this request.

Static No
Parameters No
Returns array
Example
// get the list of named parameters
$params = Request::active()->params();

get_method()

Gets the request method.

Static No
Parameters none
Returns Request method
Example
// Set the request method on a forged request
$method = Request::main()->get_method();

set_method($method)

Sets the request method.

Static No
Parameters
Param Default Description
$method string, required The name of the parameter to be returned. This must be exactly the same as defined in the route.
Returns current instance
Example
// Set the request method on a forged request
$params = Request::forge('some/route')->set_method('POST');

param($param, $default = null)

Returns a specific named parameter in the route that triggered this request, of the default if the parameter is not found.

Static No
Parameters
Param Default Description
$param string, required The name of the parameter to be returned. This must be exactly the same as defined in the route.
$default
null
value to be returned if the named parameter requested is not defined.
Returns mixed
Example
// get a specific named parameter called 'name'
$param = Request::active()->param('name', 'none');

The routing section explains how named paramaters can be defined in a route.

parent()

リクエストを生成したリクエスト

Static No
パラメタ No
返り値 Fuel\Core\Request オブジェクト。メインリクエストの場合はnull。
// 現在実行されているリクエストの親を取得
$parent = Request::active()->parent();

children()

リクエスト処理中に生成されたリクエストの各種オブジェクト。

Static No
パラメタ No
返り値 Fuel\Core\Request オブジェクトの配列
// メインリクエスト処理中に生成された各種リクエストを取得
$children = Request::main()->children();

show_404($return = false)

show_404メソッドは、アプリケーションの404ページを表示するか、そのまま返します。もしroutes.phpに設定していない場合は、Fuelのデフォルト404ページがその代わりをします。404のルート設定が間違っている場合は、エラーがスローされます。

この機能は現在非推奨でHttpNotFoundExceptionが推奨されています。

Static Yes
パラメタ
パラメタ 初期値 説明
$return
false
もしfalseなら結果がエコーされます。trueの場合はその値を返します。
返り値 もし$returnがfalseの場合メソッドは終了。そうでないならば設定された404ページのhtmlを返します。
Request::show_404();

main()

mainメソッドはメインリクエストのインスタンス(最初のページリクエストで生成されたもの)を返します。

Static Yes
パラメタ No
返り値 Fuel\Core\Request オブジェクト、 あるいはメインリクエストインスタンスが存在しない場合は false を返す。
$main = Request::main();

これは最初に実行されたリクエストインスタンスであって、必ずしも現在のリクエストのルート親(root parent)のインスタンスではありません。たとえば、メインリクエストが404で(_404_にルート付けられたものが実行された)その後に新しいリクエストを生成するという様なケースがそれにあたるでしょう。

active()

activeメソッドはアクティブなリクエストインスタンスを返します。

Static Yes
パラメタ No
返り値 Fuel\Core\Request オブジェクト、 あるいはアクティブなリクエストインスタンスが存在しない場合は false を返す。
$active = Request::active();

add_path($path, $prefix = false)

The add_path method allows you to add a search path to the current request instance. The search paths are used by Finder::search() to locate files within your application.

Static No
パラメタ
パラメタ 初期値 説明
$path 必須 リクエスト一覧に、ローカルサーチパスを加える
$prefix
false
falseなら、パスはサーチパスの後ろに、trueならサーチパスの前に付加されます。
返り値 void
// アプリケーションのmyfilesフォルダをアクティブなリクエストインスタンスのサーチパスに付け加える。
Request::active()->add_path(APPPATH.'myfiles'.DS);

get_paths()

get_pathsメソッドは設定されているサーチパス一覧を返します。

Static No
パラメタ No
返り値 void
// アクティブなリクエストインスタンス内で設定されているサーチパスを取得
$paths = Request::active()->get_paths();