Controllers & Actions
Actions in the Stratos Framework represent a very specific thing a user can do within your application. Controllers are simply a group of actions, and this grouping is entirely up to you.
Controllers
Controllers are implemented as classes in your controllers directory. The only requirements of a controller are that it has the suffix "Controller" and that the file name (ie. BlogController.php) corresponds to a class within that file (ie. BlogController). For example, the following is a perfectly valid controller implementation (though it is also perfectly useless):
controllers/BlogController.php
<?php class BlogController { } ?>
That's all it takes to create a controller. Easy, right?
Actions
To create an action, simply add a function to your controller. If we added a few actions to our BlogController, it might look something like this:
controllers/BlogController.php
<?php class BlogController { /** * Return a list of all blog posts. */ function list() { return array( 'posts' => BlogPost::getAll() // BlogPost is a data object. We'll come back to these later. ); } /** * Return a single blog post given an id. */ function view( $post ) { return array( 'post' => BlogPost::getById($post) // BlogPost is a data object. We'll come back to these later. ); } /** * Sample private method. Stratos will not expose this as an action. */ function _myPrivateMethod() { return "I'm not web accessible."; } } ?>
This controller would provide two actions in your application: Blog/list and Blog/view. Stratos will not expose underscore-prefixed methods such as _myPrivateMethod() above, or, in PHP 5, methods defined as protected or private. There are also a few method names with special meanings:
- index(): If this method exists, it will be used as the default action when a request includes a controller name but no method name.
- start(): If this method exists, it will be called before any action requested from the same controller. It can not be requested directly.
- stop(): If this method exists, it will be called after any action requested from the same controller. It can not be requested directly.
If any action method has parameters, such as view( $post ) in the example above, Stratos will automatically map script input with the same name to those parameters by checking POST, GET, and PATH_INFO variables (in that order). So, a request to http://www.example.com/index.php/Blog/view/post/3 would result in Stratos creating an instance of BlogController and calling the view() method on that instance, passing in 3 for $post.
Finally, if your action returns a hash, the key/value pairs will be passed as arguments to the view. Using the above example again, a call to the Blog/list action would cause a $posts variable to be visible to the view when it gets executed. Views are covered in more detail in the Views section of the manual.
Easy As Pie
Aside from the few caveats mentioned, you are free to treat your controllers and actions just like any regular PHP class or function -- write a constructor, add member variables or constants, implement an interface, extend another class, etc. When extending another class though, keep in mind that any public methods inherited from the parent class will be made visible through the controller.
The Stratos control panel provides a useful interface to view a complete list of actions in your application (excluding those that are built-in). Navigate to the control panel for your application, and click "Actions" under "Stratos Framework" in the menu.