2

I'm creating a module X that will be instantiated inside a controller module. How can I get the name of that controller in a method of module X?

This module is not a controller, nor a view or layout.

An example. This is an action in the controller:

public function indexAction {
    $parser = new Parser();
}

And this my new module Parser, where I need to know the controller's name.

public function __construct() {
    $controller_name = ???
}
Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Why can't you just pass in the controller's name when you construct the `Parser`? e.g. `$parser = new Parser(__CLASS__);` – Ankh Aug 07 '15 at 08:03
  • @Svengali I wanted to avoid such thing, just have a bare call to the class. But this could be a far simpler solution indeed. – Jaime G. Wong Aug 08 '15 at 00:07

2 Answers2

3

For such dependencies you should use a factory to create your service instance. Then you can inject whatever you want in there, also a controller name. Your ParserFactory could for example look like this:

<?php
namespace Application\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Service\Parser

class ParserFactory implements FactoryInterface
{
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return Parser
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $routeMatch = $serviceLocator->get('Application')->getMvcEvent()->getRouteMatch();
        $controllerName = $routeMatch->getParam('controller');
        $parser = new Parser($controllerName);
        return $parser;
    }
}

Your Parser class:

<?php
namespace Application\Service;

class Parser
{
    /**
     * @param  string $controllerName
     */
    public function __construct($controllerName)
    {
        //... use your controller name ...
    }
}

Register your factory in module.config.php like this:

'service_manager' => array(
    'factories' => array(
        'Parser' => 'Application\Factory\ParserFactory',
    )
)

Get your service where you need it from the ServiceManager like this:

$parser = $serviceManager->get('Parser');
Wilt
  • 41,477
  • 12
  • 152
  • 203
0

I think this has been asked before but I think you do:

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

You should just be able to grab it all from the router.

EDIT:

Yeah, check these out:

How to get the controller name, action name in Zend Framework 2

ZF2 - Get controller name into layout/views

ZF2: Get module name (or route) in the application layout for menu highlight

Community
  • 1
  • 1
Karl Wilbur
  • 5,898
  • 3
  • 44
  • 54
  • Again, my module is NOT a controller, view nor layout. – Jaime G. Wong Aug 06 '15 at 22:08
  • It doesn't matter. You can still grab it from the router. – Karl Wilbur Aug 06 '15 at 22:55
  • On that third link you've added, it's done on the module's Module.php file and added as a variable to the layout (using getViewModel()). I need to know the name to use it on my module's method, not its Module.php, nor a view, controller or layout. – Jaime G. Wong Aug 06 '15 at 23:02
  • You could get it from the router when your module gets loaded (onBootstrap), then set it as an accessible property of your module. There is a number of ways you could do this dictated by the needs and conventions of your application ...but they will all likely end up asking the router for the current controller. – Karl Wilbur Aug 06 '15 at 23:18
  • Here's a random example form GitHub: https://github.com/srayner/CivAccess/blob/master/Module.php You would do something like that but also fetch the current controller name from getParam(). – Karl Wilbur Aug 06 '15 at 23:20
  • Hmmm... if I get the controller name on onBootstrap(), how can I save and read it later on my module? Since, at onBootstrap() time, my module hasn't been instantiated yet. – Jaime G. Wong Aug 06 '15 at 23:30