Wolff\Core\Controller
The controllers are a great way to keep your code organized instead of defining everything in the system/web.php file.
Let's create a controller, it must be in the Controller namespace.
Any public method that is supposed to be accesible through a route, can take two parameters which are the request and response objects (instance of Wolff\Core\Http\Request and Wolff\Core\Http\Response).
app/controllers/Home.php:
namespace Controller;
class Home
{
public function index($req, $res)
{
$res->write('hello world');
}
public function sayHi($req, $res)
{
$res->write('hi');
}
}
Now if you want to call the controller's method when accessing some routes, you can add the following code to your web file.
system/web.php
Route::get('/', [ Controller\Home::class, 'index' ]);
Route::get('sayHi', [ Controller\Home::class, 'sayHi' ]);
The Wolff\Core\Controller class offers some useful static methods you can use.
get(string $path): \Wolff\Core\Controller
Returns a new instantiated controller.
Controller::get('home');
That will return the home controller.
method(string $path, string $method[, array $args]): mixed
Returns the value of a controller method.
The first parameter must be the controller name, the second and optional parameter must be the method name, the third and optional parameter must be an array with the arguments that will be used for the method.
This method throws a BadMethodCallException when the method does not exists.
Controller::method('client', 'getClientById', [ $client_id ]);
That will call the getClientById method of the client controller using the third parameter as the parameters.
exists(string $path): bool
Returns true if the specified controller exists, false otherwise.
Controller::exists('Home');
That will return true only if the app/controllers/Home.php controller exists, false otherwise.
hasMethod(string $path, string $method): bool
Returns true if the specified method of the controller exists and is accessible, false otherwise.
The first parameter must be the controller name. The second parameter must be the method name.
Controller::hasMethod('places/Info', 'getInfoById');
That will return true only if the places/Info controller class has a getInfoById method.