Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Get controller name from layout

I have the follow routes to different controllers and actions, that all shows the same layout and different views, example:

http://<my domain>/controllername1/action1 
http://<my domain>/controllername1/ 
http://<my domain>/controllername2/action1
http://<my domain>/controllername3/action1

How can I get the controller name that loads the Layout in the Layout code?, something that returns: "controllername1", "controllername2" or "controllername3"

The goal is to identify in which section I'm of my site and make some customization in layout.

I checked similar replies but are for old versions of Zend Framework.

Clarification: The idea is to get the controller name from the Layout code, not pass it from the controller code. Maybe isn't possible? Other answers are for older versions of ZendFramework (beta versions), and maybe is a more straightforward way now.

Edited: more information

I can set in my Module.php file the follow code on onBootstrap($e):

public function onBootstrap($e)
{
    // (...) Other code

    $application = $e->getParam('application');
    $viewModel = $application->getMvcEvent()->getViewModel();

    // Parsing URI to get controller name
    $viewModel->controllerName = trim($_SERVER['REQUEST_URI'],'/');
    if (substr_count($viewModel->controllerName, '/')) {
        $viewModel->controllerName = substr($viewModel->controllerName, 0, strpos($viewModel->controllerName, '/'));
     }
 }

And then from the Layout code use it as follow:

echo $this->layout()->controllerName;

The first problem is that the follow piece of code should be replaced with something (more "beautiful") using ZF2 functions to get Controller name:

(...)
// Parsing URI to get controller name
$viewModel->controllerName = trim($_SERVER['REQUEST_URI'],'/');
if (substr_count($viewModel->controllerName, '/')) {
    $viewModel->controllerName = substr($viewModel->controllerName, 0, strpos($viewModel->controllerName, '/'));
}

I want to avoid inject the Controller name from all controllers/actions: that is solved by using Module.php, but maybe is a more direct way.

Thanks!

like image 329
leticia Avatar asked May 14 '26 15:05

leticia


1 Answers

You're looking for this link: How to get the controller name, action name in Zend Framework 2

$this->getEvent()->getRouteMatch()->getParam('action', 'index'); 
$this->getEvent()->getRouteMatch()->getParam('controller', 'index');

Otherwise you have the same question (and answer(s)) here : ZF2 - Get controller name into layout/views

MvcEvent – get NAMESPACE / Module Name from Layout http://samsonasik.wordpress.com/2012/07/27/zend-framework-2-mvcevent-layout-view-get-namespace/

I didn't test but it seems correct : http://pastebin.com/HXbVRwTi

like image 67
Remi Thomas Avatar answered May 19 '26 02:05

Remi Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!