i'm trying to configure the application module with dependency injection. But the dependencies seem not to be injected. Here is the part of my config:
'di' => array(
'instance' => array(
'Zend\View\Resolver\AggregateResolver' => array(
'injections' => array(
'Zend\View\Resolver\TemplateMapResolver',
'Zend\View\Resolver\TemplatePathStack',
),
),
'Zend\View\Resolver\TemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'home' => __DIR__ . '/../view/home.phtml',
'site/layout' => __DIR__ . '/../view/site/layout.phtml',
'site/error' => __DIR__ . '/../view/site/error.phtml',
'site/404' => __DIR__ . '/../view/site/404.phtml',
),
),
),
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'application' => __DIR__ . '/../view',
),
),
),
'Zend\View\Renderer\PhpRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\Resolver\AggregateResolver',
),
),
'Zend\Mvc\View\DefaultRenderingStrategy' => array(
'parameters' => array(
'layoutTemplate' => 'site/layout',
),
),
'Zend\View\View' => array(
'parameters' => array(
'renderer'=>'Zend\View\Renderer\PhpRenderer'
)
),
)
),
The Zend\View\Renderer\PhpRenderer object is not injected. How can configure my app to use DI for Views and Resolvers?
Related
I have two controllers: "Home" and "Login" in an "Application" module. In the first controller, layout.phtml is correctly loaded, but in the second controller, layout.phtml isn't loaded. I want that the layout is be loaded in the two controllers. Here is my module.config.php file:
return array(
'router' => array(
'routes' => array(
'default' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Home',
'action' => 'index',
),
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
]
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'pt_BR',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Home' => 'Application\Controller\HomeController',
'Application\Controller\Login' => 'Application\Controller\LoginController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/home/index' => __DIR__ . '/../view/application/home/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml'
),
'template_path_stack' => array(
__DIR__ . '/../view',
__DIR__ . '/../../../templates',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
I have a Service Layer for my A module. Now I want to reuse the Service Layer by calling it from another module B. I understand that I have to inject my Service Layer in Controller of Module B.
My question is how do I give the path of the Service Layer for module B?
In my Controller Factory for module B, $serviceLocator->get('A\Service\AService'), and in my Controller, use A\Service\AService; these won't work as these are directory locations for module A.Also, It seems like default path starts from folder outside controllers. How do I use these path for B?
class IndexController extends AbstractActionController
{
function __construct(AService $sm) {
$this->sm =$sm;
}
public function indexAction()
{
$event = $this->getEvent();
$request = $event->getRequest();
$router = $event->getRouter();
$uri = $router->getRequestUri();
$baseUrl = sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $request->getBaseUrl());
$this->sm->callMethod($baseUrl);
}
}
My module.config.php,
return array(
'controllers' => array(
'factories' => array(
'Application\Controller\IndexController' => 'Application\Controller\IndexControllerFactory',
),
),
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
In module.php
public function getControllerConfig()
{
return array(
'factories' => array(
'Application\Controller\Index' => function ($sm){
$a = $sm->getServiceLocator()->get('A\Service\AService');
return new Application\IndexController($a);
}
),
);
}
It should works the same as using the Service in A controller.
I assume that you register service in module A
...
'invokables' => array(
'A\Service\AService' => 'A\Service\AService',
)
...
From that point you can use it in any module (eg. in your B modul Module.php):
public function getControllerConfig()
{
return array(
'factories' => array(
'B\Controller\Index' => function ($sm){
$a = $sm->getServiceLocator()->get('A\Service\AService');
return new B\IndexController($a);
}
),
);
}
I have added a new usersmanagement controller in ZF2.If i execute the controller(http://localhost/app/usersmanagement/).It shows 404 page not found error.
Can anyone tell me what will be the issue,
Below is my module.config.php configuration ,
<?php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Usersmanagement',
'action' => 'index',
),
),
),
'usersmanagement' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => 'usersmanagement',
'defaults' => array(
'controller' => 'Application\Controller\Usersmanagement',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Usersmanagement' => 'Application\Controller\UsersmanagementController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
Thanks
First, please make sure you have an indexAction method on your Application\Controller\Usersmanagement Controller.
As #Microbe pointed out, you must correct the line 'route' => 'usersmanagement', to 'route' => '/usersmanagement',
There is another problem with your module.config.php.
The home route points to the Application\Controller\Usersmanagement controller. In this case both URLs ('/' and '/usersmanagement') will call the same action Application\Controller\Usersmanagement::indexAction() and therefore get the same result.
I've got a problem with adding many controllers in the one module. Zend2 is difficult for beginners.
I've created Module "Home" with Controllers "Home" and "News". HomeController is going fine but when I'm trying to connect to the NewsController I'm getting Fatal error: Class 'Home\Controller\NewsController' not found in C:\wamp\www\zend2\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 170. And I don't know where the problem is.
My module.config looks like
return array(
'controllers' => array(
'invokables' => array(
'Home\Controller\Home' => 'Home\Controller\HomeController',
'Home\Controller\News' => 'Home\Controller\NewsController',
),
),
'router' => array(
'routes' => array(
'home' => array(
'type' => 'segment',
'options' => array(
'route' => '/home[/][:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Home\Controller\Home',
'action' => 'index',
),
),
),
'news' => array(
'type' => 'segment',
'options' => array(
'route' => '/news[/][:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Home\Controller\News',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
I'm using navigation factory so navigation file looks like :
return array(
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home'
),
array(
'label' => 'News',
'route' => 'news'
),
),
),
'service_manager' => array(
'factories' => array(
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory'
)
)
);
And Module.php looks like
namespace Home;
class Module {
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
And NewsController looks like
namespace News\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class NewsController extends AbstractActionController {
public function indexAction()
{
return array();
}
}
In your controller class you're declaring the namespace as:
namespace News\Controller;
yet in your module config you list the invokable class as Home\Controller\NewsController. The namespaces need to match, so you probably want to change the controller class namespace to:
namespace Home\Controller;
I want to create admin panel using ZfcAdmin module. I want to create routing, to manage users. Here is it:
<?php
return array(
'controllers' => array(
'invokables' => array(
'AdminUser\Controller\AdminUser' => 'AdminUser\Controller\AdminUserController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'admin-user' => __DIR__ . '/../view',
),
),
'router' => array(
'routes' => array(
'zfcadmin' => array(
'may_terminate' => true,
'child_routes' => array(
'user' => array(
'type' => 'segment',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'AdminUser\Controller\AdminUser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' =>array(
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/edit/:user_id',
'defaults' => array(
'controller' => 'AdminUser\Controller\AdminUser',
'action' => 'edit',
),
),
),
),
),
),
),
),
),
);
I request: /admin/user and it's ok, but when I want to receive URL like: /admin/user/edit/1 I always get /admin/user I create the link this way:
<?php $this->url('zfcadmin/user/edit', array(
'action' => 'edit',
'user_id' => $user['user_id'],
)) ?>
why? what's wrong?
The first argument in the ZF2 Url view helper is a $name. Have you tried using 'edit' like you've specified in your config?
I also think you may need to echo the value.
<?php echo $this->url('edit', array('action' => 'edit', 'user_id' => $user['user_id'],)); ?>