Controllers should contain the logic for your website, app or API. Controllers bringing the data from your models together and then pass them on to your views.
¶ Anatomy of a controller
A controller should extend the Wayfinder Class so that you have access to all of it's public methods, like so:
class MyClass extends Wayfinder {
}
¶ __construct()
Within your class, you can optionally use the __construct()
method to automatically run some tasks when the Class is first initiated.
class MyController extends Wayfinder {
function __construct() {
// do stuff here
}
}
¶ Creating an end point
To create an end point in Wayfinder, just declare a public method. If no method is specified in the URL or in your custom route, then Wayfinder will look for index()
by default.
class MyController extends Wayfinder {
public function index() {
// default page
}
public function myendpoint() {
// a custom endpoint
}
}
The definition above would give you three end point:
/mycontroller
/mycontroller/index
(same as above)/mycontroller/myendpoint
¶ Public and private methods
Public methods expose end points, where as private methods can be used to hide functionality and are great at helping you to organise your code.