About this example : Hello Word

Controller

controllers/examples/cms/HelloWorld.php

<?php

namespace controllers\examples\cms;

use framework\Controller;
use models\examples\cms\HelloWorld as HelloWorldModel;
use views\examples\cms\HelloWorld as HelloWorldView;

class HelloWorld extends Controller
{

    protected function autorun($parameters = null)
    {
        // Uses a Model
        $this->model = new HelloWorldModel();

        // Uses a View
        $this->view = new HelloWorldView();

        // Gets some data from Model
        $modelData = $this->model->getMessage();

        // Run some method of the View by passing it some data (from Model)
        $this->view->setMessage($modelData);
    }

}

Model

models/examples/cms/HelloWorld.php

<?php

namespace models\examples\cms;

use framework\Model;

class HelloWorld extends Model
{
    /**
     * Gets a simple message.
     * @return string
     */
    public function getMessage()
    {
        return "Hello 2 World from PHP Web MVC Framework";
    }
}

View

views/examples/cms/HelloWorld.php

<?php

namespace views\examples\cms;

use framework\View;

class HelloWorld extends View
{

    /**
    * Object constructor.
    *
    * @param string|null $tplName The html template containing the static design.
    */
    public function __construct($tplName = null)
    {
        if (empty($tplName))
            $tplName = "/examples/cms/hello_world";
        parent::__construct($tplName);
    }

    /**
     * Set  Message PlaceHolder with a given value
     *
     * @param string $msg
     */
    public function setMessage($msg){
        $this->setVar("Message",$msg);
    }
    
}

HTML Template

templates/examples/cms/hellp_world.html.tpl

<!DOCTYPE html>
<html>
<head>
    <title>Hello World Example</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap core CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
    <![endif]-->
</head>
<body>

<div class="container">
    <h1>Web MVC Framework</h1>
    <h2>Hello World</h2>
    <p>
        This example shows the basic concept for data communication among all the MVC layers.<br>
        It uses a simple <strong>placeholder</strong>, located inside of the GUI static design,
        for placing data provided from model and through the following control flow:<br><br>
    </p>
    <div class="well">
        <ol class="h5">
            <li>HTTP Request => Dispatcher => Controller</li>
            <li>Model <=> Controller <=> View <= Template</li>
            <li>Controller => Dispatcher => Response</li>
        </ol>
    </div>
    <p> 1) and 3) are automatically handled by the framework dispatcher</p>
    <p> 2) is made by extending the Framework Classes (Controller,Model,View)and by consuming their services</p>

    <h3>{Message}</h3>
    <br /><br />

    <a href="https://www.webmvcframework.com/webmvc/examples/about/example/hello_world" class="btn btn-info">Mostra codice sorgente</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/cms/hello_world" class="btn btn-success">Ricarica</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/" class="btn btn-primary">Indice degli esempi</a>

</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html> 


Back