Bootstrap loaded twice.

Hello!

I have a weird problem where my bootstrap file is run twice. I discovered this since I'm logging errors in the DB, and if I throw an error, two records appear in the database.

I'm not sure how this error occurs, so I'd like some help figuring out. My entire app looks like this right now (no controllers, no views, nothing else) except for the LogError model, which is really simple:

<?php

// Define constants.
if(!defined("APPLICATION_PATH")) {
    define("APPLICATION_PATH", realpath(__DIR__ . "/../App"));
}

include APPLICATION_PATH . "/configs/config.php";

try {
    // Load configuration
    $config = new \Phalcon\Config($config);

    # Loader
    $loader = new \Phalcon\Loader();

    # Register namespaces
    $loader->registerNamespaces(
        array(
            'App' => APPLICATION_PATH . "/"
        )
    );

    # Execute loader registration
    $loader->register();

    # Dependency Injector
    $dependencyInjector = new \Phalcon\DI\FactoryDefault();

    # Make config available throughout system.
    $dependencyInjector->set('config', function() use ($config) {
        return $config;
    });

    # Dispatcher
    $dependencyInjector->set('dispatcher', function() use ($dependencyInjector) {
        $eventsManager = $dependencyInjector->getShared('eventsManager');
        $dispatcher = new \Phalcon\Mvc\Dispatcher();

        $dispatcher->setEventsManager($eventsManager);

        return $dispatcher;
    });
    $application = new \Phalcon\Mvc\Application();
    $application->setDI($dependencyInjector);

    # Force error for trouble shooting.
    throw new \Exception("Forced error.");
    #echo $application->handle()->getContent();

} catch(\Exception $e) {
    $log = new \App\Models\LogError();
    $log->message = $e->getMessage();
    $log->save();
}

For your reference, my access.log only logs one request, so there must be some kind of redirect in Phalcon running.

Best, dimhoLt



1.3k

Are you sure that there is no mistake in your exception handling?

try putting echos in the code like "boostrap line: xx" and see if that is printed out twice as well.



13.8k

Hello naNuke, and thanks for your reply. I can't be certain that there is no error on my side, but as you can see in the code in the example, I throw an error before the application is handled, and catch it in the catch-block in the index.php-file immediately, so I've tried to remove any possible error on my part.

Regarding your advice to echo, I've tried that, and it only prints once, but I think that's because there's some kind of internal redirect within Phalcon that I can't find. I tried writing to the error log instead of creating my database object in the catch-clause, and it also prints once, but still, the Apache access log only gets one line.

Are you sure your apache's rewrite config is correct? Try to check all of responses which your server returns to browser per request, including favicon)) Maybe one of them returns a whole page, instead of requested file?



31.6k

Again, again and again :) Check my answer here https://github.com/phalcon/cphalcon/issues/1598 . Browser is a bitch sometimes :)) Let me know if it worked for you. About one year ago i spent a full working day on this kind of problem

I had similar problem last week. My case was that i make a hook on application:afterHandleRequest and if you got DI instance inside that event handler its execute the bootstrap again.



13.8k

Hey guys, sorry for the late reply. Thanks to all of you for taking your time to help me!

serafimovich and Calin Rada, you were spot on. The reason for this was the favicon. I added a dummy-file for it and the double logs disappeared. Thanks!!

Using dummy files sounds like bad design practice. Why not solve the problem for once and for all?

Add this lines to your.htaccess in the public folder. RewriteCond %{REQUESTFILENAME}.iso !-f RewriteCond %{REQUESTFILENAME}.png !-f RewriteCond %{REQUESTFILENAME}.jpg !-f RewriteCond %{REQUESTFILENAME}.js !-f RewriteCond %{REQUESTFILENAME}.css !-f RewriteCond %{REQUESTFILENAME}.gif !-f

I'm not very familiar with htaccess. But this is what I came up with. Works for me.

Greetz



13.8k

Hello DaanBiesterbos. Yeah, it would be if it was a permanent solution. I want to log missing files, so I want to keep this behaviour however, and I will have a favicon at a later time, so when developing, I added a dummy file.

Hmm. I understand. Good luck. Perhaps I can save you a headache some day. The problem I had with this solution is that the CSRF tokens expire when the bootstrap is loaded twice. Perhaps it could be solved by ensuring the token is not created until a route is matched. But I felt like having to hack into something that already works. If you have a better solution I would like to know. :-)

Hmm.. I ran into some problems with htaccess rules I mentioned.

Changed it to RewriteCond $1 !.(gif|jpg|png|ico|css|js|swf|wav|mp3|less). That seems to work better.

Would'nt want somebody smashing his head on the table for nothing.

Hello. I had same problem with loading page twice. So better if Phalcon make changes in default htaccess file in documentation - he needs change string from: RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] to: RewriteRule !.(xsd|wsdl|xml|pdf|js|swf|ico|txt|gif|jpg|jpeg|bmp|tif|png|css|rss|zip|wav|php|html|tar|mp3|mp4|jar|avi|.gz)$ index.php [QSA,L]

Thank you.

I have the same problem under google chrome.