V8: page controller action is empty

I get a page object from a page view event and then try to get its action and parameters but the action returns empty. The page is ‘items’, action is ‘item’, parameter is ‘123’, i.e. /items/item/123. What’s wrong here?

    Events::addListener('on_page_view', function($event) {
        $page = $event->getPageObject();
        $page_controller = $page->getPageController();
        $page_action = $page_controller->getAction();
        \Log::Info('action: '.$page_action); // <- this returns nothing
    }

The getControllerActionPath() returns ‘items’ though, so that’s the right page.

Does anyone know how to get the action name from a visited page?

Anyone? Any clue? Anyone know?

I also tried $page_controller->getRequestAction() and it’s also empty

I’ve also tried with a block controller, i.e. I find the block on the page, get its controller then get its action - it’s also empty.

Ridiculous! How do I get an action from the URL then?

Is this for a single page? If so, there’s a pretty good example of how to do it here: Sending Data To and From a Controller into the Page View

If it’s for a block, you can try: Example: Passing Data from a custom URL into a Block's View Layer

If you have another use case, you may need to provide more details.

@Myq please have a loo at the very first post - I need to get the viewed page URL action and parameters in the event listener. I’ve tried both a PageController and a BlockController to get the action and they both return empty.

Looks like changing the event to on_before_render and getting $page = Page::getCurrentPage(); got me the page action and parameters, although not what I expected.

The page is ‘items’, action is ‘item’, parameter is ‘123’, i.e. /items/item/123. I have this in the controller:

    public function action_item($handle = null) 
    {
        $this->runAction('item');
        
        if ($handle) {
            $this->view($handle);
        }
    }

What I get from the event is:

action: view
parameters: array(2) {
[0]=>
string(4) "item"
[1]=>
string(3) "123"
}

Although I can live with that, but why is the action ‘view’, not ‘item’, while the ‘item’ is a parameter instead?

@linuxoid, you could use getRequestAction():

Events::addListener('on_before_render', function($event) {
    $page = Page::getCurrentPage();
    $page_controller = $page->getPageController();
    $page_action = $page_controller->getRequestAction();
    \Log::Info('action: '.$page_action); // <- returns `item'
});

Screenshot from 2021-09-08 17-15-06