Controllers
Aside from the dispatcher class, the controller is the main entry point to your application. It controls what data is loaded and manipulated, and determines what to display to the user.
As previously discussed, in OpenAvanti we expect our URI to be formatted as follows:
http://app.yoursite.com/controller/method/args
Where, naturally, controller specifies the base name of the controller to instantiate, method determines which method of the controller to call, and args are arguments to pass to that controller.
Let’s revisit our example:
http://app.mysite.com/users/view/445
Using the dispatcher, we would instantiate the class UsersController (remember, all controllers must end in “Controller”, but the word “Controller” is not in the URI), and call the method view. 445 would be passed as an argument to view.
<?php $oUser = new UserController(); $oUser->view( 445 ); ?>
Defining a Controller
Let’s define our controller class. Make sure you’ve followed the installation directions, as well as read over the article on the dispatcher before trying this code.
<?php class UsersController extends Controller { public function index() { echo "hello, world"; } public function view( $iUserID ) { echo "You want user #{$iUserID}"; } } ?>
Save this code to your controllers/ directory, and also make sure you have a proper configuration and index.php file setup in your document root (see installation articles, as well as The Dispatcher article).
In our UsersController above, we extend the OpenAvanti Controller class. Doing so provides us with some extra functionality that we can explore later.
We define two methods: index() and view(). Remember, the index method is invoked when no method is provided through the URI. To invoke the index method, use one of these two URIs:
/users /users/index
Using either of these URIs, you should see “hello, world” in your browser.
To invoke the view method, use the following URI:
/users/view/445
Using this URI, you should see “You want user #445” in your browser.
Note that in order for this to work, you must supply a iUserID as the third path of the URI. To get around this, you could supply a default of null for iUserID and then throw a 404 error if iUserID is null. To learn how to throw a 404 error, see the article on Handling 404 errors.
Next we’re going to want to load a view file and supply it with some data. To learn how to do this, check out the article Controllers and Views.

