Hi all, I'm in the process of learning Phalcon and I've come across a roadblock. I've spent 9 hours in total this week researching this and I've made 0 progress so I decided to ask here.
Currently in my application (not micro), I have hard-coded checks in my controllers to see if a user has logged in from session. I want to make use of middleware to 'wrap' certain routes to require a user to be logged in to access, but I'm really struggling finding an implementation that works.
So far I've tried using a SecurityPlugin found in INVO, following this tutorial: https://php-phalcon-docs.readthedocs.io/en/latest/reference/tutorial-invo-2.html. My current code is identical to what's shown in the tutorial. This doesn't work for seemingly no reason as I can still access private resources when logged out/no session.
Does anyone have any useful resources I can look into? The Phalcon docs leaves me with more questions than answers in regards to Dispatcher, Event Manager, and Micro. I'm starting to get tired seeing the same links when searching for answers.
For context I created the project using Phalcon DevTools (4.0.3) running on Phalcon version 4.0.6. Fyi I can't clone Vokura to modify or use composer modules made by other people.
The following code works, so I know it's not an issue with attaching a plugin to the event manager.
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
class TestPlugin extends Injectable {
public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher) {
$auth = $this->session->get('AUTH_ID');
if(!$auth) {
$this->flashSession->error("You are not authenticated");
} else {
$this->flashSession->error("You are authenticated");
}
}
}
In services.php:
$di->set('dispatcher', function() use ($di) {
$eventsManager = $di->getShared("eventsManager");
$eventsManager->attach('dispatch:beforeExecuteRoute', new TestPlugin);
$dispatcher = new Dispatcher;
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});