Create a Menu section on Xibo CMS

Hi,

i’m using the Xibo CMS based on your Slim Framework.
I want to customize it adding a new menu section.

The Xibo support suggest me to create 2 new files in the ‘custom’ folder:
MyMiddleware.php and MyTest.php that is the controller.

this is the MyMiddleware file

<?php
namespace Xibo\Custom;

use Slim\Middleware;

/**
 * Class MyMiddleware
 * @package Xibo\custom
 *
 * Included by instantiation in `settings.php`
 */
class MyMiddleware extends Middleware
{
    public function call()
    {
        $myfile = fopen("log123.txt", "w") or die("Unable to open file!");
        $txt = "Prima\n";
        fwrite($myfile, $txt);

        $app = $this->getApplication();

        // Register some new routes
        $app->get('/mytest/view', '\Xibo\Custom\MyTest:testView')->setName('mytest.view');

        $txt = "Dopo\n";
        fwrite($myfile, $txt);
        fclose($myfile);

        // Register a new controller with DI
        // This Controller uses the CMS standard set of dependencies. Your controller can
        // use any dependencies it requires.
        // If you want to inject Factory objects, be wary of circular references.
        $app->container->singleton('\Xibo\Custom\MyTest', function($container) {
            return new \Xibo\Custom\MyTest(
                $container->logService,
                $container->sanitizerService,
                $container->state,
                $container->user,
                $container->helpService,
                $container->dateService,
                $container->configService
            );
        });

        // Next middleware
        $this->next->call();
    }
}

and this is the MyTest.php file:

<?php
namespace Xibo\Custom;

use Xibo\Controller\Base;
use Xibo\Service\ConfigServiceInterface;
use Xibo\Service\DateServiceInterface;
use Xibo\Service\LogServiceInterface;
use Xibo\Service\SanitizerServiceInterface;

/**
 * Class MyController
 * @package Xibo\Custom
 */
class MyTest extends Base
{
    /**
     * Set common dependencies.
     * @param LogServiceInterface $log
     * @param SanitizerServiceInterface $sanitizerService
     * @param \Xibo\Helper\ApplicationState $state
     * @param \Xibo\Entity\User $user
     * @param \Xibo\Service\HelpServiceInterface $help
     * @param DateServiceInterface $date
     * @param ConfigServiceInterface $config
     */
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config)
    {
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);
    }

    /**
     * Display Page for Test View
     */
    public function testView()
    {
        // Call to render the template
        //$this->getState()->template = 'about-page';
        //$this->getState()->setData([]); /* Data array to provide to the template */
        $this->setNoOutput(true);
        echo 'String Output';
    }
}

in the authed.twig file i’ve added the new menu section:

{% if currentUser.routeViewable("/mytest") %}
                            <li class="sidebar-list"><a href="{{ urlFor("mytest.view") }}">{% trans "Test" %}</a></li>
                        {% endif %}

But when i try to open the CMS it shows this error:
[2016-08-09 09:05:20] WEB.ERROR: An exception has been thrown during the rendering of a template ("Named route not found for name: mytest.view") in "authed.twig" at line 118. Exception Type: Twig_Error_Runtime [] {"uid":"610e147","method":"GET","route":"/dashboard/status","userId":1}

I am already contacting the Xibo support to solve this problem, but can you also help me to understand this issue?

Thank you very much

The error messages says that mytest.view is not being registered in the router.

where is your middleware being added to the app ? I don’t see it anywhere.

Hi geggleto,

i add the middleware in the web\settings.php file:

<?php

/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo - and is automatically generated by the installer
 *
 * You should not need to edit this file, unless your SQL connection details have changed.
 */

defined('XIBO') or die(__("Sorry, you are not allowed to directly access this page.") . "<br />" . __("Please press the back button in your browser."));

global $dbhost;
global $dbuser;
global $dbpass;
global $dbname;

$dbhost = 'mysql';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxx';
$dbname = 'xxxxxxxx';

define('SECRET_KEY', 'xxxxxxxxxx');

// Additional Monolog handlers/processors to be registered
$logHandlers = [new \Monolog\Handler\StreamHandler(PROJECT_ROOT . '/log12.txt')];
// $logProcessors = [];


// Additional middleware
    $middleware = [new \Xibo\Custom\MyMiddleware()];
// $authentication = ;


This is not adding the middleware… this is defining an array of middleware.

Where is the $middleware attribute being iterated and added to Slim ?