ZF2 - Get controller name into layout/views
ZF2 is out and so is the skeleton. This is adding on top of the skeleton so it should be your best example:
Inside Module.php
public function onBootstrap($e)
{
$e->getApplication()->getServiceManager()->get('translator');
$e->getApplication()->getServiceManager()->get('viewhelpermanager')->setFactory('controllerName', function($sm) use ($e) {
$viewHelper = new View\Helper\ControllerName($e->getRouteMatch());
return $viewHelper;
});
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
The actual ViewHelper:
// Application/View/Helper/ControllerName.php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class ControllerName extends AbstractHelper
{
protected $routeMatch;
public function __construct($routeMatch)
{
$this->routeMatch = $routeMatch;
}
public function __invoke()
{
if ($this->routeMatch) {
$controller = $this->routeMatch->getParam('controller', 'index');
return $controller;
}
}
}
Inside any of your views/layouts
echo $this->controllerName()
Zend Framework 2 Get controller name from layout
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
Zend Framework 2: Getting current controller name inside a module
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');
How to get controller and action name in zf2
you can't access these variables in controller __construct()
method, but you can access them in dispatch
method and onDispatch
method.
but if you would like to check whether action exist or not, in zf2 there is already a built in function for that notFoundAction as below
public function notFoundAction()
{
parent::notFoundAction();
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent("Action not found");
return $response;
}
but if you still like to do it manually you can do this using dispatch methods as follow
namespace Mynamespace\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Mvc\MvcEvent;
class IndexController extends AbstractActionController
{
public function __construct()
{
}
public function notFoundAction()
{
parent::notFoundAction();
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent("Action not found");
return $response;
}
public function dispatch(Request $request, Response $response = null)
{
/*
* any customize code here
*/
return parent::dispatch($request, $response);
}
public function onDispatch(MvcEvent $e)
{
$action = $this->params('action');
//alertnatively
//$routeMatch = $e->getRouteMatch();
//$action = $routeMatch->getParam('action', 'not-found');
if(!method_exists(__Class__, $action."Action")){
$this->noaction();
}
return parent::onDispatch($e);
}
public function noaction()
{
echo 'action does not exits';
}
}
How to get the controller name, action name in Zend Framework 2
Try as below for ZF2
$this->getEvent()->getRouteMatch()->getParam('action', 'index');
$this->getEvent()->getRouteMatch()->getParam('controller', 'index');
ZF2 widgets/views in layout
Here is what i did. This should also be the right way to do it
in module.php
public function getViewHelperConfig()
{
return array(
'factories' => array(
'statusWidget' => function ($sm) {
-- some service handling --
$statusWidget = new statusWidget($service);
return $statusWidget;
}
)
);
}
then i created a viewHelper in operationalStatus\View\Helper
<?php
namespace operationalStatus\View\Helper;
use Zend\View\Helper\AbstractHelper;
class statusWidget extends AbstractHelper
{
public function __construct($service){
$this->service = $service
}
public function __invoke()
{
-- collect data from the service --
return $this->getView()->render('operational-status/widget/status', array('operation' => $status));
}
}
Related Topics
PHP MySQL Greek Letters Showing Like? Marks
Is There Any Sort of "Pre Login" Event or Similar
PHP Get Dropdown Value and Text
PHP and Microsoft Access Database - Connection and Crud
Jquery Mobile: Sending Data from One Page to the Another
In PHP, How to Check If a Function Exists
How to Use String Concatenation to Define a Class Const in PHP
How to Get the Last N Items in a PHP Array as Another Array
PHP Unable to Load PHP_Curl.Dll Extension
How to Get the Value of a Private Property with Reflection
Error: File Is Encrypted or Is Not a Database
Invoke External Shell Script from PHP and Get Its Process Id
%2F in Url Breaks and Does Not Reference to the .PHP File Required
Updating Product Stock Programmatically in Woocommerce 3
How to View/Open a Word Document in My Browser Using with PHP or HTML