FAQ: Frequently Asked Questions
Menu
Configure your virtualhostDeliver an application on production server
Get a variable GET,POST
Get list of parameters
Manage your cache
Assign a variable from action to the view
Check if a variable is assigned on the view
Load a module on a layout placeholder
Execute a process to global module
Add a plugin
Call / Use a plugin
An export excel (csv exactly)
Get the module name
Get the action name
Get the navigation variable
Make a link to the action "list" of the module "article"
Assign a variable to the layout
Check a variable in the layout
Make a private access
Make a redirection
Make a website multi-language
Log in your website
Use the url-rewriting
Configure the default module
Read a file
Create/Write in a file
Create a directory
List file in a directory
List directory in an othen directory
JQuery
JQuery, make a link innerHtmlJQuery, update an html element
JQuery, make an ajax call with a callback
Answers
Configure your virtualhost
This is the virtualhost configuration for this case:Sources: /var/www/Projects/yourApplication
Url: www.projet.com
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.projet.com
DocumentRoot /var/www/Projects/yourApplication/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/Projects/yourApplication/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
When you will deliver on Production environment follow instruction below
Deliver an application on production server
Builder generate website in the Projects/ directoryYou should copy the framework directory (Lib/mkframeworkLib/) and your website directory (Projects/yourApplication) on the Production server.
Then you have to configure in your website where is the library (configure it in the file conf/site.ini.php, variable lib in the section [path])
[path]
lib=../../../Lib/mkframeworkLib/
[cache]
(...)
autoload.enabled
The virtuallhost configuration block : in this example the website is installed in /var/www
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.projet.com
DocumentRoot /var/www/yourApplication/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/yourApplication/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
If you use urlrewriting, #link ##urlrewriting #label read this link#
* warn to check permission on data/cache directory (the website have to write in it)
Get a variable GET,POST
_root::getParam('myvariable')
Get list of parameters
_root::getRequest()->getParams()
Manage your cache
You have to ways1. the entire website is cached
Configure enabled=1 in the file conf/site.ini.php, in the section [cache]
You can indicate a defined time validity for cache with the parameter lifetime (in the same section)
Note: if all the website is cached except some pages,
Add in method before_yourPageNotCached (for example a contact form...)
module_article extends abstract_module{
//this method is executed for the entire module before check the cache
public function before(){
//il suffit donc de forcer la valeur à 0
_root::setConfigVar('cache.enabled',0);
}
//this method is executed before the action _yourPageNotCached before check the cache
public function before_yourPageNotCached(){
//il suffit donc de forcer la valeur a 0
_root::setConfigVar('cache.enabled',0);
}
public function _yourPageNotCached(){
(...)
//your page not cached
}
}
2. you cache only a couple module/action
In this case, you have to use the methode _root::getCache(), which get the unique instance of _cache class
Cache is managed with view object (_view), you check is a cache exists
- if yes, you check its date validity (if it is recent, you use it)
- if no, you make your page and think to save your view in cache at end
private function getViewList(){
//is there an active cache which validiry is under 1 minute
if( _root::getCache()->isCached( 'article_list',1) == true ){
//if there is, get the cache view
$oView=_root::getCache()->getCached( 'article_list');
}else{
//if not, process
$oModelArticle=new model_article;
$tArticle=$oModelArticle->findAll();
$oView=new _view('article::list');
$oView->tArticle=$tArticle;
$oView->tColumn=$oArticleModel->getListColumn();
//at end we save the view in cache
_root::getCache()->setCache('article_list',$oView);
}
return $oView;
}
public function _list(){
$oView=$this->getViewList();
$this->oLayout->add('main',$oView);
}
note: you can use a cache for static parts (menu for example)
If it's a static part, don't limit the cache validity
public function _list(){
//is there a cache ?
if( _root::getCache()->isCached( 'menu') == true ){
//we always use it, we don't check the validiy
$oView=_root::getCache()->getCached( 'menu');
}else{
$tLink=array(
'Accueil' => 'pages::accueil',
'News' => 'pages::news',
'Présentation' => 'pages::presentation',
'Librairie' => 'pages::librairie',
'L ORM' => 'pages::orm',
'Tutoriaux' => 'pages::tutoriaux',
'CommentKonFait ?' => 'pages::faq',
'Builder' => 'pages::builder',
);
$oView=new _view('menu::index');
$oView->tLink=$tLink;
//we save the view in cache
_root::getCache()->setCache('menu',$oView);
}
return $oView;
}
Warn in a menu case, if you use it you will will always have the same menu, but it won't display if a menu is selected.
When you will ask for news page, it will display the menu cache (with link home selected) :(
To avoid this, you have to name the cache with an unique id for every declension of your menu (one by tab)
You can do this for example
public function _index(){
//there we create a unique id with the couple module/action
$sMenuId='menu_'._root::getModule().'_'._root::getAction();
//y a t il un cache
if( _root::getCache()->isCached( $sMenuId ) == true ){
$oView=_root::getCache()->getCached( $sMenuId );
}else{
$tLink=array(
'Accueil' => 'pages::accueil',
'News' => 'pages::news',
'Présentation' => 'pages::presentation',
'Librairie' => 'pages::librairie',
'L ORM' => 'pages::orm',
'Tutoriaux' => 'pages::tutoriaux',
'CommentKonFait ?' => 'pages::faq',
'Builder' => 'pages::builder',
);
$oView=new _view('menu::index');
$oView->tLink=$tLink;
//when you save the cache you will save each declension of your menu
_root::getCache()->setCache( $sMenuId ,$oView);
}
return $oView;
}
Assign a variable from action to the view
For example, assign article object from action to the view "show"module/article/main.php
$oView=new _view('article::show');
//we assign the variable $article to the view with the name "myArticle"
$oView->myArticle=$oArticle;
In the view
module/article/view/show.php
echo $this->myArticle->id;
echo $this->myArticle->titre;
Check if a variable is assigned on the view
For example, you want to check if a variable is assignedmodule/article/main.php
$oView=new _view('article::show');
//we assign the variable $oArticle to the view on the name "myArticle"
if($myCondition==true){}
$oView->myArticle=$oArticle;
}
In the view
module/article/view/show.php
if(isset($this->myArticle)){
echo $this->myArticle->id;
echo $this->myArticle->titre;
}
Manage the pagination of your site (in php)
In your file module/article/main.php
public function _listPagination(){
//first we get our entire article list
$oArticleModel=new model_article;
$tArticle=$oArticleModel->findAll();
//then we instance the module pagination (directory module/pagination)
$oModulePagination=new module_pagination;
$oModulePagination->setModuleAction('article::listPagination');//we indicate our couple module/action
$oModulePagination->setParamPage('page');//the variable name for the page
$oModulePagination->setLimit(2);
$oModulePagination->setPage( _root::getParam('page') );
$oModulePagination->setTab( $tArticle ); //we give it our article array
$oView=new _view('article::listPagination');
$oView->tArticle=$oModulePagination->getPageElement(); //we assign the article array to the view
$oView->tColumn=$oArticleModel->getListColumn();
$oView->oModulePagination=$oModulePagination->build(); //we assign the pagination module object to the view
$this->oLayout->add('main',$oView);
}
(...)
In your view module/article/view/listPagination.php
(...)
//add at your view end to show your pagination navigation
<?php echo $this->oModulePagination->show()?>
Load a module on a layout placeholder
You can need to load a module in your viewFor example a calendar module
$oView=new _view('module::action');
$tpl->oViewCalendrier=module_calendrier::getInstance()->build(); //the method build has to return the view object
//ou
$oModuleCalendrier=new module_calendrier;
$oView->oViewCalendrier=$oModuleCalendrier->build(); //the method build has to return the view object
<?php echo $this->oViewCalendrier->show();?>
Execute a process to global module
This is the job of the method before(), this method is always executed when the module is loaded by the framework (note: module, not embedded module)
function before(){
$this->oLayout=new _layout('template1');
$this->oLayout->addModule('menu','menu::index');
//for all the module we load the module menu at "menu" placeholder
}
Add a plugin
Simply create a class file in the directory plugin/, your class has to begin with plugin_For example
Class plugin_votrePlugin{
//the code of your plugin
}
An export excel (csv exactly)
In your module file for example module/article/main.php
(...)
public function _listeArticleCsv(){
$tArticle=model_article::getInstance()->findAll();
$oView=new _view('article::listeArticleCsv');
$oView->tArticle=$tArticle;
//$this->oLayout has been instanciate in the before method
$this->oLayout->add('main',$oView);
$this->oLayout->setLayout('download');
$this->oLayout->sFileName='file_article_list.csv';
$this->oLayout->sExtension='csv';
}
In a layout file layout/download.php
<?php
header('Cache-Control:public');
header('Pragma:');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Disposition: attachment; filename=\"".$this->sFileName."\"");
header('Content-Transfer-Encoding:binary');
header("Content-Type: application/".$this->sExtension.";name=\"".$this->sFileName."\"");
ob_clean();
flush();
echo $this->load('main')?>
In the view module/article/view/listeArticleCsv.php
TITRE;AUTEUR
<?php foreach($this->tArticle as $oArticle):?>
<?php echo $oArticle->titre ?>;<?php echo $oArticle->auteur ?>
<?php endforeach;?>
Get the module name
This framework use a variable which contains the couple module::actionYou can display/get the module name like this
echo _root::getModule();
Get the action name
This framework use a variable which contains the couple module::actionYou can display/get the action name like this
echo _root::getAction();
Get the navigation variable
This framework use a variable which contains the couple module::actionYou can display/get the navigation variable like this
echo _root::getParamNav();
Make a link to the action "list" of the module "article"
You can make a link whith use _root::getLink()For example to link the module "article" and action "list":
//without parameter
<a href="<?php echo _root::getLink('article::list');?>">libelle</a>
//with parameter (for example edit the id 2)
<a href="<?php echo _root::getLink('article::edit',array('id'=>2));?>">edit the article 2</a>
[UPDATE 27nov09]
Add the possibility to make a link with an array
//avec parametre (par exemple edition id 2)
<a href="<?php echo _root::getLink(array('article::edit','id'=>2));?>">edit the article 2</a>
Assign a variable to the layout
You process as you do with the views (_view)
class module_article extends abstract_module{
public function before(){
$this->oLayout=new _layout('template1');
(...)
}
public function _show(){
$this->oLayout->title="The article title";
}
}
layout/template1.php
<html>
<head>
<title><?php echo $this->title ?></title>
</head>
(...)
</html>
Check a variable in the layout
As you do with the view (_view)
class module_article extends abstract_module{
public function before(){
$this->oLayout=new _layout('template1');
(...)
}
public function _show(){
$this->oLayout->test="The article title";
}
}
layout/template1.php
<html>
<head>
<title>you website.com</title>
</head>
<body>
<?php if(isset($this->test)):?><p><?php echo $this->test?></p><?php endif;?>
(...)
</html>
Make a private access
You have 2 ways to make a private accessYou can make you website
1. all the site is private
2. all the site is public except some pages
You can manage the total private access if you configure enabled=1 in the [auth] section
In conf/site.ini.php
[auth]
;note : >= php5.2 dans le php.ini
;session.cookie_httponly=1
;session.use_cookies = 1
;session.use_only_cookies = 1
enabled=0
You can manage the private acces on a module
For example
class module_exemple extends abstract_module{
public function before(){
//with this method you unforce private access for this module
_root::getAuth()->enable();
}
}
and here you decide to make a private access on a couple module/action
class module_exemple extends abstract_module{
(...)
public function before_list(){
//here you unforce private access for this action
_root::getAuth()->enable();
}
public function _list(){
//page privee
}
note: when you use the private access, you have to configure the section [auth]
In conf/site.ini.php
[auth]
;note : >= php5.2 dans le php.ini
;session.cookie_httponly=1
;session.use_cookies = 1
;session.use_only_cookies = 1
enabled=0
class=plugin_auth
module=auth::login
session.timeout.enabled=1
session.timeout.lifetime=(60*1)
As you can read in this section, there is a variable which contains the private module, here auth::login
If you are on private page and you are not log on, it will redirect you to this parameter module. (variable module)
You can read a variable which contains the puglin used to check access (variable class)
The framework will call the method isConnected() on you class (plugin_auth)
Make a redirection
_root::redirect('module::action'[,$tParam]);
For example to redirect to the page which show the article 2
_root::redirect('article::show',array('id'=>2) );
Make a website multi-language
To make you website multi-language need 3 steps1. configure a php array by language
2. use the method to translate the tag in your language
3. provide links to swich between language and keep the language chosen
First step, go to the directory data/i18n, you can see one file by language
File data/i18n/en.php
<?php _root::setConfigVar('tLangue',array(
'BONJOUR' => 'Hello',
'BIENVENUE' => 'Welcome',
'CHOISISSEZ_LANGUE' => 'Choose your language',
'DU_TEXTE_EN_FRANCAIS' => 'Some text in english',
'La_date' => 'The date',
))?>
Second step, you the static get method of the plugin plugin_i18n
<?php echo plugin_i18n::get('BONJOUR'); ?>
note: you can add a function in your plugin plugin_i18n at the end
function tr($sText){
return plugin_i18n::get($sText);
}
Last step, you can display some links by languaget
In your layout, or your views put a link
For example
<a href="?lang=fr">FR</a>
In module, think to intecept the value to load the language file
_root::startSession();
if( _root::getParam('lang') ){
$_SESSION['lang']=_root::getParam('lang');
}
if(!isset($_SESSION['lang'])){
$_SESSION['lang']=_root::getConfigVar('language.default');
}
plugin_i18n::load($_SESSION['lang']);
Log in your website
The mkFramework provide you 4 log level1. error
2. warning
3. applicative message
4. information
This is enable/disable in configuration file conf/site.ini.php, in section [log]
[log]
class=plugin_log
error=0
warning=0
application=0
information=0
When you enable logs, you can read in directory data/log/ the framework logs
But you can use the loger if you need with method _root::getLog()->log()
_root::getLog()->info('An information message');
_root::getLog()->warning('A warning message');
_root::getLog()->error('An error message');
_root::getLog()->log('You applicative message');
An example of log
2012-07-14;12:13:43;info;module a appeler [article::list]
2012-07-14;12:13:43;info;appel module [article::before]
2012-07-14;12:13:43;info;--layout: initialisation [template1]
2012-07-14;12:13:43;info;-layout: choix de [layout/template1.php]
2012-07-14;12:13:43;info;-layout: ajout appel module [menu::index] a la place [menu]
2012-07-14;12:13:43;info;--vue: initialisation [menu::index]
2012-07-14;12:13:43;info;---vue: assignation [tLink]
2012-07-14;12:13:43;info;appel module [article::_list]
2012-07-14;12:13:43;info;sql select:
2012-07-14;12:13:43;info;--vue: initialisation [article::list]
2012-07-14;12:13:43;info;---vue: assignation [tArticle]
2012-07-14;12:13:43;info;---vue: assignation [tColumn]
2012-07-14;12:13:43;info;-layout: ajout appel vue [module/article/view/list.php] a la place [main]
2012-07-14;12:13:43;info;appel module [article::after]
2012-07-14;12:13:43;info;-layout: affichage [layout/template1.php]
2012-07-14;12:13:43;info;-layout: chargement/affichage place [menu]
2012-07-14;12:13:43;info;--vue: affichage [module/menu/view/index.php]
2012-07-14;12:13:43;info;-layout: chargement/affichage place [main]
2012-07-14;12:13:43;info;--vue: affichage [module/article/view/list.php]
Use the url-rewriting
If you want to use url-rewriting, you need:1. enable the mod_rewrite on your apache
2. configure your virtualhost to enable url rewriting, or create a .htaccess
3. enable in your file conf/site.ini.php the variable enabled=1 in the section [urlrewriting]
4. configure your routing rules in file conf/routing.php
Point 2
Configure your virtualhost
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.projet.com
DocumentRoot /var/www/Projects/yourApplication/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/Projects/yourApplication/public>
Options Indexes FollowSymLinks MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Point 3,
In your file conf/site.ini.php
[urlrewriting]
enabled=0
class=plugin_routing
conf=conf/routing.php
[urlrewriting]
enabled=1
class=plugin_routing
conf=conf/routing.php
Point 4,
Edit your file conf/routing.php
<?php
$tab=array(
'articles.html' => array(
'nav'=>'article::list',
),
'articleDetail_:id:.html' => array(
'nav'=>'article::show',
'tParam' => array('id')
),
'articleEdit_:id:' => array(
'nav'=>'article::edit',
'tParam' => array('id')
),
'taches' => array(
'nav'=>'tache::list',
),
'taches_archives' => array(
'nav'=>'tache::archivelist',
),
'tacheDetail_:id:' => array(
'nav'=>'tache::show',
'tParam' => array('id')
),
'tacheEdit_:id:' => array(
'nav'=>'tache::edit',
'tParam' => array('id')
),
//page 404 (page non trouve)
'404' => array(
'nav' => 'article::list',
),
);
To explain: when you call a page, the framework look for on of keys in this array
For example, you call the page articles.html which will call module "article" and action "list"
'articles.html' => array(
'nav'=>'article::list',
),
'article_:id:.html' => array(
'nav'=>'article::show',
'tParam' => array('id')
),
We use the variable "nav" wich contains the couple module/action and "tParam" (not mandatory) if there is some parameters to get
For example, if you need a variable "CAT_PKEY" in a category page (index.php?:nav=categorie::list&CAT_PKEY=2)
'categorie_:CAT_PKEY:.html' => array(
'nav'=>'categorie::list',
'tParam' => array('CAT_PKEY')
),
You can enable/disable the urlrewriting.enabled to switch between mode normal/urlrewriting, it's automatic (the method _root::getLink() is in charge of this)
Configure the default module
The default page of your website is configured in [navigation] sectionIn your file conf/site.ini.php
[navigation]
scriptname=index.php
var=:nav
module.default=article
action.default=list
You can read the variable "module.default" which indicate the default module, and the variable "action.default" for default action to call.
In this configuration, the framework will call the module "module_article" and his method "_list" (it is an action)
Read a file
Use the class _file
$oFile=new _file('/tmp/myfile.txt');
print $oFile->getContent();
Create/Write in a file
Use the class _file
$oFile=new _file('/tmp/myfile.txt');
$oFile->setContent('my text');
$oFile->save();
Create a directory
Use the class _dir
$oDir=new _dir('/tmp/myDirectory');
print $oDir->save();
List file in a directory
Use the class _dir
$oDir=new _dir('/tmp/myDirectory');
$tFile= $oDir->getListFile();
foreach($tFile as $oFile){
print $oFile->getAdresse();
}
List directory in an othen directory
Use the class _dir
$oDir=new _dir('/tmp/myDirectory');
$tDir $oDir->getListDir();
JQuery, make a link innerHtml
You can update an html element with the return of a pageWith the plugin plugin_jquery, you can create javascript function using jquery framework
For example, you can have an article list, you click on it and display the article detail
First create the javascript function "showArticle" which will need one arguement (the article id)
<?php
$oFunctionShow=new plugin_jquery('showArticle',array('param_id'));
$oFunctionShow->addLinkUpdateElement( $this->getLink('article::showajax',array('id'=> '$param_id')) ,'rendu') ;
$oFunctionShow->addModifyElement('rendu','show');
echo $oFunctionShow->getJs();
?>
<script language="Javascript">
function showArticle(param_id){
$.ajax({
url: 'index.php?:nav=article::showajax&id='+param_id+'',
success: function(response) {
// update status element
$('#rendu').html(response);
}
});
$('#rendu').show();}</script>
<?php foreach($tArticle as $oArticle):?>
<a href="#" onclick="showArticle(<?php echo $oArticle->id?>)"><?php echo $oArticle->titre?></a>
<?php endforeach;?>
<div id="rendu"></div>
note: to use jQuery library, you have to include it in your layout
Think to get the latest version of jQuery to keep it in local http://docs.jquery.com/Downloading_jQuery#Current_Release (or link the latest)
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
JQuery, update an html element
You can update an html element with jQuery
<?php
$oFunctionShow=new plugin_jquery('afficher');
$oFunctionShow->addModifyElement('rendu','show');
echo $oFunctionShow->getJs();
?>
<script language="Javascript">function affiche(){
$('#rendu').show();}</script>
note: to use jQuery library, you have to include it in your layout
Think to get the latest version of jQuery to keep it in local http://docs.jquery.com/Downloading_jQuery#Current_Release (or link the latest)
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
JQuery, make an ajax call with a callback
You can make an ajax call and ask to send the response to a javascript function
<?php
$oFunctionShow=new plugin_jquery('showArticle',array('param_id'));
$oFunctionShow->addLinkCallFunction( $this->getLink('article::showajax',array('id'=> '$param_id')) ,'interprete') ;
echo $oFunctionShow->getJs();
?>
<script language="Javascript">function showArticle(param_id){
$.ajax({
url: 'index.php?:nav=article::showajax&id='+param_id+'',
success: function(response) {
// call function
interprete(response);
}
});
}</script>
note: to use jQuery library, you have to include it in your layout
Think to get the latest version of jQuery to keep it in local http://docs.jquery.com/Downloading_jQuery#Current_Release (or link the latest)
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>