ZF2 best practice for loading modules dynamically - zend-framework

I'm trying to create a service, that helps the user to loading modules dynamically from system administration panel. That is my code:
if(!$this->isModuleInstalled($moduleName)) {
$appConfigService = $this->getServiceManager()->get('ApplicationConfig');
$appConfig = new Config($appConfigService, true);
$modules = $config->modules->toArray();
end($modules);
$nextModuleKey = (key($modules) + 1);
unset($modules);
$config->modules->{$nextModuleKey} = $moduleName;
$writter = new Writer\PhpArray();
$writter->toFile(
__DIR__ . '/../../../../../config/application.config.php',
$config->toArray()
);
}
I think that is not the best practice for loading modules dynamically.

Try this
$configuration = $serviceManager->get('ApplicationConfig');
$configuration['modules'][] = 'ModuleName';
$serviceManager->setService('ApplicationConfig', $configuration);
$serviceManager->get('ModuleManager')->loadModules();

Related

Redirection from component to view in joomla 2.5.8

I am new to joomla. am using joomla 2.5.8 version. i have created one component. i can post form data from view to task in component. but i couldn't redirect from task to view. this is my simple function in controller. This URL is saved in Redirect manager.
function sample(){
$mainframes = JFactory::getApplication();
$link= JRoute::_('index.php?option=com_new',FALSE);
$msg = JText::_('welcome');
$mainframes->redirect($link,$msg);
}
This works for me in my component's custom task, release():
# Get iop_id from query string
$app = JFactory::getApplication();
$iop_id = $app->input->get('id');
$url = JRoute::_('index.php?option=com_iop&task=plan.edit&id=' . (int)$iop_id, false);
$app->redirect($url);
$id = JRequest::getVar('id');
$app = JFactory::getApplication();
$url = JRoute::_('index.php?option=com_iop&task=plan.edit&id=' . (int)$id);
$app->redirect($url);

How to get typoscript setup in a scheduler/cron script?

I need to get the extension typoscript setup in schedular script.
I am using typo3 v 4.5.
My schedular script looks like this.
class tx_myext_scheduler extends tx_scheduler_Task {
public function execute() {
//here i need to get typoscript setup
}
}
and my extension setup looks like this.
plugin.tx_myext_pi1{
listView{
file.height = 216c
}
}
In schedualr script I need to get the file.height value.
How to do that ?
Currently i tried this without success
$pObj = $GLOBALS['TSFE'];
$conf = $pObj->tmpl->setup['plugin.']['tx_myext_pi1.'];
Thank you.
The TSFE is only available in the frontend, so have to initialize it yourself (that consumes some resources!). You can create it like that in scheduler: (source)
$GLOBALS['TT'] = new t3lib_timeTrackNull;
$GLOBALS['TSFE'] = t3lib_div::makeInstance('tslib_fe', $GLOBALS['TYPO3_CONF_VARS'], 2, 0);
$GLOBALS['TSFE']->sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
$GLOBALS['TSFE']->sys_page->init(TRUE);
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->rootLine = '';
$GLOBALS['TSFE']->sys_page->getRootLine(1, '');
$GLOBALS['TSFE']->getConfigArray();
or in an eID script: (source)
require_once(PATH_tslib.'class.tslib_fe.php');
require_once(PATH_t3lib.'class.t3lib_page.php');
$temp_TSFEclassName = t3lib_div::makeInstanceClassName('tslib_fe');
$GLOBALS['TSFE'] = new $temp_TSFEclassName($TYPO3_CONF_VARS, $pid, 0, true);
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->getCompressedTCarray();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
or in a backend module: (source)
function loadTypoScriptForBEModule($extKey) {
require_once(PATH_t3lib . 'class.t3lib_page.php');
require_once(PATH_t3lib . 'class.t3lib_tstemplate.php');
require_once(PATH_t3lib . 'class.t3lib_tsparser_ext.php');
list($page) = t3lib_BEfunc::getRecordsByField('pages', 'pid', 0);
$pageUid = intval($page['uid']);
$sysPageObj = t3lib_div::makeInstance('t3lib_pageSelect');
$rootLine = $sysPageObj->getRootLine($pageUid);
$TSObj = t3lib_div::makeInstance('t3lib_tsparser_ext');
$TSObj->tt_track = 0;
$TSObj->init();
$TSObj->runThroughTemplates($rootLine);
$TSObj->generateConfig();
return $TSObj->setup['plugin.'][$extKey . '.'];
}
If you have missing class errors somewhere, maybe you have to add some requires.
This solution is perfect if the page is in standard mode, but doesn't work if the page is a Draft:
function loadTypoScriptForBEModule($extKey) {
require_once(PATH_t3lib . 'class.t3lib_page.php');
require_once(PATH_t3lib . 'class.t3lib_tstemplate.php');
require_once(PATH_t3lib . 'class.t3lib_tsparser_ext.php');
list($page) = t3lib_BEfunc::getRecordsByField('pages', 'pid', 0);
$pageUid = intval($page['uid']);
$sysPageObj = t3lib_div::makeInstance('t3lib_pageSelect');
$rootLine = $sysPageObj->getRootLine($pageUid);
$TSObj = t3lib_div::makeInstance('t3lib_tsparser_ext');
$TSObj->tt_track = 0;
$TSObj->init();
$TSObj->runThroughTemplates($rootLine);
$TSObj->generateConfig();
return $TSObj->setup['plugin.'][$extKey . '.'];
}

