How To Use Controllers

Basic Functionality

Controllers are where the application logic is stored. This may involve fetching information from the database via the model layer and then defining what information is made available to the view layer which is then displayed to the user.

Out of the box, PHP Wax takes care of basic URL mapping to controller actions. For example if you look in your default controller app/controller/PageController.php you'll see you have a method named index. This gets run by default when you hit the root of a site. Try visiting your local site and you'll see the default page.

Any public method we make in this controller will map to a url. Let's try a test:

public function test() {
  echo "HELLO WORLD";
  exit;
}

Now if you go to http://yoursite.local/test you'll get the hello world message.

Matching URLs, Method Names and Views

As PHP doesn't support - inside function names these are replaced in the URL with and underscore (_) and this will be the name of the method.

For example, http://yoursite.local/contact-us will look for a public method called contact_us

In such cases the name of the view will match the url style, with hyphens (-).