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

flash messages display well in some controllers, but empty in others

Hi! I'm using phalcon Version => 2.0.9 Build Date => Jan 7 2016 13:53:17 Powered by Zephir => Version 0.8.0a

This is my code, and is almost equal to another controller where it works perfectly:

    // $this->view->disable();

    if ($deleted) {
        $this->flash->success('User was deleted successfully');
    } else {
        $this->flash->success('You cant delete this user');
    }

    // $this->view->disable();

    $this->response->redirect('admin/users');
    return;

In my index.php I have:

// Flash Data (Temporary Data)
$di->set('flash', function() {
    $flash = new \Phalcon\Flash\Session([ // cosas de Bootstrap
        'error' => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice' => 'alert alert-info',
        'warning' => 'alert alert-warning'
    ]);
    return $flash;
});

And in my view(volt):

...more code...
</div>

{{ flash.output() }}

{% block content %}
{% endblock %}

</body>
</html>

In my DashboardController.php this code works perfectly and show the messages in a green or red background. But when I use those messages in my AdminController.php the background shows with the right color but no message is displayed.

I think I didn't touched anything, because my AdminController, with his views is almost a copy of the another but with more capabilities. What I'm doing wrong? Thanks in advance.

Could you show us the code in your AdminController method? Perhaps there is some logic that breaks things?



4.5k

Could you show us the code in your AdminController method? Perhaps there is some logic that breaks things? Of course,

public function deleteUserAction($userId)
{
    $deleted = false;
    $user = Users::findFirstById($userId);
    $userId = $user->getId();
    $normaluser = NormalUsers::find("user_id = $userId");

    $normalUserrray = $normaluser->toArray();
    $normalUsersIds = [];
    foreach ($normalUserrray as $normalUser) {
        $normalUsersIds[] = $normalUser['id'];
    }

    if (!empty($normalUsersIds)) {
        $userAdverts = userAdverts::find([
            'user_id IN ({user_id:array})', // aquĆ­ decimos que la id sale de un array, array es un tipo de bind
            'bind' => [
                'user_id' => $normalUsersIds
            ]
        ]);
    }

    if (!in_array($user->email, $this->nonDeleteableUsers)) {
        $user->delete();

        foreach ($userAdverts as $userAdvert) {
            $advert = Adverts::find($userAdvert->getAdvertId());
            $advert->delete();
            $userAdvert->delete();
        }

        $deleted = true;
    }

    // $this->view->disable();

    if ($deleted) {
        $this->flash->success('User was deleted successfully');
    } else {
        $this->flash->success('You cant delete this user');
    }

    // $this->view->disable();

    $this->response->redirect('admin/users');
    return;
}

I've read some post on this topic, but in this case the $this->disable->view() does not work.

I had some problems similar. I notice that the final view should have {{ content() }} to output the previous rendered content which shows the messages coming from flash service. Ex:

in IndexController.php


class IndexController extends ControllerBase
{

    public function indexAction()
    {

    }

    public function homeAction(){

    }

}

In home.volt

{% extends "layouts/base.volt" %}

Hello World

{% block container %}
    {{ content() }}
{% endblock %}

Hope this helps. Thanks.



4.5k

Thanks for your reply but this does not solve my problem.

I had some problems similar. I notice that the final view should have {{ content() }} to output the previous rendered content which shows the messages coming from flash service. Ex:

in IndexController.php


class IndexController extends ControllerBase
{

   public function indexAction()
   {

   }

   public function homeAction(){

   }

}

In home.volt

{% extends "layouts/base.volt" %}

Hello World

{% block container %}
  {{ content() }}
{% endblock %}

Hope this helps. Thanks.



4.5k

Sorry to all, but phalcon was working perfectly. The problem was that some divs in bootstrap and CSS configs hide the text. The web page renders perfectly, I saw this because a friend came to my house yesterday to help. So many thanks to all.