3

Is it fine to have more than one namespaces for a custom module? I haven't tried yet. But for some situations it's better if we can have this feature. Any suggestion will be appreciated.

Example:

Mynamespace1
      |
      --------Mynamespace2
                    |
                    --------------Mymodule
                                       |
                                       ---------Block
                                       |
                                       ---------Helper
                                       |
                                       ---------etc
2
  • 2
    Can you add a small example on what you mean exactly? Commented Sep 30, 2013 at 9:35
  • Hi, I edited the question so it can have an example
    – Sukeshini
    Commented Sep 30, 2013 at 10:17

3 Answers 3

6

NOTE: This will not work with controllers

As Marius and Ben pointed out this will not work because of Mage_Core_Controller_Varien_Router_Standard::getControllerFileName. Here when the file path for the controller is being built it will be limited to two folders level.

$parts = explode('_', $realModule);
$realModule = implode('_', array_splice($parts, 0, 2));

Though it appears to be possible for blocks, models and helpers with the following simple code example.

/app/etc/modules/Manners_Double_Test.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Manners_Double_Test>
            <active>true</active>
            <codePool>local</codePool>
        </Manners_Double_Test>
    </modules>
</config>

/app/code/local/Manners/Double/Test/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Manners_Double_Test>
            <version>0.0.1</version>
        </Manners_Double_Test>
    </modules>
    <global>
        <models>
            <manners_double_test>
                <class>Manners_Double_Test_Model</class>
            </manners_double_test>
            <newsletter>
                <rewrite>
                    <subscriber>Manners_Double_Test_Model_Subscriber</subscriber>
                </rewrite>
            </newsletter>
        </models>
        <helpers>
            <manners_double_test>
                <class>Manners_Double_Test_Helper</class>
            </manners_double_test>
        </helpers>
        <blocks>
            <manners_double_test>
                <class>Manners_Double_Test_Block</class>
            </manners_double_test>
        </blocks>
    </global>
</config>

/app/code/local/Manners/Double/Test/Model/Test.php

<?php
class Manners_Double_Test_Model_Test extends Mage_Core_Model_Abstract {
    public function doubleTest() {
        return 'asdasdasd';
    }
}

/app/code/local/Manners/Double/Test/Model/Subscriber.php

<?php
class Manners_Double_Test_Model_Subscriber extends Mage_Newsletter_Model_Subscriber {

    public function sendConfirmationSuccessEmail()
    {
        Mage::Log('My_Stuff_Model_Subscriber::sendConfirmationSuccessEmail()');
    }
}

I then added a call to this function in the index.php and it returned the string. I also called the newsletter/subscriber and the class was my new rewritten one. I am interested to see and would suspect that there will be issues with this approach with more complex code.

/app/code/local/Manners/Double/Test/Block/Template.php

<?php
class Manners_Double_Test_Block_Template extends Mage_Core_Block_Template {
    public function sayHello() {
        echo "Hello!";
    }
}

/app/code/local/Manners/Double/Test/Helper/Data.php

<?php
class Manners_Double_Test_Helper_Data extends Mage_Core_Helper_Abstract {
}

I have successfully tested this with:

  1. Blocks
  2. Models
  3. Helpers
  4. Observers
10
  • Wow.. it's working. I checked your codes. It may become complex while going through a huge development process. Is it fine if I use more than one namespace for a module?
    – Sukeshini
    Commented Sep 30, 2013 at 11:10
  • Technically it seems to be fine, I am interested in why you would want more than one namespace? Not that it is wrong I am just interested :) Commented Sep 30, 2013 at 11:11
  • The scenario is like this. In the project currently I have developed most of the modules are under a one namespace. So some modules contains small changes to core files. In the existing procedure the name I expect to put them are already allocated for another module which is doing a huge job. So in the case I want to put all of the modules under this namespace and two or modules must need the same name, in that case if we can use more than one namespace for a module this will solve the problem. Thanks in advance for your interest.
    – Sukeshini
    Commented Sep 30, 2013 at 11:20
  • 2
    Action controllers will not work at this depth
    – benmarks
    Commented Sep 30, 2013 at 12:04
  • 1
    These are the lines that prevents the controllers from working in this case. In method Mage_Core_Controller_Varien_Router_Standard::getControllerFileName, $parts = explode('_', $realModule); $realModule = implode('_', array_splice($parts, 0, 2));
    – Marius
    Commented Sep 30, 2013 at 12:25
3

I believe that everything except action controllers will work. Action controllers are hardcoded to exist in a controllers folder one or two subfolders under a codepool.

(Edit, now that I'm not on my phone):

See Mage_Core_Controller_Varien_Router_Standard::getControllerFileName():

public function getControllerFileName($realModule, $controller)
{
    $parts = explode('_', $realModule);
    $realModule = implode('_', array_splice($parts, 0, 2));
    $file = Mage::getModuleDir('controllers', $realModule);
    if (count($parts)) {
        $file .= DS . implode(DS, $parts);
    }
    $file .= DS.uc_words($controller, DS).'Controller.php';
    return $file;
}

The limiting factor here is implode('_', array_splice($parts, 0, 2)); which forces the controllers directory to be in one or two (but not more) subfolders. You could modify this behavior by changing the class definition in the local or community codepools.

0

Here's how it works for controllers. The following is my controller class.

class Manners_Double_Test_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "wwwwwooooooooooooooooooow";
    }
}

And here's the test.php ( the out side file which calls the controller action. )

if(!class_exists('Manners_Double_Test_IndexController')) 
{
    require_once('Manners/Double/Test/controllers/IndexController.php');        
}

$controller         = new Manners_Double_Test_IndexController(
                            Mage::app()->getRequest(),
                            Mage::app()->getResponse()
                        );        

$request            = $controller->getRequest();
$response           = $controller->getResponse();

$controller->indexAction();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.