action_index()
This method will be called if the controller is called without a second parameter. In the above example www.yoursite.com/example/index will be the same as www.yoursite.com/example.
Controllers are classes that can be reached through the URL and take care of handling the request. A controller calls models and other classes to fetch the information. Finally, it will pass everything to a view for output. If a URL like www.yoursite.com/example/index is requested, the first segment ("example") will be the controller that is called and the second segment ("index") will be the method of that controller that is called.
In Fuel, Controllers are put in the fuel/app/classes/controller directory. They should, optionally, extend the Controller class and be prefixed with "Controller_". Below is an example of the controller "example":
class Controller_Example extends Controller {
public function action_index()
{
$data['css'] = Asset::css(array('reset.css','960.css','main.css'));
$this->response->body = View::factory('test/index', $data);
}
}
Methods that can be requested through the URL are prefixed with "action_". This means that you're not limited by PHP's constructs on which name you might use (example: method "list" isn't allowed, "action_list" is no problem). But this also means you can give your controller public methods that can be used from other classes but are not routable.
Let say we also have the following method in our Controller_Example:
public function action_name_to_upper($name_1, $name_2)
{
$data['name_1'] = strtoupper($name_1);
$data['name_2'] = strtoupper($name_2);
$this->response->body = View::factory('test/name_to_upper', $data);
}
If we call this method using:
You can also put a controller in a subdirectory, like fuel/app/classes/controller/subdir/test.php. In this case, the controller must include the dirname in the classname like this: Controller_Subdir_Test.
Please Note: Only 1 level of subdirectories is currently supported.
This method will be called if the controller is called without a second parameter. In the above example www.yoursite.com/example/index will be the same as www.yoursite.com/example.
This method will be executed before the method from the URL is called on your controller, even if that method turns out not to exist.
This method will be executed after the method from the URL was called successfully, this will not be called if the method turns out not to exist.
This method will take over the internal routing of the controller. Once the controller is loaded, the router() method will be called and use the $method that is being passed in, instead of the default method. It will also pass in $params, in an array, to that $method. The before() and after() methods will still work as expected.
This property holds the Request object. It should not be overwritten. It should be used to get information about the current request. It should be thought of as read-only.
This property holds the Reponse object. To output something, add it to the $this->response->body property.
Thanks to the autoloader, you can extend other controllers without writing anything more than its name in the class definition:
class Controller_Example extends Controller_Welcome {
// your methods
}
This may sound strange at first, but extending controllers allows you to share methods and create base controllers really easily.
A base controller is a shared controller, like Controller_Public or Controller_Admin and are used to share logic between groups of controllers. For example, the Controller_Admin controller could contain your login/logout actions and maybe a dashboard, but it could also contain a before() method that checks to see if the user is logged in as an admin. Then all other controllers in your admin panel will extend this and automatically be secured.
class Controller_Admin extends Controller {
public function before()
{
// check for admin
}
// your methods
public function action_index()
{
// load the dashboard
}
public function action_login()
{
// log in the user
}
}
That code will go in fuel/app/classes/controller/admin.php and all of your other controllers should go in fuel/app/classes/controller/admin/, like this one:
class Controller_Admin_User extends Controller_Admin {
public function action_index()
{
// overrides the dashboard with the user index listing
}
public function action_edit($id)
{
// edit users
}
}