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

Hi,
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?

Regards,
Kemal

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.

You might look into this thread

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

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

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.

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 .

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

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>
            </neos:link.node>

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?

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