Creating a well-organized file structure is crucial for maintaining and scaling a software framework. This tutorial will guide you through the process of setting up a standard file structure for a module in Ketroute Framework. We will cover different types of modules: add, edit, list and provide examples to illustrate the structure.
A typical module in your framework should follow a standardized structure to ensure consistency and readability. Here is an example of a well-organized module file structure:
component
----application
----------------intranet
------------------------modules
-----------------------------------module-name.php
$strComponentRealname = $this->getComponentRealname();
$arrErrors = array();
$form_prefix = 'frm_';
$arrFields = $this->runtime()->getAddFields(); // you can also specify an array i array('field1', 'field2')
if(KRequest::isPosted())
{
//validate form elements/fields
foreach ($arrFields as $FieldName){
if($error = KSystemManager::getFormElementError($form_prefix,$this->runtime()->getFieldProperties($FieldName), $FieldName, KRequest::getPost("{$form_prefix}{$FieldName}"))){
$arrErrors[$form_prefix.$FieldName] = $error;
}
}
// no error
if(empty($arrErrors)){
// global signature
$this->runtime()->save($form_prefix, $this->runtime()->getTableName(), $arrFields);
// capture audit log
$this->logAuditTrail("Added a new record: {$this->getModule()}", $this->runtime()->getTableName(), $this->runtime()->getActiveRecord());
// raise success message
KSecurity::setActionSuccess(KLanguage::getWord($this->runtime()->addActionOnCompleteMessage(), array('item' => KLanguage::getWord("{$strComponentRealname}-{$this->getModule()}"))));
// response with the redirector rule
$this->stopRedirector($this->urlPath(0,1,1).'list/');
}
}
else{
// setup the back page -
$this->startRedirector();
}
$replace_tags = array('form_action' => KRequest::getUri(),
'back_url' => (KRequest::isQueryString('backurl') && KRequest::getQueryString('backurl')) ? base64_decode(KRequest::getQueryString('backurl')) : KSecurity::getSession('BACK_URL'),
'fields'=> $this->runtime()->createFormFields($this, $arrFields, $form_prefix, $arrErrors),
'form_identifier'=> "mykfwform-{$this->getComponent()}-{$this->getModule()}",
);
// this list page is ajax capable
$this->render($replace_tags);
By following this file structure, you can keep your modules organized and maintainable. The most important thing to remember is that 99% of the time, you don't have to write your own modules. The Ketroute Framework is capable of creating these forms for you based on the rules and properties you have defined in the configurations.
We discourage developers from writing their own module process files, unless if it's absolutely necessary.
Thank you for following along with this tutorial. Happy coding!
Your download is here.