3

hi i am new in zend framework2.2.0. i want to create one module with multiple controller i have download "Album" module from github and its working fine Now i want to add the more controller in it the below i have shown my folder structure for file in module

module/
    Album/
         config/
             module.config.php
         src/
            Album/
                Controller/
                         AlbumController.php
                         UserController.php
               Form/
                     AlbumForm.php
                     UserForm.php
               Model/
                   AlbumTable.php 
                   Album.php
                   UserTable.php 
                   User.php

        view/
            album/ 
                 album/             
                       index.phtml
                 user/             
                       index.phtml

i have also changed all the name space in file

namespace Album\Controller;

class UserController extends \Zend\Mvc\Controller\AbstractActionController

and some indexAction method witch returns a new \Zend\View\Model\ViewModel();

then you can create your viewfile in

Album/view/Album/user/index.phtml i did above changes. is there any chage needed in "Album/Module.php" file ? can you tell me through which link i can see users list ?

i and tired with this error help me to get out of it

enter image description here

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
deck john
  • 637
  • 1
  • 9
  • 22

1 Answers1

9

You may need to add url rules for user in module.config.php in Album/config folder. Then you can access the url like

// The following section is new and should be added to your file
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
        'user' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/user[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\User',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

Also

'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController',
        'Album\Controller\User' => 'Album\Controller\UserController',
    ),
),
kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
  • i did the same thing in same file but still get the same error message – deck john Jun 05 '13 at 06:28
  • i am using "project/public/user". Now i am getting this error "Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Album\Model\UserTable" – deck john Jun 05 '13 at 06:30
  • You may need to add similar code for user in getServiceConfig method of Module.php like existing albumtable code – kuldeep.kamboj Jun 05 '13 at 06:36
  • finally i did it. we have to assign the database table to user. so need to chage the "Album/Module.php" file. i got the list of all the user – deck john Jun 05 '13 at 06:37
  • can you tell me now how i access the album function in user controller ? – deck john Jun 05 '13 at 06:46
  • By using $this->getServiceLocator()->get('Album\Model\UserTable')->someUserMethod(); – kuldeep.kamboj Jun 05 '13 at 06:51
  • i write this in my album controller "$this->getServiceLocator()->get('Album\Model\UserTable')->indexAction();" and i get this error message "Call to undefined method Message\Model\UserTable::indexAction() " i have also included "use Message\Model\user;" in album controller in top – deck john Jun 05 '13 at 06:58
  • Well you can't call methods of another controller in this way. It is only way to call method from models. like $this->getServiceLocator()->get('Album\Model\UserTable')->getUserList(); OR $this->getServiceLocator()->get('Album\Model\UserTable')->addUser();. – kuldeep.kamboj Jun 05 '13 at 07:14
  • hey i am getting the error with the $this->url. i cant pass more than one parameter in the link. right now i am passing ' but when i hover it show only id not aa' – deck john Jun 05 '13 at 15:04
  • @deckjohn One suggestion please post this problem as seperate question as more people can help you in this regard. – kuldeep.kamboj Jun 06 '13 at 05:54
  • if anyone can help me on my another question http://stackoverflow.com/questions/16954383/a-link-pass-multiple-parameter-in-zend-framework – deck john Jun 06 '13 at 05:57