Is there any way to get the controller name and action name using a given uri?
Example:
uri: http://test/client/edit/48
Controller name => client Action name => edit
Is there any way to get the controller name and action name using a given uri?
Example:
uri: http://test/client/edit/48
Controller name => client Action name => edit
You can match your uri against the application router to get a RouteMatch
object.
$request = new \Zend\Http\Request();
$request->setUri($uri);
$router = $serviceLocator->get('Router');
$routeMatch = $router->match($request);
Now you can retrieve your params.
if ($routeMatch) {
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
}
In your controller add these:-
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
Then create a dispatch function in your controller
public function dispatch(Request $request, Response $response = null)
{
$controller = $this->params('controller');
$action = $this->params('action');
echo "Controller: " . $controller . " Action: " . $action;
}
Though late, but to help new visitors, resolved ans here,
ZF2: Get url parameters in controller
in short, this code should help,
$controller = $this->params()->fromRoute('RouteParamForController');