Hello World Site Side
Creata a model /workspace/site/models/StaffModel.php. It is recommended to always suffix models with the word Model.
<?php
declare(strict_types=1);
namespace workspace\site\models;
use nicotine\Model;
// Note that the filename and class has the same name,
// and it is a good practice to suffix them with "Model" word.
class StaffModel extends Model {
public function getMembers()
{
return $this->db->getAll("SELECT * FROM `staff`");
}
}
Create a controller /workspace/site/controllers/Staff.php. Don't suffix the controller with the word Controller. It will appear in the URL!
<?php
declare(strict_types=1);
namespace workspace\site\controllers;
use nicotine\Controller;
// Optional but very useful. This holds all framework instances.
use nicotine\Registry;
// Same as file name. E.g. /workspace/site/controllers/Staff.php
class Staff extends Controller {
// Optional.
public function __construct()
{
// If you use class constructor, don't forget parent constructor.
parent::__construct();
// Do you want to use same layout for all controller actions?
// Of course, you can set the layout custom, inside each method (aka action).
$this->proxy->layout = 'layout-file';
}
public function listMembers($httpParamExample = '')
{
// Call the method getMembers() from the /workspace/site/models/StaffModel.php
$staffMembers = $this->proxy->site->model('StaffModel')->getMembers();
$this->proxy->view(
// Set the view to be /workspace/site/views/staff/list.php
'staff/list',
// Set into view the property $this->vars->staffMembers
compact('staffMembers')
);
}
}
Create a layout /workspace/site/layouts/layout-file.php. This layout can hold multiple views, accross HTTP requests.
<!DOCTYPE html>
...
<?php print $this->contentForLayout; ?>
...
...
Create a view /workspace/site/views/staff/list.php.
<?php foreach ($this->vars->staffMembers as $staffMember) { ?>
<p>Staff ID = <?php print $staffMember['id']; ?></p>
<?php } ?>