The framework is mvc based, which stands for model, view, controller. The mvc design pattern is build with this structure where, models are used for database interaction, views for the html markup and controllers for the main/core logic of the webapplication. Commonly, controllers return views and model instances are created inside controllers.
Controllers can be created inside the /app/controllers folder.
<?php
namespace app\controllers;
class ExampleController {
public function example() {
echo 'Hello world!';
}
}
Extend the base controller and call the view function method. The argument for the view function method should be the file location path + the filename without php extension. Not the absolute path, but from app/views. Whether you have controller data to pass or not, you should always chain the data method when returning a view.
<?php
namespace app\controllers;
class ExampleController extends Controller {
public function example() {
return $this->view('view')->data();
}
}
Create an associative array where the key value lateron can be used inside the view to get the data and pass the array inside the data method.
<?php
namespace app\controllers;
class ExampleController extends Controller {
public function example() {
$data['string'] = 'Hello world!';
return $this->view('view')->data($data);
}
}
Note: echo $string will print: Hello world!
<!DOCTYPE html>
<html lang="en">
<head>
<title>example</title>
</head>
<body>
<?php echo $string; ?>
</body>
</html>
Both get and post data will automatically be merged inside controller function methods.
<?php
namespace app\controllers;
class ExampleController {
public function example($request) {
print_r($request);
}
}