PHPainfree2 App Routing Example

Using the $App->objects[] Array in PHPainfree2

$App->objects[] is the hard-working, private sibling of $App->data[]. It knows how to keep a secret.


<?php 
/**
 * test/controllers/test.php - Primary controller for test application.
 */
// All visible via the JSON API
$App->data = array(
	'info'    => "You can put whatever information in this array that you want",
	/* snipped for brevity */
);

// Example of $App->objects[]. This is another place to store data 
// you want to expose to your view templates, but this variable is NOT 
// exposed automatically in a JSON/API request 
$App->objects['private-data'] = "Don't leak this in JSON";
$App->objects['user_id']  = 1;
$App->objects['username'] = "Eric Harrison";
$App->objects['password'] = "look@MySecr3tPASSword24!";

The $App->objects[] array is also a fresh new array created with every request. The big difference between $App->objects[] and $App->data[] is that $App->objects[] is NEVER used for any magical purpose behind the scenes in either Painfree.php or App.php.

You can put any data inside the objects[] property to use in your view templates, and unless you intentionally expose those values inside your template manually, it will never be visible to any user and will be discarded at the end of every request.

See $App->data[] and JSON API for more details.

$App->objects[] Template Usage

Use $App->objects[] in your controllers and templates the exact same way you would use $App->data[]. Any values you store in the object[] property can be retrieved using whatever key you specified at assignment time.


<?php 
/** test/views/test.php - Primary template for test application. */
/** @var App $App */
$App = get_App();

$user_id  = $App->objects['user_id'];
$username = $App->objects['username'];
$is_admin = $App->objects['is_admin'] ?? false;
?>
<h2><?= $username; ?>'s Page</h2>
<?php 
	if ( $is_admin ) {
?> 
<a href="/user/<?= $user_id; >/edit">Edit User</a>
<?php 
	}
?> 
<h2>
In the controller

<?php 
/** test/controllers/test.php - Primary controller for test application. */
$App = get_App();

require_once $App->BASE_PATH . '/includes/vendor/Parsedown/Parsedown.php';
$pd = new Parsedown();
$pd->setSafeMode(true);
$App->objects['Parsedown'] = $pd;

Passing Large Objects

This property was initially created as a means to create a single large object in a controller and make that object available inside the template without resorting to either polluting the global namespace or by forcing the template to recreate commonly-used objects from libraries and packages.

In the view template

<?php 
/** test/views/test.php - Primary template for test application. */
$App = get_App();

$pd = $App->objects['Parsedown'];
?>
<div class="parsedown"><?= $pd->text($App->data['info']); ?></div>

Using the objects in our templates

In this example, we create an instance of the Parsedown Markdown library in the controller and store that in $App->objects['Parsedown']. Inside our template, we grab a reference to that object and use it to generate HTML from a potential markdown-formatted string in $App->data['info'].

PHPainfree Debugging Console [exec: 0.0021s]

1. App

