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 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 am trying to get my head around Module routing in ZF2.
At the moment I am only able to create a single controller for a single action and am struggling to figure this routing out. I have looked at other modules and plugins and I kind of get it, just need a small push to "get it".
In this example I am trying to route to the two actions: indexAction and cmstoolsAction
Essentially a user navigates to:
/affiliates/overview
/affiliates/cmstools
And the error is:
The requested URL could not be matched by routing.
I think where I am struggling is firstly comprehending how MVC works and getting lost in the detail. There is so much information in the manual that it becomes a little overwhelming.
anyway - would greatly appreciate any input!
Image of the Module structure:
My controller looks like this:
<?php
namespace Affiliates\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class AffiliatesController extends AbstractActionController
{
//Overview page
public function IndexAction()
{
}
public function CmstoolsAction()
{
}
}
And my module config looks like this:
<?php
return array(
'view_manager' => array(
'template_path_stack' => array(
'affiliates' => __DIR__ . '/../view',
),
),
'controllers' => array(
'invokables' => array(
'Affiliates\Controller\Affiliates' =>
'Affiliates\Controller\AffiliatesController'
),
),
'router' => array(
'routes' => array(
'affiliates' => array(
'type' => 'Literal',
'options' => array(
'route' => '/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
),
),
);
The routing config is the only important part here. At the moment, you have a route for /overview, which has a child route for /cmstool. This would match the following URLs:
/overview
/overview/cmstool
Not quite what you were after.
There are a few different ways you could configure this. The one closest to what you currently have would be a route for /affiliates, with two child routes, one for each of your actions. The configuration for this would be:
'router' => array(
'routes' => array(
'affiliates' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
'child_routes' => array(
'overview' => array(
'type' => 'Literal',
'options' => array(
'route' => '/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
),
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
),
),
This configuration contains three routes: affiliates, overview and cmstools. The latter two are both child routes of affiliates. Note that I removed the line 'may_terminate' => true, from the affiliates route. This determines whether or not the affiliates route would match on its own (i.e. whether the URL /affiliates would work). Since you didn't list this I'm assuming you don't want it to.
Another way you could configure this would be to simply create two literal routes, once for each URL (not using child routes at all):
'router' => array(
'routes' => array(
'overview' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
),
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
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 been searching and trying for a while now and I can't find out what I'm doing wrong...
I've been reading quite some documentation now and have tried a lot of different approaches.
I hope one of you guys can help me out..
My idea:
I have a module called Application. In this module there is an \Application\Controller\IndexController which is used to show the homepage of my application.
The IndexController needs an object with \Zend\http\Client, so I though I should use the factories key in the configuration that is returned by \Application\Module->getServiceConfig() and add a constructor with required param \Zend\Http\Client $client.
Unfortunatly this is not working..
My code:
<?php
// module/Application/Module.php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'IndexController' => function($serviceManager) {
$httpClient = \Zend\Http\Client;
return new IndexController($httpClient);
},
),
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
<?php
// module/Application/config/module.config.php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
)
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'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/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
<?php
// module/Application/src/Application/Controller/IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
protected $httpClient;
public function __construct(\Zend\Http\Client $client)
{
$this->httpClient = $client;
}
public function getHttpClient()
{
return $this->httpClient;
}
public function indexAction()
{
$httpClient = $this->getHttpClient();
return new ViewModel();
}
}
The error:
Catchable fatal error: Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Zend\Http\Client, none given, called in /var/www/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined in /var/www/module/Application/src/Application/Controller/IndexController.php on line 13
You need to implement the getControllerConfig method in Module.php to do what you're asking, controllers have their own manager, remove what you have from getServiceConfig, and use the following instead
public function getControllerConfig()
{
return array(
'factories' => array(
'Application\Controller\Index' => function($serviceManager) {
$httpClient = new \Zend\Http\Client;
return new \Application\Controller\IndexController($httpClient);
},
),
);
}
You'll also need to remove the line from controller invokables in module.config.php since it'll conflict with your factory
'controllers' => array(
'invokables' => array(
// this line's no longer needed
//'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),