My project has a serious bug.
my phalcon project operate with docker for mac.
so, If you go to https://{my_host_name}/
,IndexController passes twice .For example in the following cases, two new users will be added in DB. What's happen!?
and, sometimes IndexController passes three times as soon as docker-compose start
completes.
Is this due to the dispatcher or phalcon's version or docker's version?
My controller was described below.
class IndexController extends Controller
{
/**
* GET /
*/
public function indexAction()
{
// it's test
$user = new User();
$user->create();
}
public function afterExecuteRoute()
{
if (!$this->dispatcher->isFinished()) {
return;
}
// css and js roading
$cssHeaderCollection = $this->assets->collection("cssHeader");
foreach (self::CSS_LIST as $css) {
$cssHeaderCollection->addCss($css . "?" . self::VERSION);
}
$jsHeaderCollection = $this->assets->collection("jsHeader");
foreach (self::JS_LIST as $js) {
$jsHeaderCollection->addJs($js . "?" . self::VERSION);
}
}
public function beforeExecuteRoute(){
// user_setting
$this->user = Sentinel::check();
if ($this->user) {
$this->role = Services::getService('User')->getRole($this->user->id);
}
$this->view->user = $this->user;
$this->view->role = $this->role;
}
}
And, My di's was described below.
$di['router'] = function () {
$router = new Phalcon\Mvc\Router();
$router->setDefaultModule('frontend');
$router->setDefaultNamespace('Application\Frontend\Controllers');
$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);
$router->removeExtraSlashes(true);
}
$di['dispatcher'] = function () {
$eventsManager = new Manager();
$eventsManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) {
if ($exception->getMessage() && (getenv("MODE") == "development" || getenv("MODE") == "staging")) {
var_dump($exception->getMessage());
}
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward([
'namespace' => 'Application\Frontend\Controllers',
'module' => 'frontend',
'controller' => 'errors',
'action' => 'show404'
]);
return false;
default:
if (getenv("MODE") == "production") {
$dispatcher->forward([
'namespace' => 'Application\Frontend\Controllers',
'module' => 'frontend',
'controller' => 'errors',
'action' => 'show500'
]);
}
return false;
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setDefaultNamespace('Application\Frontend\Controllers');
return $dispatcher;
};