Custom panel page (action function cannot be called statically), better way to do this?

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);
      }
    }

You can find working examples here:

Hope that helps

So basically there is no cleaner way to write this?

Thanks for the answer though!

At least I don’t know a better way…

However, you might also want to check out this: https://github.com/LCD344/Kirby-userManager-panel-extention

Allright! Thanks!

I will check that out.
Meanwhile, this question can be marked as solved! :slight_smile:

I will do that but you can change the category yourself in the future. Click on the pencil icon in the headline and change the category.

1 Like