I’m usign the latest Laminas framework and I want to know how to get the name of the module, controller and action? I need those names in a view helper.
Thanks in advance!
This information is not available by default, as it is not required in a template in a regular application. So the question is, why do you need this information on the view layer?
@ froschdesign
RBAC
I have a view helper that shows user information in the views of the different modules and I am implementing an RBAC library that must check whether or not the user can access a certain action or controller or module of the website.
Then set this information for the view helper during creation in factory if it is required there.
But this does not happen in the view layer?!
you need get route from your request and after that you can get those:
$controllerName = explode("\\", $routeMatch->getParam("controller"))[2];
$actionName = $routeMatch->getParam("action");
$actionName = str_replace("-", "", lcfirst(ucwords($actionName, "-")));
$moduleName = explode("\\", $routeMatch->getParam("controller"))[0];
Hi @dev.lubinets,
I’ve tried your code and found an issue. Unexpected method called getParam() on variable $routeMatch. Can you please copy and paste the full code? Thanks!
public function userHasAuthentication(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
if (!empty($routeMatch)) {
$controllerName = explode("\\", $routeMatch->getParam("controller"))[2];
$actionName = $routeMatch->getParam("action");
$actionName = str_replace("-", "", lcfirst(ucwords($actionName, "-")));
$moduleName = explode("\\", $routeMatch->getParam("controller"))[0];
if (($controllerName !== "UserController" && $actionName !== "index") ||
($controllerName !== "UserController" && $actionName !== "logout")) {
$result = $this->getLoginService()->filterAccess(
$moduleName,
$controllerName,
$actionName,
$taskName = "*"
);
//another our code
}
}
}
I have used that code in custom LoginListener.
Try to use __FUNCTION__
for get current action in action)
__CLASS__
for controller
but for module, I think you can get the namespace and parse it.
Hi @dev.lubinets ,
The prescription which you’ve provided will not be going to help our friend @lokotek. As our friend wants to access the information in the view. So, the suggestion given by @froschdesign can be found in the provided references.
Create A ViewHelper.
Create A factory for it.
Get Route information.
Thanks!