Hello World Admin Side
Creata a model /workspace/admin/models/StaffModel.php. It is recommended to always suffix models with the word Model.
<?php
declare(strict_types=1);
namespace workspace\admin\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/admin/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\admin\controllers;
use nicotine\Controller;
use nicotine\RequestMethod;
use nicotine\AdminRoles;
// Optional but very useful. This holds all framework instances.
use nicotine\Registry;
// Same as file name. E.g. /workspace/admin/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';
}
// RequestMethod could be only "get" or "post".
// Admin roles for this action. Comma separated.
#[RequestMethod('get')]
#[AdminRoles('super_admin')]
public function listMembers($httpParamExample = '')
{
// Call the method getMembers() from the /workspace/admin/models/StaffModel.php
$staffMembers = $this->proxy->admin->model('StaffModel')->getMembers();
$this->proxy->view(
// Set the view to be /workspace/admin/views/staff/list.php
'staff/list',
// Set into view the property $this->vars->staffMembers
compact('staffMembers')
);
}
}
Create a layout /workspace/admin/layouts/layout-file.php. This layout can hold multiple views, accross HTTP requests.
<!DOCTYPE html>
...
<?php print $this->contentForLayout; ?>
...
...
Create a view /workspace/admin/views/staff/list.php.
<?php foreach ($this->vars->staffMembers as $staffMember) { ?>
<p>Staff ID = <?php print $staffMember['id']; ?></p>
<?php } ?>