How to load shared functions?
I have several custom functions that I need on both the store side and admin side. To date, I've been duplicating the functions on both sides creating problems with maintenance and taking up extra memory. So I put all the shared functions in a shared functions file on the store side and tried to auto-load the file in the admin. The code I used is:
Code:
if (!defined ('IS_ADMIN_FLAG')) {
die ('Illegal Access');
}
$autoLoadConfig[0][] = array(
'autoType' => 'require',
'loadFile' => DIR_WS_FUNCTIONS . 'extra_functions/shared_functions.php',
);
which is placed in a file named config.shared_functions.php located in admin/includes/auto_loaders/.
But it doesn't work. I still get call to undefined function fatal error. The undefined function is in the shared_functions.php file located in includes/functions/extra_functions/. There are no other errors. I followed the instructions in the docs.
What am I doing wrong?
Dave
Re: How to load shared functions?
Quote:
Originally Posted by
Dave224
I have several custom functions that I need on both the store side and admin side. To date, I've been duplicating the functions on both sides creating problems with maintenance and taking up extra memory. So I put all the shared functions in a shared functions file on the store side and tried to auto-load the file in the admin. The code I used is:
Code:
if (!defined ('IS_ADMIN_FLAG')) {
die ('Illegal Access');
}
$autoLoadConfig[0][] = array(
'autoType' => 'require',
'loadFile' => DIR_WS_FUNCTIONS . 'extra_functions/shared_functions.php',
);
which is placed in a file named config.shared_functions.php located in admin/includes/auto_loaders/.
But it doesn't work. I still get call to undefined function fatal error. The undefined function is in the shared_functions.php file located in includes/functions/extra_functions/. There are no other errors. I followed the instructions in the docs.
What am I doing wrong?
Dave
If that's the admin auto-loader, you need to use
Code:
$autoLoadConfig[0][] = array(
'autoType' => 'require',
'loadFile' => DIR_FS_CATALOG . DIR_WS_FUNCTIONS . 'extra_functions/shared_functions.php',
);
Re: How to load shared functions?
Thank you, thank you, thank you! It works!:clap:
Re: How to load shared functions?
lat9,
Can the same technique be used for common store and admin defines in zc 1.5.7, and in zc 1.5.8 and beyond with the new lang. approach?
Dave
Re: How to load shared functions?
Quote:
Originally Posted by
Dave224
lat9,
Can the same technique be used for common store and admin defines in zc 1.5.7, and in zc 1.5.8 and beyond with the new lang. approach?
Dave
Yep. In the admin, the DIR_WS_* definitions refer to the like-named directories in the admin. You need to prefix those definitions with DIR_FS_CATALOG to pull in the storefront/common files.
Just remember for language files, that the file (with or without the lang. prefix) could be present in a template-specific sub-directory.
Re: How to load shared functions?
Since a file in the admin .../extra_functions is loaded automatically, I have a file in there that just includes the store function file....no need to edit the auto loader.
Or am I missing something?
Re: How to load shared functions?
Quote:
Originally Posted by
torvista
Since a file in the admin .../extra_functions is loaded automatically, I have a file in there that just includes the store function file....no need to edit the auto loader.
Or am I missing something?
It's an alternate approach if there are functions available only to the admin in addition to those 'shared' ones. If a site is just using a set of shared functions, then it's just as easy to create an entry in an auto_loader instead of an admin /extra_functions file.