aRvi Examples

These examples demonstrate the simplicity of aRvi and will help you using it in the proper way.

Controller

Simple Controller

class SettingsController extends \aRvi\Controller {
    
    protected function generate() {

        // Do your stuff here ...
        // Interoperate with your view
        // $this->view-> ....
        // That's it

        return parent::generate();
    }

}

Redirection

class SettingsController extends \aRvi\Controller {
    
    protected function generate() {

        return new \aRvi\WebResponse\RedirectionWebResponse("http://www.example.com/", 301);
    }

}

Redirection with UriFetcher

class SettingsController extends \aRvi\Controller {
    
    protected function generate() {

        return new \aRvi\WebResponse\RedirectionWebResponse($this->uriFetcher->fetch("home"), 302);
    }

}

JSON Response without View

class SettingsSaveProcessController extends \aRvi\Controller {
    
    protected function generate() {

        $response = $this->createResponseByViewType();

        $response->setPayload(array(
            "success" => true,
            "message" => "Successfully saved!",
        ));
        
        return $response; 
    }

}

View

Simple View

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        $content = "<html>";
        $content .= "<body>";
        $content .= "Hello World!";
        $content .= "</body>";
        $content .= "</html>";

        return $content;
    }

}

Passing data from Controller to View

class SettingsController extends \aRvi\Controller {
    
    protected function generate() {

        $this->view->payload->firstName = "Ronald";
        $this->view->payload->fruits = array(
            "apple",
            "cherry",
            "orange"
        );

        return parent::generate();
    }

}

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        $content = "Hello " . $this->payload->firstName . "<br />";

        foreach ($this->payload->fruits as $strFruit) {
            $content .= $strFruit . "<br />";
        }

        return $content;
    }

}

UriFetcher

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        $uri = $this->uriFetcher->fetch("home");
        $label = $this->uriFetcher->fetchURILabelByPageId("home");

        $content = "<a href=\"{$uri}\">{$label}</a>";

        return $content;
    }

}

Adding GET-Parameters

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        $uri = $this->uriFetcher->fetch("home");
        $label = $this->uriFetcher->fetchURILabelByPageId("home?param1=this&param2=that#jump"); // /home/?param1=this&param2=that#jump

        $content = "<a href=\"{$uri}\">{$label}</a>";

        return $content;
    }

}

Variables in Page Registry

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        $uri = $this->uriFetcher->fetch("home");
        // Page Registry: <uri>/home/%param1%/</uri>
        $label = $this->uriFetcher->fetchURILabelByPageId("home", array("param1" => "this")); // /home/this/

        $content = "<a href=\"{$uri}\">{$label}</a>";

        return $content;
    }

}

Client

Accessing client data

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        $content = "IP-address " . $this->client->strIpAddress . "<br />";
        $content .= "Real IP-address " . $this->client->strRealIpAddress . "<br />";

        return $content;
    }

}

Extending client

namespace App\Client;

class MyClient extends \aRvi\Client\Client {

    public $intUserId = null;
    
    /**
     * Constructor
     * 
     * @param array $arrClient Client data
     */
    public function __construct($arrClient) {
        
        parent::__construct($arrClient);
        
        $this->intUserId  = $this->getValueOrNull($arrClient, "user_id");
        
    }
    
    /**
     * Returns whether a user is logged in or not
     * 
     * @return boolean Status
     */
    public function isLoggedIn() {
        return isset($this->intUserId);
    }
    
}

Checking login state

class SettingsHtmlView extends \aRvi\View\View {

    protected function render() {       
 
        if (true === $this->client->isLoggedIn()) {
            $content = "Logged in";
        } else {
            $content = "Not logged in";
        }

        return $content;
    }

}

Request

Accessing request data

class SettingsController extends \aRvi\Controller {
    
    protected function generate() {

        // /settings?param1=17&param2=05
        echo $this->request->param1 . " - " . $this->request->param2;

        return parent::generate();
    }

}

Page Registry

<entry>
    <id>settings</id>

    <title><![CDATA[User settings form]]></title>
    <uri>/settings</uri>

    <methods>GET</methods>

    <namespace>settings</namespace>
    <view>settings_form</view>

    <login>force_login</login>
    <https>force_on</https>        
</entry>

Single Page Registry - Full length example

<?xml version="1.0" encoding="UTF-8"?>
<registry>

    <entry>
        <id>_default_</id>
        
        <type>html</type>
        
        <login></login>
        <https></https>
        
        <redirections>
            <login>
                <force_login>/login/?url=%request_uri_url_encoded%</force_login>
                <deny_login>/my-home/</deny_login>
            </login>
            <ajax_login>
                <force_login>/ajax/login/?url=%request_uri_url_encoded%</force_login>
                <deny_login></deny_login>
            </ajax_login>
            <https>
                <force_on>https://%http_host%%request_uri%</force_on>
                <force_off>http://%http_host%%request_uri%</force_off>
            </https>
        </redirections>
        
        <permissions></permissions>
    </entry>

    <entry>
        <id>settings</id>

        <title><![CDATA[User settings form]]></title>
        <uri>/settings</uri>

        <methods>GET</methods>

        <namespace>settings</namespace>
        <view>settings_form</view>

        <login>force_login</login>
        <https>force_on</https>        
    </entry>
</registry>

Landing Page

require_once("./../bootstrap.php");

try {
    $provider = new \aRvi\PageRegistry\Provider\PageRegistryXmlProvider("./../data/page_registry.xml");

    $registry = new \aRvi\PageRegistry\Provider\MultiProviderPageRegistry();
    $registry->addProvider($provider);
} catch (Exception $e)  {
}

try {

    $webRequestHandler = new \aRvi\WebRequest\Handler\WebRequestHandler();
    
    $webRequestHandler->pageRegistry = $registry;
        
    $webRequestHandler->dispatcher = new \aRvi\Dispatcher\Dispatcher();
    $webRequestHandler->client = \App\Client\ClientFactory::getClient();
    $webRequestHandler->request = \aRvi\ClientRequest\ClientRequestFactory::getWebRequest();
    $webRequestHandler->cookie = \aRvi\ClientRequest\ClientRequestFactory::getCookieRequest();
    
    $webRequestHandler->fltRenderTimeStart = microtime();
    $webRequestHandler->strHostName = gethostname();
    
    if (false === isset($webRequestHandler->request->_page) && true === isset($_GET['_page'])) {
        $webRequestHandler->request->_page = $_GET['_page'];
    }
    if (false === isset($webRequestHandler->request->_type) && true === isset($_GET['_type'])) {
        $webRequestHandler->request->_type = $_GET['_type'];
    }

    $webRequestHandler->run();

} catch (\aRvi\WebRequest\Handler\InvalidPageIdException $e) {
    
    header("Cache-Control: private");
    header("HTTP/1.1 404 Not Found");
    header("Location: /404/");
    header("Connection: close");
    exit;
    
} catch (\aRvi\Dispatcher\DispatcherFileNotFoundException $e) {
    
    header("Cache-Control: private");
    header("HTTP/1.1 404 Not Found");
    header("Location: /404/");
    header("Connection: close");
    exit;

} catch (\aRvi\WebRequest\Handler\InvalidRequestMethodException $e) {
    
    header("Cache-Control: private");
    header("HTTP/1.1 405 Method Not Allowed");
    header("Allow: " . $e->strAllowedMethods);
    header("Connection: close");
        
    echo "Method not allowed: " . $e->getMessage();
    exit;
    
}