Problem with redirections in phalcon

Hello everyone. I am building a sample app for myself and I am having a little problem with redirects. What I am basically trying to achieve is something like route filters in laravel. The code I have is:

FIlters Collection:

<?php
namespace Network\Filters;

use Phalcon\Mvc\User\Plugin;

class FilterGroup extends Plugin {

    private $_filters;

    public function __construct ()
    {
        $this->add('auth', new AuthFilter())
            ->add('csrf', new CsrfFilter());
    }

    public function add ($key, FilterInterface $filter)
    {
        if (!isset($this->_filters[$key])) {
            $this->_filters[$key] = $filter;

        }   
        return $this;   
    }

    public function remove ($key)
    {
        if (!isset($this->_filters[$key])) {
            unset($this->_filters[$key]);

        }   
        return $this;   
    }

    public function __get ($property)
    {
        if (isset($this->_filters[$property])) 
        {
            return $this->_filters[$property];
        }
    }

}

Authentication filter: <?php namespace Network\Filters;

use Phalcon\Text;
use Phalcon\Mvc\User\Plugin;

class AuthFilter extends Plugin implements FilterInterface
{
    public function filter (array $actions = null)
    {
        // die(var_dump($array[0] == '*')); {1}

        if (empty($actions) || 
            in_array(strtolower($this->dispatcher->getActionName()), array_map('strtolower', $actions)) || 
            $array[0] == '*') {
            // die(var_dump('test')); {2}

            if ($this->auth->isGuest()) {

                $this->response->redirect('');
                return false;               
            }
        }
    }

}

And an ordinary controller:

<?php

use Network\Forms\StatusForm;
use Network\Statuses\GetStatusesQuery;
use Network\Statuses\PostStatusCommand;

class StatusesController extends ControllerBase 
{
    private $_form;

    public function beforeExecuteRoute ($d)
    {
        $this->filters->auth->filter(['*']);

    }

    public function initialize ()
    {
        parent::initialize();
        $this->_form = new StatusForm();    
    }
    /**
     * Display all statuses
     * @return void
     */
    public function indexAction ()
    {

        $this->view->form = $this->_form;

        $this->view->statuses = $this->queryBus->execute(new GetStatusesQuery([
            'orderBy' => 'updatedAt',
            'orderDir' => 'desc',
            'limit' => 10,
            'currentPage' => $this->dispatcher->getParam('page') ? (int)$this->dispatcher->getParam('page') : 1
        ]));
    }

}

And a simple route: $this->addGet('/@{username}/statuses[/]?{page:[0-9]+}?', 'Statuses::index');

The problem is that when I filter, the redirection doesn't happen. I am guessing that a problem might be caused by the redirect method wich requires 2 http requests. (http://docs.phalconphp.com/pt/latest/reference/dispatching.html#forwarding-to-other-actions)

I also get a fatal error that a variable doesn't exist in my statuses/index.volt view file. But it will exist only if user is authenticated. This may also be causing an error.

When I used my first die(var_dump()), the condioon was true, but it never got to the second die(var_dump()). is there a way to make it work as I expect ?



31.2k

Hi,

so far as I understand, I would have write in Authentication filter:


if (!empty($actions) || ......