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);
}
),
);
}
Related
I made login/registration module. I also changed indexAction in LoginController to loginAction but still Logout doesn't work. I try my best but I don't know what the problem is.
My code is given below:
LoginController.php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Users\Form\LoginForm;
use Users\Form\LoginFilter;
use Users\Model\User;
use Users\Model\UserTable;
class LoginController extends AbstractActionController
{
protected $storage;
protected $authservice;
public function getAuthService()
{
if (! $this->authservice) {
$this->authservice = $this->getServiceLocator()->get('AuthService');
}
return $this->authservice;
}
public function indexAction()
{
$this->layout("layout/layout_users");
if ($this->getAuthService()->hasIdentity())
{
return $this->redirect()->toRoute('admin',
array('action'=>'index'));
}
$form = $this->getServiceLocator()->get('LoginForm');
$viewModel = new ViewModel(array('form' => $form));
return $viewModel;
}
public function processAction()
{
if (!$this->request->isPost()) {
return $this->redirect()->toRoute(NULL , array(
'controller' => 'login',
'action' => 'index'
));
}
$post = $this->request->getPost();
$form = new LoginForm();
$inputFilter = new LoginFilter();
$form->setInputFilter($inputFilter);
$form->setData($post);
if (!$form->isValid()) {
$model = new ViewModel(array(
'error' => true,
'form' => $form,
));
$model->setTemplate('users/login/index');
return $model;
} else {
//check authentication...
$this->getAuthService()->getAdapter()
->setIdentity($this->request->getPost('email'))
->setCredential($this->request->getPost('password'));
$result = $this->getAuthService()->authenticate();
if ($result->isValid()) {
$this->getAuthService()->getStorage()->write($this->request->getPost('email'));
return $this->redirect()->toRoute('admin' , array( ));
} else {
$model = new ViewModel(array(
'error' => true,
'form' => $form,
));
$model->setTemplate('users/login/index');
return $model;
}
}
}
public function logoutAction()
{
if ($this->getAuthService()->hasIdentity()) {
$this->getAuthService()->clearIdentity();
}
return $this->redirect()->toRoute('users/login',
array('action'=>'index'));
}
}
module.config.php for Users module
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Register' => 'Users\Controller\RegisterController',
'Users\Controller\Login' => 'Users\Controller\LoginController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
'route' => '/users',
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'login' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/login[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
),
'register' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/register[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Register',
'action' => 'index',
),
),
),
),
),
),
),
'view_manager' => array(
'template_map' => array(
'layout/layout_users' => __DIR__ . '/../view/layout/layout.phtml',
),
'template_path_stack' => array(
'users' => __DIR__ . '/../view',
),
),
);
module.config.php for Admin module:
<?php
namespace Admin;
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Admin\Controller\Index',
'action' => 'index',
),
),
),
'profile' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/profile[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Admin\Controller\Profile',
'action' => 'index',
),
),
),
'logout' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/users/login',
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'logout',
),
),
),
'admin' => array(
'type' => 'Literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'Admin\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',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
// 'Admin\Controller\Index' => Controller\IndexController::class
'Admin\Controller\Index' => 'Admin\Controller\IndexController',
'Admin\Controller\Profile' => 'Admin\Controller\ProfileController',
'Admin\Controller\Provinces' => 'Admin\Controller\ProvincesController',
'Admin\Controller\Districts' => 'Admin\Controller\DistrictsController',
'Admin\Controller\Cities' => 'Admin\Controller\CitiesController',
'Admin\Controller\Stations' => 'Admin\Controller\StationsController',
'Admin\Controller\Services' => 'Admin\Controller\ServicesController',
'Admin\Controller\Vehicles' => 'Admin\Controller\VehiclesController',
),
),
'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_admin' => __DIR__ . '/../view/layout/layout.phtml',
'admin/index/index' => __DIR__ . '/../view/admin/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(
),
),
),
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'Profile',
'route' => 'profile',
),
array(
'label' => 'Logout',
'route' => 'logout',
),
),
),
);
Directory for view in User module is:
view/users/login/index.phtml
You have mistakes in a route and a controller.
In controller
public function logoutAction()
{
if ($this->getAuthService()->hasIdentity()) {
$this->getAuthService()->clearIdentity();
}
return $this->redirect()->toRoute('users/login', //Change to 'home'
array('action'=>'index'));
}
In module.config
'logout' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/users/login', //Change to '/users/logout'
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'logout',
),
),
),
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 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;
<?php
return array(
'controllers' => array(
'invokables' => array(
'Member\Controller\Member' => 'Member\Controller\MemberController',
),
),
'router' => array(
'routes' => array(
'member' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'index',
),
),
),
'member' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action][/:pkMemberId][/:status]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'pkMemberId' => '[0-9]+',
'status' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'index',
),
),
),
'changeMemberPassword' => array(
'type' => 'segment',
'options' => array(
'route' => '/changeMemberPassword',
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'changePassword',
),
),
),
),
),
'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',
),
),
),
);
?>
i have a route like this http://www.abc.com/member-profile/756 but i want to show it like this http://www.abc.com/john. showing user first name in the url. Thanks
If you want to use firstname in url by hiding module and action from url, you can do this by add routing as below:
'member' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:firstname][/:pkMemberId][/:status]',
'constraints' => array(
'firstname' => '[a-zA-Z][a-zA-Z0-9_-]*',
'pkMemberId' => '[0-9]+',
'status' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Member\Controller\Member',
'action' => 'view',
),
),
),
You need to call this by adding this line:
harish
you need to use a unique/distinct first name for every member to identify the user or provide an id in parameter to identify the member.