Request Class

The Request class processes URI requests. It is used by Fuel in index.php to process the users URI request, and you need it to generate requests in an HMVC context.

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

The forge method returns a new request object.

Static Yes
Parameters
Param Default Description
$uri
null
The URI being requested. If this parameter is not present, the URI class will be used to detect the URI.
$route
true
If true, the URI passed or detected will be processed by the routing class. If no route is present, a 404 will be generated. If you request a non-public URI, set this to false.
Returns Fuel\Core\Request Object
Example
// create the request object for the admin controller, login method
$request = Request::forge('admin/login');

// note that this only creates the object, it does not execute the request!

You can also use Request::forge() to forge a cURL or a SOAP request object.

execute($method_params = null)

The execute method executes a request and stores the resulting response in the request object to be used later.

Static No
Parameters
Param Default Description
$method_params
null
An array of optional parameters to pass to the controller being called.
Returns Fuel\Core\Request Object
Example
// create and execute the request object for the admin controller, login method
$request = Request::forge('admin/login')->execute();

// pass an object to the login controller
$object = new Object();
$request = Request::forge('admin/login')->execute(array('object' => $object));

response()

The response method fetches the response object of the executed request.

Static No
Parameters No
Returns Fuel\Core\Response Object
Example
// execute the request, fetch the response, and display it
$response = Request::forge('admin/login')->execute()->response();
echo $response;

// The Request object contains a __toString() method as an alternative way to fetch the output
$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
// Get 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()

The Request that created this Request.

Static No
Parameters No
Returns Fuel\Core\Request Object or null when it's the main request
Example
// get the parent of the current active Request
$parent = Request::active()->parent();

children()

The Request objects that were created during this Request.

Static No
Parameters No
Returns Array of Fuel\Core\Request Objects
Example
// get the Requests that were created during the main Request
$children = Request::main()->children();

main()

The main method returns the main Request instance (the one created by the initial page request).

Static Yes
Parameters No
Returns Fuel\Core\Request Object, or false if no main request instance exists.
Example
$main = Request::main();

This is the first executed Request instance, not necessarily the root parent of the current Request. That might be the case for example if the main Request 404s and you create a new Request after that (which is what happens in the _404_ route).

active()

The active method returns the currently active Request instance.

Static Yes
Parameters No
Returns Fuel\Core\Request Object, or false if no active request instance exists.
Example
$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
Parameters
Param Default Description
$path required Path to be added to the list of Request local search paths
$prefix
false
If false, the path will be appended to the search path. If true, it will be prepended.
Returns void
Example
// add the applications myfiles folder to the search path of the active request instance
Request::active()->add_path(APPPATH.'myfiles'.DS);

get_paths()

The get_paths method returns the defined list of search paths.

Static No
Parameters No
Returns void
Example
// get the defined search paths of the active request instance
$paths = Request::active()->get_paths();