PHPainfree2 App Routing Example

PHPainfree2 App-based Routing

Using the new app-based routing system in PHPainfree2.

PHPainfree2 introduced a new routing mechanism called App-routing that allows you to keep code for specific routes together inside of folders. In the default routing system, the following locations are automatically used for controller and template loading:

  • Controller: {$App->BASE_PATH}/includes/Controllers/{$App->view}.php
  • View Template: {$App->BASE_PATH}/templates/views/{$App->view}.php

Limitations for Large Projects

On large projects, this quickly becomes unwieldy with hundreds of files stored in both includes/Controllers/ and templates/views/. To solve this, we introduced App-based Routing!

PHPainfree/
|-- includes/
|   |-- Controllers/
|   `-- test.php
`-- templates/
    |-- app.php
    |-- views/
	`-- test.php

App-based Routing

App-based Routing is a new routing concept that works in concert with the default routing mechanism but allows you to keep all controllers, functions, and view templates contained inside a unique directory.

PHPainfree2 introduced a new top-level project folder called apps/. When a request comes in to your server, the $App->route() function will look inside apps/ for a folder matching the $App->view taken from the first path name of the Request URL.

Request https://php.programming-is-easy.com/test/main
$App->view = "test"
App Folder {$App->BASE_PATH}/apps/test/
Controllers apps/test/controllers/
Main Controller apps/test/controllers/test.php
Templates apps/test/views/
Main Template apps/test/views/test.php
PHPainfree/
|-- apps/
|   `-- test/
|     |-- controllers/
|     |   `-- test.php
|     `-- views/
|         `-- test.php
|-- includes/
|   `-- Controllers/
`-- templates/
    `-- views/

PHPainfree Debugging Console [exec: 0.0028s]

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
    [app] => test
    [view] => test
    [id] => main
    [action] => 
    [output_file] => 
    [data] => Array
        (
            [sub-pages] => Array
                (
                    [main] => Array
                        (
                            [label] => App Routing
                            [title] => Using App Routing
                            [selected] => 1
                        )

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

                )

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

)

2. Painfree

Painfree = PHPainfree Object
(
    [Version] => 2.3.0
    [URI] => https://php.programming-is-easy.com/test
    [route] => test
    [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
    [app] => test
    [view] => test
    [id] => main
    [action] => 
    [output_file] => 
    [data] => Array
        (
            [sub-pages] => Array
                (
                    [main] => Array
                        (
                            [label] => App Routing
                            [title] => Using App Routing
                            [selected] => 1
                        )

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

                )

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

)

        )

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