Controllers
Plugin controllers follow the same patterns as Pubvana core controllers. There are two distinct types: admin controllers and public controllers. Each extends a different base class and uses a different view rendering mechanism.
Admin Controllers
<?php
namespace PluginsMyPluginControllersAdmin;
use AppControllersAdminBaseAdminController;
class Dashboard extends BaseAdminController
{
public function index(): string
{
$data = $this->baseData();
$data['active_nav'] = 'myplugin_dashboard';
$data['page_title'] = 'My Plugin Dashboard';
$data['items'] = model('PluginsMyPluginModelsItemModel')->findAll();
return view('Plugins\MyPlugin\Views\admin\dashboard', $data);
}
}
baseData()
BaseAdminController::baseData() returns the standard data array expected by all admin views (user, site_name, plugins menu items, page_title, active_nav). Always call it and then add page-specific keys.
active_nav
Set $data['active_nav'] to the nav_key of the currently active menu item to highlight the correct sidebar entry:
$data['active_nav'] = 'pvdocs_articles';
Rendering Admin Views
return view('Plugins\MyPlugin\Views\admin\index', $data);
Input Validation
if (! $this->validate(['title' => 'required|min_length[3]'])) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
Public Controllers
<?php
namespace PluginsMyPluginControllers;
use AppControllersBaseController;
class Main extends BaseController
{
public function index(): string
{
$items = model('PluginsMyPluginModelsItemModel')
->where('status', 'published')
->orderBy('created_at', 'DESC')
->findAll();
return $this->themeService->view(
'PluginsMyPluginViewsstoreindex',
['page_title' => lang('MyPlugin.page_title'), 'items' => $items]
);
}
public function show(string $slug): string
{
$item = model('PluginsMyPluginModelsItemModel')
->where('slug', $slug)
->where('status', 'published')
->first();
if ($item === null) {
throw new CodeIgniterExceptionsPageNotFoundException();
}
return $this->themeService->view(
'PluginsMyPluginViewsstoreshow',
['page_title' => $item->title, 'item' => $item]
);
}
}
ThemeService::view()
Public views must be rendered via $this->themeService->view() — not CI4's view(). This renders the template inside the active theme's layout.tpl. All data array keys become template variables.
404 Handling
throw new CodeIgniterExceptionsPageNotFoundException('Item not found');
Directory Structure
plugins/MyPlugin/Controllers/
├── Main.php
└── Admin/
├── Dashboard.php
├── Items.php
└── Settings.php