[SOLVED] Controller for all "Page" node-typed elements

(kemal özkan) #1

is there a way to register a controller for every page-load. What I want to do is a section named "mostly viewed pages"
So, what I want to build a controller which checks a get parameter like ?search_term=huhu

  • if the get parameter is present
    -get the node id, check the type: does it has a property counter?
  • add +1 to counter and save it.

What is the best way to do it?


(Benjamin Klix) #2

I would create a new db table / Domain Model that stores URI (or Identifier) + Count (+ whatever you need) into a separate table. Then you can use a custom FrontendNodeRoutePartHandler that extends the original one (from neos) and update the page count for the requested page. You can also restrict this to only count in the live workspace. Hope this helps.

(Soren Malling) #3

You might look into this thread

(kemal özkan) #4

Thank you both for the tips, I will update the post with a complete solutin when I’m done.

(kemal özkan) #5

Hi Benjamin, I did everything you mention but there is a problem by storing data in the database.
When I try to add data manually with XCommandController it works. But unfortunately, the same code (create entity and initialize,create repository and add) doesn’t work when the Repository is called by CustomRoutePartHandler in the function convertRequestPathToNode. Do you have any ideas what is missing?

Thank you

(Ludwig Rafelsberger) #6

Wild guess: You (your visitors) are requesting the affected locations via a GET request - which is a safe request - and thus Neos does not start “the part of its persistence that persists changes”. That is by design, as a safe request should not alter any data.

Of course, your case is perfectly valid: such small data alterations, as visitor-counting, logging, etc… might be done even on safe requests. Now I do not really know the Neos / Flow code well, but you could look for a method like PersistenceManager->persistAll() and call that explicitely.

Exclaimer: I don’t know anything, neither your case nor Neos by heart. Just guessing.

(kemal özkan) #7

Thank you for the tipp. I already try the persisntanceManager->PersistAll() in the Add funciton of the Repository.

I’m still (looking for an answer)/ trying different variations .

(Bastian Waidelich) #8

I would really avoid that. GET requests are not meant to change the server state and even for these minor things you might run into trouble if you do so.
To name a few caveats:

  • GET requests are meant to be cacheable – if the browser or a proxy caches the request your view-counter won’t be called
  • GET requests are allowed to be followed by search engines… do you want to count the google bot? :wink:

I would suggest to increase the view counter via an AJAX (POST) request when the page has been loaded.
See [SOLVED] Counter for survey or page views for a related question

(kemal özkan) #9

Thank you all. I implemented following steps:

  1. Create Counter-Controller ,-Model & -Repository (node_id, counter)

  2. The logic for inserting a new entry or updating the existing entry (++counter)

<neos:link.node node="node://{counter.node_id}" class="results__link">
                <li class="results__item">{counter.node_id}</li>

The challenge now is to link to the nodes. When I try to link to the node via its identifier I got some error "node:// URI conversion requires a context node to be passed"
Is there any solution for this?

(kemal özkan) #10

I use a signal (afterControllerInvocation) and manipulate the node Property via NodeDataRepository.