How do I place a file in the core folder into the modules folder? Same for .tpl files

Hi, I have created a new database table and a class file that extends oxI18n. And it works great, when I have it in the core file folder.
But as Im creating a module I want to place it in the modules folder. But when I do, it doesnt find the file anymore.
Iam not declaring my extension of oxI18n in the “Installed Shop Modules” where files such as oxPaymentGateway is extended. And I dont think I should either.

[U]So how can I move the file that works as wanted in the core folder to my modules folder?[/U]

And how do I do the same for the .tpl files? Currently I have them placed in the out/basic/tpl folder.

I suspect its possible to do this, because there wouldnt be much point in having the modules folder otherwise, would it?

Hi,
4.3+ oxid provided spl_autoloader, so you can write yours own autoloader in modules/functions.php
But be aware that you will not get ajax assignment windows in this case (as strange oxid implementation)

moving templates and languages files to modules dir is not recomended, as it needs aditional module to control from were to get all templates and languages files

[QUOTE=wanis;30256]Hi,
4.3+ oxid provided spl_autoloader, so you can write yours own autoloader in modules/functions.php
But be aware that you will not get ajax assignment windows in this case (as strange oxid implementation)

moving templates and languages files to modules dir is not recomended, as it needs aditional module to control from were to get all templates and languages files[/QUOTE]
Why is it not recommended? It sounds like you got a good reason for it, so I will probably leave it bee then. But Im still curious.

However, Iam still wondering about the core file I made, I suppose the same goes for that file? Should I use the spl_autoloader for this? But not for the .tpl files?

Btw, Thanx for your reply :slight_smile:

[QUOTE=betatest;30245]And how do I do the same for the .tpl files? Currently I have them placed in the out/basic/tpl folder.[/QUOTE]
Search the dok for “template override”.

[QUOTE=betatest;30291] Should I use the spl_autoloader for this? But not for the .tpl files?[/QUOTE]
The spl_autoload is only for classes not for templates.

However, you can give realtive path to smarty outside of the template-folder.
In the render method of your View use Instead
’return “myTemplateName.tpl”; '
somthing like this
’return “…/…/…/modules/myModuleFolder/out/myTemplateName.tpl”; '.
So smarty use /modules/myModuleFolder/out/myTemplateName.tpl

If you do it, you can think about:

  • try if the template you wanna exists in ‘template_overwrite’ folder.
  • if not, try if template exists in the normal template folder.
  • if not, use the template in your module folder.

Yes, it will be nice, if you can write modules like in normal frameworks. With one Folder and a simular subfolder-structure like the app.


<?php
function yoursmoduleAutoload( $sClass )
{
    $sClass = basename( $sClass );
    $sClass = strtolower($sClass);

    static $sBasePath  = null;
    static $aClassDirs = null;


    static $aClassPaths;

    if (!$aClassPaths) {
        $aClassPaths = oxUtils::getInstance()->fromPhpFileCache("class_file_paths");
    }

    if (isset($aClassPaths[$sClass])) {
        include $aClassPaths[$sClass];
        return;
    }

    // initializing paths
    if ( $aClassDirs == null ) {
        $sBasePath = getShopBasePath();
        $aClassDirs = array( $sBasePath . 'modules/yourmodule/core/',
                             $sBasePath . 'modules/yourmodule/views/',
                             $sBasePath . 'modules/yourmodule/admin/',
                             $sBasePath . 'modules/yourmodule/',
                            );
    }

    foreach ( $aClassDirs as $sDir ) {
        $sFilename = $sDir . strtolower( $sClass ) . '.php';
        if ( file_exists( $sFilename ) ) {
            if (!isset($aClassPaths[$sClass])) {
                $aClassPaths[$sClass] = $sFilename;
                oxUtils::getInstance()->toPhpFileCache("class_file_paths", $aClassPaths);
            }
            include $sFilename;
            return;
        }
    }
}

spl_autoload_register("yoursmoduleAutoload");
?>

add this to yours /modules/functions.php and all classes will be loaded.

