PHPainfree2 App Routing Example

The Automatic JSON API powered by $App->data[]

Using the Automatic JSON API.

Automatic APIs for Free!

To streamline API development and promote code reusability in your web applications, the PHPainfree2 routing system provides you with a JSON API at each and every unique URL and endpoint.

Rather than writing a separate API to serve any of your users that need access to an API, PHPainfree2 turns every page and route into an API automatically using the $App->data[] array.

<

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 $App->data[] and $App->objects[] for more details.

Accessing the API

By default, the JSON API for any route/endpoint is automatically served whenever a request is made with an Accept request header set to application/json.

The $App->route() method will also serve the API for any request submitted with ANY query parameter or POST body field with the name json.


<?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'),
	),
);

See it in action

Take a look at this current page with the json query parameter turned on: /test/json/?json=true

API Design

How you design your API is ultimate up to you. You can do whatever you need in any way to meet the needs of your application use-cases. Just be aware that any time you use $App->data, you are silently creating a Publicly-accessible API for any user that has access to any given page.

The Safe Template Data Alternative

If you want to send data from your controller to your templates without worrying about exposing sensitive information to your users, the $App->objects[] array provides all the same options as $App->data[] but without any magical array to JSON conversion happening behind the scenes.

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

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

                )

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

)

2. Painfree

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

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

                )

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

)

        )

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