Using Zend Framework 1 Service Window Azure Standalone

I am using zend service for window azure wrapper class standaline in my custom application. I would like to know how do i connect to my window azure storage. there seems to be no way i can specify the connection details (storage key etc)
$storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
$result = $storageClient->createContainer('testcontainer');
echo 'Container name is: ' . $result->Name;
I am taking refernces from http://framework.zend.com/manual/1.12/en/zend.service.windowsazure.storage.blob.html
zend/Azure expert advice appreciated. thanks
Just add this code to the IndexController in your application
public function indexAction()
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$this->view->content = '<h1>Welcome to TestIndex!</h1>';
// Need to change
}
else
{
$storageClient = new Zend_Service_WindowsAzure_Storage_Blob('blob.core.windows.net', 'YOURNAME', 'YOURCODE');
$result = $storageClient->createContainer('container');
echo 'Container name is: ' . $result->Name;
var_dump($result->Name);
exit();
}
}

How to use extensions in Zend Framework Routing

I'd like to use an extension like .htm in my URLs. Is there a way to accomplish that?
I've defined the following rule:
frontend.route = '/:standort/:id'
When I use the following
frontend.route = '/:standort/:id.htm'
then the name of the variable is id.htm like in $params["id.htm"].
How can I tell the Zend Framework what to use as variables?
Greetings
//Edit
my full config looks like this now:
frontend.type = 'Zend_Controller_Router_Route_Regex'
frontend.route = '/(.?)/(\w+?\.htm)'
frontend.defaults.module = 'frontend'
frontend.defaults.controller = 'index'
frontend.defaults.action = 'index'
frontend.defaults.standort = 'national'
frontend.map.1 = 'standort'
frontend.map.2 = 'id'
this is how I load the config
$file = APPLICATION_PATH . '/modules/' . $module . '/configs/routing.ini';
if(Zend_Loader::isReadable($file)){
$config = new Zend_Config_Ini($file);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config);
}
You can do this with regex routes:
frontend.type = "Zend_Controller_Router_Route_Regex"
frontend.route = '/(.?)/(\w+?\.htm)'
frontend.map.1 = "standort"
frontend.map.2 = "id"
but unless you're trying to preserve an existing URL structure, I'd just leave the .htm out - it serves no purpose.

Module based application.ini in Zend Framework

I want to have module based application.ini in my application.
Is it possible?
Basic requirement arises because I am having multiple databases depending on modules.
Please guide.
You can use multiple Db in application.ini:
resources.db.master.adapter = "PDO_MYSQL"
resources.db.master.default = false
resources.db.master.params.dbname = "db1"
resources.db.master.params.host = "127.0.0.1"
resources.db.master.params.username = "root"
resources.db.master.params.password = ""
resources.db.slave.adapter = "PDO_MYSQL"
resources.db.slave.default = true
resources.db.slave.params.dbname = "db2"
resources.db.slave.params.host = "127.0.0.1"
resources.db.slave.params.username = "root"
resources.db.slave.params.password = ""
And initialize in your bootstrap:
public function _initDatabase() {
$config = $this->getApplication()->getOption('resources');
$dbArrays = array();
foreach($config['db'] as $name => $dbConf){
// Set up database
$db = Zend_Db::factory($dbConf['adapter'], $dbConf['params']);
$db->query("SET NAMES 'utf8'");
$dbArrays[$name] = $db;
if((boolean)$dbConf['default']){
Zend_Db_Table::setDefaultAdapter($db);
}
unset($db);
}
Zend_Registry::set("db", $dbArrays);
}
In my case I always save each adapter in the registry so I can use them separately later.
I also made a new class which extend Zend_Db_table where I have My own getAdapter($name) like so:
public function getAdapter($name = null){
if($name !== null){
$dbAdapters = Zend_Registry::get('db');
return $dbAdapters[$name];
}
return parent::getAdapter();
}
With that in each model I can do:
$this->getAdapter('slave')->fecthAll($sql);
$this->getAdapter('master')->fecthAll($sql);
The application.ini is read long before the module is determined. I'd suggest you forget about application.ini and instead try and write a controller plugin that will load in some additional configuration depending on which module was selected.