1

I am having problems with a test implementation of a checkout, using the Dutch psp Mollie. The problem is not Mollie related. What is happening is that the index function below is sometimes called twice, resulting in two records in my sales table. I am a little at a loss why this is happening. Below I pasted my test code that results in the strange behaviour. I already tried to change the route, but to no avail. The same is happening, even if I move the code to the route /test/send, or when I link the /test stub to another function, like '' => 'send'.

The dataobject "Sale" has no extra code apart from the default $db and $has_something etc.

Update: I removed all the Mollie related code, and the application is still doing the same thing. I think the redirect has something to do with it, because when I remove it the problem does not surface (it only occurs the first reload after I remove it, after that, a reload just results in one row in the database). When the reload is in place it happens a whole lot more often (but I still can't really predict when or why).

Update2: I tried a fresh install, and still the same problem. As the site is hosted on a LEMP stack, I also tried a fresh install on a LAMP stack. Still the same problem...

Update3: I made a minimal example of code that still shows the same behavior, and updated the code below accordingly

class TestController extends Controller {

    public function index() {
        $sale = Sale::create();
        $sale->TicketName = "test ".date("Y-m-d H:i:s");
        $sale->write();
        return $this->redirect("http://other.site");
    }
}

The Sale dataobject

class Sale extends DataObject {
        private static $db = array(
                "TicketName"            => "Varchar(255)",
        );
}

class SaleAdmin extends ModelAdmin {
        private static $managed_models  = array('Sale');
        private static $url_segment     = 'Sales';
        private static $menu_title      = 'Sales';
}

And this is the route.yml

---
Name: myroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'test' : 'TestController'
6
  • You're still subclassing your TestController from Page_Controller. Have you tried to inherit from Controller instead, as suggested in my answer?
    – bummzack
    Commented May 29, 2016 at 7:41
  • @bummzack Yes, to no avail
    – jberculo
    Commented May 29, 2016 at 7:42
  • I doubt that it's related to the framework version, but could you post that as well? I used 3.3.2 for testing your setup and didn't get any duplicates. Maybe also check you .htaccess for any oddities?
    – bummzack
    Commented May 29, 2016 at 7:53
  • I just did a fresh install of 3.3.2. and added the code above, and I did get duplicates. I can reproduce it bij calling site/test like 10 times in a row (after the redirect has taken place). Then I see 15-18 rows in the Sale table.
    – jberculo
    Commented May 29, 2016 at 8:40
  • And I am now by default extending Controller instead of Page_Controller
    – jberculo
    Commented May 29, 2016 at 8:45

3 Answers 3

3

I don't think you need the index reference in the url_handlers static. index is called automatically if requests arrive at a controller without an entry in allowed_actions.

Another tip is that you may come across problems accessing your current instance of SS_HTTPRequest calling it as you are. Instead of $this->request->getVar('foo') add a $request arg to index(). SS automatically passes the current instance of SS_HTTPRequest to all controller methods as the first arg.

Regardless, depending on what the actual URL turned out to be when dumping payment->getPaymentUrl() I'd suspect that too. Have you tailed your webserver's access logs to see what the URL looks like whenever the second / double-up request occurs?

3
  • You're correct about the index reference. As a test, I moved the method to the /test/send route, but that did not make a difference. I also removed all the Mollie code, and replaced the redirect url with a generic link to a news site, and that did not change the behavior either.
    – jberculo
    Commented May 26, 2016 at 7:40
  • Oh, I did not check the access log, but in the network tab of the chrome tools it does not show multiple calls
    – jberculo
    Commented May 26, 2016 at 13:38
  • I found the problem, Unrelated to SilverStripe... See my answer & thanks for your help
    – jberculo
    Commented May 29, 2016 at 9:34
3

I found out what the problem is. It has got nothing to do with Silverstripe, but I noticed that my Chrome browser is calling sites that I enter in the location bar BEFORE I press enter. It also does that when I switch tabs. So yes, that WILL result in more than one call, and more than one row in the database...

It is the result of the prefetch feature of google chrome, that tries to cache pages before you navigate to them. I think it is a stupid feature that causes an awful lot of unnecessary traffic, but I sure as hell need to take it into account.

The strange part is, that the problem does not occur when there is no redirect, so that suggests that in these cases, chrome is showing the cached page without doing a new request. The redirect maybe invalidates that cache, forcing two hits on the url...

Also, it will probably not be an issue in a production environment, as the redirect will only be called after a form post, and not be entered in the navigation bar. But still...

Thank you all for your help! It is appreciated :)

0
2

I think your main problem is that you're subclassing from Page_Controller. You should subclass from Controller if your controller isn't a page-controller (eg. a controller that is linked to a SiteTree instance).

Something like this works for me and doesn't produce any duplicate entries:

class TestController extends Controller
{
    private static $allowed_actions = array(
        'result',
    );

    private static $url_handlers = array(
        'result/$Code' => 'result',
    );

    public function index() {
        // process request and write DataObject
    }

    public function result() {
        $code = $this->request->param('Code');
        // do soemthing with the result code
    }
}
3
  • Thanks. I tried this, but it does not change the behavior. I'll try with a fresh install and see if there is something else interfering...
    – jberculo
    Commented May 27, 2016 at 21:37
  • The fresh install did not change anything
    – jberculo
    Commented May 29, 2016 at 8:43
  • I found the problem, Unrelated to SilverStripe... See my answer & thanks for your help
    – jberculo
    Commented May 29, 2016 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.