templates and language files i wont recomend to seperate, as it will be dificult to maitain the project style. i mean jumping in dirs /out/basic/tpl… and modules/yoursmodule/out/basic/tpl… it is nice at first thought, but if you will work like this more then once, you will see that it is not functional as thought before
(BTW languages have feature like making yours language file yoursmodule_lang.php and putting it in language dirs)

[QUOTE=avenger;30302]Search the dok for “template override”.[/QUOTE]
Thank you, I found your thread, but I have not yet decided Im goíng to do that. As it doesnt seem to be recommended to have .tpl files in the modules folder. And messing around in a file that cannot even be extended doesnt feel like a solution I can use.

[QUOTE=MBa;30307]The spl_autoload is only for classes not for templates.[/QUOTE]
Ok thank you to :slight_smile:

[QUOTE=MBa;30307]
However, you can give realtive path to smarty outside of the template-folder.
In the render method of your View use Instead
’return “myTemplateName.tpl”; '
somthing like this
’return “…/…/…/modules/myModuleFolder/out/myTemplateName.tpl”; '.
So smarty use /modules/myModuleFolder/out/myTemplateName.tpl

If you do it, you can think about:

  • try if the template you wanna exists in ‘template_overwrite’ folder.
  • if not, try if template exists in the normal template folder.
  • if not, use the template in your module folder.

Yes, it will be nice, if you can write modules like in normal frameworks. With one Folder and a simular subfolder-structure like the app.[/QUOTE]
I have seen this version of solutions on another module before, and it looks really messy and not really something I want to make use of. And also if Im not misstaken Im gonna have to move upwards in the folder structure which is a problem as you can prohibit that in the php.ini file which I recently discovered :frowning:
Thanx for the idea though.

[QUOTE=wanis;30309]


<?php
function yoursmoduleAutoload( $sClass )
{
    $sClass = basename( $sClass );
    $sClass = strtolower($sClass);

    static $sBasePath  = null;
    static $aClassDirs = null;


    static $aClassPaths;

    if (!$aClassPaths) {
        $aClassPaths = oxUtils::getInstance()->fromPhpFileCache("class_file_paths");
    }

    if (isset($aClassPaths[$sClass])) {
        include $aClassPaths[$sClass];
        return;
    }

    // initializing paths
    if ( $aClassDirs == null ) {
        $sBasePath = getShopBasePath();
        $aClassDirs = array( $sBasePath . 'modules/yourmodule/core/',
                             $sBasePath . 'modules/yourmodule/views/',
                             $sBasePath . 'modules/yourmodule/admin/',
                             $sBasePath . 'modules/yourmodule/',
                            );
    }

    foreach ( $aClassDirs as $sDir ) {
        $sFilename = $sDir . strtolower( $sClass ) . '.php';
        if ( file_exists( $sFilename ) ) {
            if (!isset($aClassPaths[$sClass])) {
                $aClassPaths[$sClass] = $sFilename;
                oxUtils::getInstance()->toPhpFileCache("class_file_paths", $aClassPaths);
            }
            include $sFilename;
            return;
        }
    }
}

spl_autoload_register("yoursmoduleAutoload");
?>

add this to yours /modules/functions.php and all classes will be loaded.

templates and language files i wont recomend to seperate, as it will be dificult to maitain the project style. i mean jumping in dirs /out/basic/tpl… and modules/yoursmodule/out/basic/tpl… it is nice at first thought, but if you will work like this more then once, you will see that it is not functional as thought before[/QUOTE]
Wow. Thanx for the code example :slight_smile: I hope I can make good use of this code. It looks like your using spl_autoloader as well. So Im gonna have to find out how that works before I can use your code though/if I can make use of your code :stuck_out_tongue:

[QUOTE=wanis;30309]
(BTW languages have feature like making yours language file yoursmodule_lang.php and putting it in language dirs)[/QUOTE]
Many thanx for that. I had forgotten about getting the language items into a separate file, which I now immediatley implemented :slight_smile: