Hi Kirby users!
I was trying to add a custom page to the panel by creating a new plugin.
After reading some source code, fiddling around and using this great answer.
I got something close to a custom page.
However, when routing the page I get an error for statically calling a function from the controller in the action
parameter. Now, for the question:
From my knowledge/research I found that in the action can only be called statically (at least as a string parameter).
I fixed this by creating a function as the action parameter (see example below). Is there a better/neater way to call this function non-statically?
Sample files:
plugin-name.php:
<?php // site/plugins/plugin-name/plugin-name.php
if (class_exists('panel')) {
require_once __DIR__ . DS . 'controller.php';
require_once __DIR__ . DS . 'view.php';
panel()->routes([
[
'pattern' => 'plugin',
'method' => 'GET|POST',
'filter' => 'auth',
'controller' => '\pluginName\Controller',
'action' => function() {
$plugin = new \PluginName\Controller;
return $plugin->config();
}
]
]);
}
view.php:
<?php // site/plugins/plugin-name/view.php
namespace PluginName;
class View extends \Kirby\Panel\View
{
public function __construct($file, $data = [])
{
$this->_root = kirby()->roots()->plugins() . DS . 'plugin-name' . DS . 'views';
$this->_file = $file;
$this->_data = $data;
}
}
Controller.php:
<?php // site/plugins/plugin-name/controller.php
namespace PluginName;
use Exception;
class Controller extends \Kirby\Panel\Controllers\Base
{
public function config()
{
$configuration = 'fetch configuration';
$form =
$this->form('config', compact('configuration'),
function ($form) use ($configuration) {
// this will be called only when the form POSTs to this action
try {
// save configuration…
$this->notify(':)');
} catch (Exception $e) {
$this->alert('Something went wrong!');
}
$this->redirect(); // go back to dashboard
}
);
return $this->modal('config', compact('configuration', 'form'));
}
public function form($id, $data = array(), $submit = null)
{
$file = kirby()->roots()->plugins() . DS . 'plugin-name' . DS . 'forms' . DS . $id . '.php';
return panel()->form($file, $data, $submit);
}
public function view($file, $data = array())
{
return new View($file, $data);
}
}