Documentation 2.x

Model View Controller (MVC)

ImpressPages 2.0 has new MVC implementation. But it applies only for frontend. Backend has its own entry point. See example on how to create simple view and controller in backend.

 ImpressPages 3.X will have one enty point and unified MVC implementation.

Model

ImpressPages currently uses no automatization of communication with database except connection / disconnection. We suggest to use model.php files to store interactions with databse.

Always use DB_PREF constant before table name. It will allow to run several different installations on the same database.

Example of SQL query with DB_PREF prefix:

      $sql = "
            SELECT * 
            FROM
                `".DB_PREF."m_example_module` 
            WHERE
                `condition` = '" . mysql_real_escape_string($inputValue) . "'
        ";
        $rs = mysql_query($sql);
        if (!$rs){
            throw new Exception('Can\'t get information from the database '.$sql.' '.mysql_error());
        }

See complete example on how to use MVC structure in your plugin.

View

Views are plane HTML / PHP files. They can be placed anywhere, but we recommend to place them in folder "view".

Simple view file example:

<h1 class="ipwTitle">
    <?php echo $this->esc($var1); ?>
</h1>
<p>
    <?php echo $var2 ?>
</p>

To render view files, use following syntax:

$data = array (
    'variable1' => 'value1',
    'variable2' => 'value2'
);
$renderedHtml = \Ip\View::create('view/page_options_general.php', $data)->render();
echo renderedHtml;

Paths to views are relative to file where view file is accessed. For eg. this code:

\Ip\View::create('viewFile.php', $data)->render();

will search for viewFile.php in the same directory as php file where this code is written. We recommend to put all views into folder 'view'. So often you will use something like ...create('view/viewFile.php', $data)....

Within the view you can use following functions:

<?php

echo $this->esc($anyVariable); //equivalent of htmlspecialchars. 

echo $this->par('standard/configuration/translations/copyright'); //print parameter value

echo $this->escPar('standard/configuration/translations/copyright'); //print and escape parameter value

echo $this->renderWidget('IpTitle', array('title' => $newsletterTitle)); //render and print title widget. In this way you can print any widget. You just need to know which data to pass to widget.

echo $this->renderWidget('IpTitle', array('title' => $newsletterTitle), 'level2'); //the same as above, but 'level2' layout instead of default.

$this->getDoctype(); //returns current doctype constant. See ip_cms/includes/Ip/View.php for constant values

echo \Ip\View::create('subview.php', $this->getData())->render(); //include another file within view and pass the same data.

?>

See complete example on how to use MVC structure in your plugin.

All views can be overriden in theme directory.

Controller

Controller file should be named 'controller.php' and be root directory of your plugin. Each public method represents public action. 

Current controller is determined by $_POST parameters. So if you want to call method myMethod on your plugin examples/simpleController, you need to post three variables: 

'g' => 'examples' - your plugin group

'm' => 'simpleController' - your plugin name

'a' => 'myMethod' - method to execute. 'index' by default.

.. => ... - you can add your own variables as many as you like.

 

Alternatively you can pass the same parameters via $_GET. The result will be the same.

Workflow

  1. System initialization
  2. If 'g', 'm', 'a' variables exists in $_POST or $_GET, required controller action is being executed.
  3. Output page content.

Controller should never echo content by it self. To set browser output, use this code:

<?php

function myMethod() {
    global $site;
    $content = 'Some content. Could be HTML, Json or anything else. If you output HTML, use views';
    $site->setOutput($answer);
}

?>

If controller does not execute $site->setOutput method the page proceed to be rendered as usual. 

See complete example on how to use MVC structure in your plugin.

Comments (2)

Peter

Peter

Hi,
I'd like to interact with the MySQL database from within ImpressPages. I want to view and change simple tables. I understand that this is not yet available. Is this correct or is there someone who created a solution to work with?

Mangirdas

Mangirdas

Just use native PHP musql_query. See default modules / plugins for example.

Write a comment

You must be logged in to comment.