Joomla 4 - getActive Menu item in Controller

633 views
Skip to first unread message

Glenn Arkell

unread,
Jan 2, 2021, 10:28:29 PM1/2/21
to Joomla! General Development
I have managed to update a simple component to Joomla 4 still using the MVC file structure and am now learning the new file structure layout with namespaces etc.  When I have processed a record in my model and return to the controller, my redirects don't seem to work as I was expecting.  I'm guessing this is due to my lack of knowledge around the new Router?
So, when I return to the controller I want it to redirect to the menu item used (if it exists) using the my old code as follows:

$menu = Factory::getApplication()->getMenu();
$item = $menu->getActive();
$this->setRedirect(Route::_($item->link, false));

The menuitem object always returns the default and not the actual menuitem used prior to the processing.

I've basically copied the Service/Router from com_banner.  Any advice/guidance much appreciated.  Cheers.

Glenn

Glenn Arkell

unread,
Jan 10, 2021, 6:35:17 PM1/10/21
to Joomla! General Development
Well I'm not entirely sure, it now works with my redirection and I think it was in the router.  So firstly I copied the router from com_content rather than com_banner and removed the id reference line from the multiple record listing.  This makes some sense as you want all records for the list view rather than just a single record based on the id.  Anyway, works fine.

        $attendances = new RouterViewConfiguration('attendances');
        //$attendances->setKey('id');   // don't set a key for list view
        $this->registerView($attendances);


Glenn
Reply all
Reply to author
Forward
0 new messages