Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Detecting a 404 [SOLVED]


Go to End


8 Posts   1564 Views

Avatar
Eurisko

Community Member, 27 Posts

17 August 2015 at 10:33pm

Hi

I've created a module that track a visitors path through my site.

I add it with

_config.php

Object::add_extension('Page', 'VisitorExtension');

and the extension looks like
VisitorExtension.php

<?php
    class VisitorExtension extends DataExtension
    {
        private static $has_one = array (
            'Visitor' => 'Visitor'
        );

        function __destruct()
        {
            // Only way I can see of adding module javascript after the main page javascript (So after the jquery include)
            // is to put the requirement in the __destruct
            Requirements::javascript( "moduleVisitorTracker/javascript/visitor-tracker.js" );
        }

        public function contentControllerInit( $controller )
        {
            if ( ! Director::is_ajax() )
            {
                // Find or create the visitor record
                    $visitor = Visitor::initVisitor();

                // Log the arrival of this visitor to this page
                    $visitor->logPageArrival();
            }
        }
    }

The problem is, it's also picking up requests by IE for favicon and other nonsense. Is there a way to detect if the page is a 404 (much the same way I detect for ajax) so I can not track those calls?

Thanks

Avatar
Pyromanik

Community Member, 419 Posts

19 August 2015 at 2:45am

Avatar
Eurisko

Community Member, 27 Posts

19 August 2015 at 2:47am

You beauty. Thanks again Pyro. You're like a an API glossary :)

Avatar
Eurisko

Community Member, 27 Posts

19 August 2015 at 3:01am

That function doesn't return true on a 404. I did a print_r of the $controller->getResponse() and it's showing code 200.

Avatar
Pyromanik

Community Member, 419 Posts

19 August 2015 at 9:22pm

You'll have to catch it later, after $this->httpError() has been called.
You need to catch in a number of places (eg. no rule match=404, controller can't handleAction=404, custom code in controller can = 404, etc.)
or use a postRequestProcessor (which seems to be a little known but quite neat functionality).

Avatar
Eurisko

Community Member, 27 Posts

19 August 2015 at 9:42pm

Nice! You always come through Pyro :) That works a treat now. For anyone else I changed my code to

<?php
    class VisitorExtension extends DataExtension implements RequestFilter
    {
        private static $has_one = array (
            'Visitor' => 'Visitor'
        );

        function __destruct()
        {
            // Only way I can see of adding module javascript after the main page javascript (So after the jquery include)
            // is to put the requirement in the __destruct
            Requirements::javascript( "moduleVisitorTracker/javascript/visitor-tracker.js" );
        }

        public function contentControllerInit( $controller )
        {
        }

        public function preRequest( SS_HTTPRequest $request, Session $session, DataModel $model )
        {
        }

        public function postRequest( SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model )
        {
            if  ( ! $response->isError() && ! Director::is_ajax() )
            {
                // Find or create the visitor record
                    $visitor = Visitor::initVisitor();

                // Log the arrival of this visitor to this page
                    $visitor->logPageArrival();
            }
        }
    }

and added the following to my config.yml

Injector:
  RequestProcessor:
    properties:
      filters:
        - '%$VisitorExtension'

Avatar
Eurisko

Community Member, 27 Posts

19 August 2015 at 9:42pm

Nice! You always come through Pyro :) That works a treat now. For anyone else I changed my code to

<?php
    class VisitorExtension extends DataExtension implements RequestFilter
    {
        private static $has_one = array (
            'Visitor' => 'Visitor'
        );

        function __destruct()
        {
            // Only way I can see of adding module javascript after the main page javascript (So after the jquery include)
            // is to put the requirement in the __destruct
            Requirements::javascript( "moduleVisitorTracker/javascript/visitor-tracker.js" );
        }

        public function contentControllerInit( $controller )
        {
        }

        public function preRequest( SS_HTTPRequest $request, Session $session, DataModel $model )
        {
        }

        public function postRequest( SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model )
        {
            if  ( ! $response->isError() && ! Director::is_ajax() )
            {
                // Find or create the visitor record
                    $visitor = Visitor::initVisitor();

                // Log the arrival of this visitor to this page
                    $visitor->logPageArrival();
            }
        }
    }

and added the following to my config.yml

Injector:
  RequestProcessor:
    properties:
      filters:
        - '%$VisitorExtension'

Avatar
Pyromanik

Community Member, 419 Posts

20 August 2015 at 1:12am

Edited: 20/08/2015 1:13am

Based in Europe? Tap into a wealth of knowledge and pick up some neat tricks like this from my friends at http://silverstripe-europe.org