App = App Object
(
    [env] => development
    [title:App:private] => PHPainfree2
    [htmx] => 
    [htmx_boosted] => 
    [headers] => Array
        (
            [accept] => */*
            [user-agent] => Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
            [accept-encoding] => gzip, br, zstd, deflate
            [host] => php.programming-is-easy.com
        )

    [response_headers] => Array
        (
            [X-Frame-Options] => SAMEORIGIN
        )

    [route] => test/objects
    [app] => test
    [view] => test
    [id] => objects
    [action] => 
    [output_file] => 
    [data] => Array
        (
            [sub-pages] => Array
                (
                    [main] => Array
                        (
                            [label] => App Routing
                            [title] => Using App Routing
                            [selected] => 
                        )

                    [objects] => Array
                        (
                            [label] => $App->objects[]
                            [title] => The $App->objects[] Array
                            [selected] => 1
                        )

                    [data] => Array
                        (
                            [label] => $App->data[]
                            [title] => The $App->data[] Array
                            [selected] => 
                        )

                    [json] => Array
                        (
                            [label] => JSON API
                            [title] => Automatic JSON API
                            [selected] => 
                        )

                )

            [template] => test
            [visible-in-JSON] => 1
            [records] => Array
                (
                    [0] => Array
                        (
                            [0] => 4
                            [1] => 1
                            [2] => PHP
                            [3] => 27
                            [4] => 1
                            [5] => 0
                        )

                    [1] => Array
                        (
                            [0] => 5
                            [1] => 1
                            [2] => Javascript
                            [3] => 23
                            [4] => 1
                            [5] => 0
                        )

                    [2] => Array
                        (
                            [0] => 1
                            [1] => 1
                            [2] => C
                            [3] => 29
                            [4] => 1
                            [5] => 0
                        )

                    [3] => Array
                        (
                            [0] => 3
                            [1] => 1
                            [2] => C++
                            [3] => 20
                            [4] => 0
                            [5] => 1
                        )

                    [4] => Array
                        (
                            [0] => 9
                            [1] => 1
                            [2] => Python
                            [3] => 4
                            [4] => 0
                            [5] => 1
                        )

                    [5] => Array
                        (
                            [0] => 8
                            [1] => 1
                            [2] => Jai
                            [3] => 2
                            [4] => 1
                            [5] => 0
                        )

                    [6] => Array
                        (
                            [0] => 7
                            [1] => 1
                            [2] => Rust
                            [3] => 2
                            [4] => 0
                            [5] => 1
                        )

                )

        )

    [objects] => Array
        (
            [private-data] => Don't leak this in JSON
            [user_id] => 1
            [username] => Eric Harrison
            [password] => look@MySecr3tPASSword24!
        )

    [BASE_PATH] => /var/www/vhosts/php.programming-is-easy.com
    [BASE_URI] => https://php.programming-is-easy.com
    [db] => 
    [route_parts] => Array
        (
            [0] => test
            [1] => objects
        )

)

2. Painfree

Painfree = PHPainfree Object
(
    [Version] => 2.3.0
    [URI] => https://php.programming-is-easy.com/test/objects
    [route] => test/objects
    [Root] => /var/www/vhosts/php.programming-is-easy.com/
    [Autoload] => Array
        (
            [PainfreeUtils.php] => /var/www/vhosts/php.programming-is-easy.com/includes/Autoload/PainfreeUtils.php
        )

    [db] => 
    [__debug] => Array
        (
            [App] => App Object
(
    [env] => development
    [title:App:private] => PHPainfree2
    [htmx] => 
    [htmx_boosted] => 
    [headers] => Array
        (
            [accept] => */*
            [user-agent] => Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
            [accept-encoding] => gzip, br, zstd, deflate
            [host] => php.programming-is-easy.com
        )

    [response_headers] => Array
        (
            [X-Frame-Options] => SAMEORIGIN
        )

    [route] => test/objects
    [app] => test
    [view] => test
    [id] => objects
    [action] => 
    [output_file] => 
    [data] => Array
        (
            [sub-pages] => Array
                (
                    [main] => Array
                        (
                            [label] => App Routing
                            [title] => Using App Routing
                            [selected] => 
                        )

                    [objects] => Array
                        (
                            [label] => $App->objects[]
                            [title] => The $App->objects[] Array
                            [selected] => 1
                        )

                    [data] => Array
                        (
                            [label] => $App->data[]
                            [title] => The $App->data[] Array
                            [selected] => 
                        )

                    [json] => Array
                        (
                            [label] => JSON API
                            [title] => Automatic JSON API
                            [selected] => 
                        )

                )

            [template] => test
            [visible-in-JSON] => 1
            [records] => Array
                (
                    [0] => Array
                        (
                            [0] => 4
                            [1] => 1
                            [2] => PHP
                            [3] => 27
                            [4] => 1
                            [5] => 0
                        )

                    [1] => Array
                        (
                            [0] => 5
                            [1] => 1
                            [2] => Javascript
                            [3] => 23
                            [4] => 1
                            [5] => 0
                        )

                    [2] => Array
                        (
                            [0] => 1
                            [1] => 1
                            [2] => C
                            [3] => 29
                            [4] => 1
                            [5] => 0
                        )

                    [3] => Array
                        (
                            [0] => 3
                            [1] => 1
                            [2] => C++
                            [3] => 20
                            [4] => 0
                            [5] => 1
                        )

                    [4] => Array
                        (
                            [0] => 9
                            [1] => 1
                            [2] => Python
                            [3] => 4
                            [4] => 0
                            [5] => 1
                        )

                    [5] => Array
                        (
                            [0] => 8
                            [1] => 1
                            [2] => Jai
                            [3] => 2
                            [4] => 1
                            [5] => 0
                        )

                    [6] => Array
                        (
                            [0] => 7
                            [1] => 1
                            [2] => Rust
                            [3] => 2
                            [4] => 0
                            [5] => 1
                        )

                )

        )

    [objects] => Array
        (
            [private-data] => Don't leak this in JSON
            [user_id] => 1
            [username] => Eric Harrison
            [password] => look@MySecr3tPASSword24!
        )

    [BASE_PATH] => /var/www/vhosts/php.programming-is-easy.com
    [BASE_URI] => https://php.programming-is-easy.com
    [db] => 
    [route_parts] => Array
        (
            [0] => test
            [1] => objects
        )

)

        )

    [options:PHPainfree:private] => Array
        (
            [ApplicationController] => App.php
            [BaseView] => app.php
            [DefaultRoute] => main
            [PublicFolder] => htdocs
            [AppFolder] => apps
            [TemplateFolder] => templates
            [LogicFolder] => includes
            [ControllerFolder] => Controllers
            [ImagesFolder] => images
            [CssFolder] => css
            [JsFolder] => js
            [DynamicFolder] => views
            [Database] => Array
                (
                )

            [RouteParameter] => route
        )

)

3. DebugExamples

DebugExamples = Please be aware that there are several $Painfree->debug() calls made in templates/debug.php to be used as examples. You should probably remove them.

4. $TestArray

$TestArray = Array
(
    [this_is] => a simple dummy array.
    [example] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

5. $PainfreeConfig

$PainfreeConfig = Array
(
    [ApplicationController] => App.php
    [BaseView] => app.php
    [DefaultRoute] => main
    [PublicFolder] => htdocs
    [AppFolder] => apps
    [TemplateFolder] => templates
    [LogicFolder] => includes
    [ControllerFolder] => Controllers
    [ImagesFolder] => images
    [CssFolder] => css
    [JsFolder] => js
    [DynamicFolder] => views
    [Database] => Array
        (
        )

    [RouteParameter] => route
)

6. EXAMPLE

EXAMPLE = You can basically pass anything to $Painfree->debug().