PHPainfree2 App Routing Example

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

A brief overview of $App->data[] and using it to communicate with templates and the automatic JSON API.

The $App->data[] array is a unique array freshly created for every request. The intended use of this array is to fill it with information from your controller so that this data is available for your view templates.

This array also has another secret weapon -- it's automatically used as output for a JSON-powered API available at every page request.

Security Warning!

ALL data in $App->data[] is exposed to any user making a request to your application either by providing application/json in the Request Headers Accept field, or by passing the query parameter ?json along with the request.

Because of this, you should never put any sensitive information in $App->data[] that you do NOT want visible to any user of your page.

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


<?php 
/**
 * test/controllers/test.php - Primary controller for test application.
 */
$App->data = array(
	'info'    => "You can put whatever information in this array that you want",
	'numbers' => array(4, 8, 15, 16, 23, 42),
	'is_user' => true,
	'records' => array(
		array(1, 'Test User', 'test@user.com'),
		array(2, 'Wife User', 'wife@user.com'),
		array(3, 'Son User', 'son@user.com'),
		array(4, 'Daugher User', 'daughter@user.com'),
	),
);

Template Usage

Using these data variables in your templates is extremely easy. Simply reference $App->data[] directly, or make local variable copies of either the entire array, or of individual array items.


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

$data = $App->data;
?>
<h2><?= $data['title']; ?></h2>
<ul>
	<?php 
		if ( $data['records'] ) {
			foreach ( $data['records'] as $row ) {
	?> 
	<li><em><?= $row[1]; ?></em></em>
	<?php 
			}
		} else {
	?> 
	<li><em>No records found...</em></em>
	<?php 
		}
	?>
<ul>

PHPainfree Debugging Console [exec: 0.0024s]

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/data
    [app] => test
    [view] => test
    [id] => data
    [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] => 
                        )

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

                    [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] => data
        )

)

2. Painfree

Painfree = PHPainfree Object
(
    [Version] => 2.3.0
    [URI] => https://php.programming-is-easy.com/test/data
    [route] => test/data
    [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/data
    [app] => test
    [view] => test
    [id] => data
    [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] => 
                        )

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

                    [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] => data
        )

)

        )

    [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().