We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

internal server error when using plugin for dynamic menus

I am using a plugin made by myself for dynamically show the menu. Sadly it throws an 500 error and the content of the website is empty, no code shown using view-source:site in chrome

But when I remove this piece of code from my ControllerBase.php:

public function initialize()
{
    $this->content->setMenu((object) array("name" => "Home","url" => "/"));
    $this->content->setMenu((object) array("name" => "Registreren","url" => "/register"));
    $this->content->setMenu((object) array("name" => "Inloggen","url" => "/login"));
}

It will work again.

Help!

This is how my code looks like:

Url: /

IndexController.php:

<?php
    use Phalcon\Mvc\View;
    class IndexController extends ControllerBase
    {

        public function indexAction()
        {
            if($this->session->has("auth")){
                $this->dispatcher->forward(array(
                    "controller" => "Game",
                    "action" => "index",
                ));
            }
        }
    }

ControllerBase.php:

<?php

    use Phalcon\Mvc\Controller;

    class ControllerBase extends Controller
    {
        public function initialize(){
            $this->content->setMenu((object) array("name" => "Home","url" => "/"));
            $this->content->setMenu((object) array("name" => "Registreren","url" => "/register"));
            $this->content->setMenu((object) array("name" => "Inloggen","url" => "/login"));
        }
    }

Services.php (content plugin code):

$di->set("content",function(){
    return new content();
});

plugins/content.php:

<?php
class content extends Phalcon\Mvc\User\Component{
        public function setMenu($menu)
        {
            global $menus;
            if(!isset($menus)){
                $menus = array();
            }
            $menus[] = $menu;
            $that->view->setVar("menus",$menus);
        }
        public function emptyMenu(){
            global $menus;
            $menus = array();
        }
    }

view(for menus):

    <nav>
        {% if menus is defined %}
            <ul>
                {% for menu in menus %}
                    <li><a href="{{menu.url}}">{{menu.name}}</a></li>
                {% endfor %}
            </ul>
        {% endif %}
    </nav>


98.9k

Could you please upload your code to a repository on Github so it can be reproduced easily by someone else?



10.1k
Accepted
answer
edited Jul '14

I saw what I did wrong, I was using $that ($that was fetched from an parameter in the function) instead of $this in my plugin(plugins/content.php code) because I did not know how I could include the dependency Injector into my class but after that I did know, just by extending it with Phalcon\Mvc\User\Component.

Do you still want me to upload my code?

I also found out that $model->create(); will also trigger an 500 error if an foreign key constraint fails.

Hi, Please send me your code, I need it

I saw what I did wrong, I was using $that ($that was fetched from an parameter in the function) instead of $this in my plugin(plugins/content.php code) because I did not know how I could include the dependency Injector into my class but after that I did know, just by extending it with Phalcon\Mvc\User\Component.

Do you still want me to upload my code?

I also found out that $model->create(); will also trigger an 500 error if an foreign key constraint fails.