+ Reply to Thread
Results 1 to 5 of 5

Thread: Boostraping modules, models and all the fun

  1. #1
    BornForCode is offline Junior Member
    Join Date
    May 2009
    Posts
    13

    Question [Solved] Boostraping modules, models and all the fun

    Cheers,

    I banging my head against different objects for two days and i'm still far from getting the illumination (i am padawan level 1 in Zend Framework) especially in the bootstraping an application and its modules area.

    I would like to create an application withe the following structure:

    - application
    - configs [general app configs]
    - modules
    \-default
    \- controllers
    \- models
    \- views
    \-admin
    \- controllers
    \- models
    \- views

    Everything runs smoothly till i get to models, there is no way to call from a controller a model located in the same module without adding some code in the main bootstrap application file, though everything else runs perfectly (controllers and views).

    I made all kind of naughty tries from
    [PHP]
    $moduleLoader = new Zend_Application_Module_Autoloader(array(
    'namespace' => '',
    'basePath' => APPLICATION_PATH));
    return $moduleLoader;
    [/PHP]
    I even add namespaces (default) etc.

    Please show some mercy and give a bit of help with some code.
    Last edited by BornForCode; 06-29-2009 at 10:32 PM.

  2. #2
    Tekerson is offline Senior Member
    Join Date
    Jul 2008
    Posts
    288

    Default

    A few things.
    Firstly, the "default" module doesn't belong under the modules directory. The default module is the application itself and lives directly under application/

    Add
    Code:
    resources.modules = ""
    resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
    to appliction.ini to initialize the module resource plugin and set the modules directory.

    Create modules/admin/Bootstrap.php and fill it with
    [php]
    <?php
    class Auth_Bootstrap extends Zend_Application_Module_Bootstrap
    {
    }
    [/php]
    to bootstrap the module, including setting up resource autoloaders for models, forms, services etc.

    Add this function
    [php]
    protected function _initAutoload()
    {
    new Zend_Application_Module_Autoloader(array(
    'basePath' => APPLICATION_PATH,
    'namespace' => 'Default',
    ));
    }
    [/php]
    to your application/Bootstrap.php to initialize the resource autoloaders for the default module. "Default" can be changed to whatever you are prefixing your resources (models, forms, services etc) of your default module with - often the application name.

    I've also blogged about turning the Guestbook application from the Quickstart guide into a module, there might be some more information there: Brenton Alker’s Deprecated Behaviour Building a Modular Application in Zend Framework – Part 2
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  3. #3
    BornForCode is offline Junior Member
    Join Date
    May 2009
    Posts
    13

    Default

    Hm, but why shouldn't reside the 'default' in the module area? To some point it has the same structure like any other module (controllers, models and views).

    If you use a modular approach the zf tool creates all modules inside the modules folder but keeps the models folder outside. This behavior is hard to understand for me. I understand that in the models folder may reside all kind of tables that can be used general all over the application, but shouldn't this be made available from the modules/default/models to maintain the application architecture.

    Thank you for your comment, i succeeded to add the default module in the format i want just by adding this code in the application bootstrap file:

    [PHP]
    protected function _initAutoLoad()
    {

    $autoloader = new Zend_Application_Module_Autoloader(
    array(
    'namespace' => 'Default',
    'basePath' => APPLICATION_PATH . '/modules/default'),
    array(
    'namespace' => 'Admin',
    'basePath' => APPLICATION_PATH . '/modules/admin')
    );
    return $autoloader;
    }
    [/PHP]

    I am not very proud with this code but for the moment it seems that this is the only one way to solve this.

  4. #4
    alokin is offline Senior Member
    Join Date
    Apr 2009
    Posts
    192

    Default

    Default module resources (controllers, models, views, etc.) should be placed outside modules directory, like Tekerson said. Idea is to make that default module and its resources some kind of global scope...

    You shouldn't instantiate Zend_Application_Module_Autoloader for every module. Each module, in modules directory should have its own boostrap (Boostrap.php), which extends Zend_Application_Module_Bootstrap. In that case, if you put this line in your config file: resources.modules = "", an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.

    But if you still want to hold your default module in modules directory, its Boostrap class should extend Zend_Application_Module_Bootstrap, and then you have to tell front controller what module is default.

  5. #5
    aneevs is offline Junior Member
    Join Date
    Jun 2008
    Posts
    4

    Default

    Hi,

    I tried using the above said method, but its not loading my models.

    [PHP]
    protected function _initAutoLoad()
    {

    $moduleLoader = new Zend_Application_Module_Autoloader(
    array('namespace'=>'Backend','basePath'=>APPLICATI ON_PATH . '/modules/backend'),
    array('namespace' => 'Frontend','basePath' => APPLICATION_PATH . '/modules/frontend'));

    return $moduleLoader;
    }
    [/PHP]

    What I am trying is I have a class Frontend_Model_Language() in modules/frontend/models/Language.php. This class is not getting loaded.

    Any idea why is it so?

    Thanks for your time!

    regards,
    Aneesh

    Blog

+ Reply to Thread

Similar Threads

  1. Modules, models and autoloading...
    By ajlisowski in forum General Q&A on Zend Framework
    Replies: 3
    Last Post: 07-11-2010, 09:17 AM
  2. Multiple modules using the same models folder
    By wojto in forum Model-View-Controller (MVC)
    Replies: 2
    Last Post: 03-09-2010, 10:16 PM
  3. Loading Models from Modules on ver 1.10
    By mysticav in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 03-09-2010, 10:44 AM
  4. autoloading default modules models
    By saif in forum General Q&A on Zend Framework
    Replies: 0
    Last Post: 03-02-2010, 11:04 AM
  5. zend modules models
    By cc96ai in forum Model-View-Controller (MVC)
    Replies: 3
    Last Post: 12-19-2009, 03:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts