Translations Español

In contact with StratusPHP.

In contact with StratusPHP. In this article we will show you a tutorial with the necessary steps to apply what we published about StratusPHP previously first video we published about StratusPHP. We recommend to watch the first undersigned video if you didn´t yet.

As we said previously, this project is still developing and we will be publishing articles about its new chracteristics, changes, news, etc. as it goes on. This publication will make easier to the users to create applications of proof with StratusPHP and the user could try the new characteristics we will be publishing gradually. Something important we have to specify is that StratusPHP will have different ways of work. That means that it can have a lot of options of configuration and those options need to be specified according to the application needs. StratusPHP could work with HTTP and with WebSockets too and that is why the option taked depend of the way of work . We are focus just on the working by HTTP.

Don´t worry if you have some queries about it. We know that there are a lot of things we have to explain but we will do it when the time comes, because it is necessary a practical approach in this publication.

1. Installing StratusPHP.

The StratusPHP installation is realized by Composer this way:

$ composer require thenlabs/stratus-php dev-master

You have to take into account that StratusPHP needs PHP 7.3 or superior.

2. Creating the application class.

We create the application class once the app has finalized that it has given back the HTML code page and created the next achive with that purpose:

<?php
// src/App.php

namespace ThenLabs\StratusPHP\Demo;

use ThenLabs\StratusPHP\AbstractApp;

class App extends AbstractApp
{
    public function getView(): string
    {
        return <<<HTML
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>Document</title>
            </head>
            <body>
                <input type="" name="">
                <label></label>
                <button> to greet</button>
            </body>
            </html>
        HTML;
    }
}

You can notice that in our case we are using the space names ThenLabs\StratusPHP\Demo. As you can supose the application structure will depend of its own necessaries and that is why it is not absolute. In our case, we have to create the names space. You need to add the next archive for this composer.json:

{
    // ...
    "autoload": {
        "psr-4": {
            "ThenLabs\\StratusPHP\\Demo\\": "src/"
        }
    }
}

Once it has been realized we have to execute the command :

$ composer dump-autoload

As you could notice, our class App inherit from the class ThenLabs\StratusPHP\AbstractApp and it contain the method getView(): string that give back the HTML code of thepage.

3. Introduction to the controllers.

The web applications has controllers as you know.

In the case of the apps made withn StratusPHP will need two controllers to work. One of them will give back the HTML of the page, and the other one will process the asynchronous calls.

It is important to say that the platform of creation is totally this controllers are not within StratusPHP reach. It guarantee that it is compatible with other frameworks like Symfony, Laravel, etc. There are no doubt about this one of the best StratusPHP characteristics.

4. Creating the load controller.

For the load controller we will create the next archive:

<?php
// public/index.php

require_once __DIR__.'/../vendor/autoload.php';

use ThenLabs\StratusPHP\Demo\App;
use function Opis\Closure\serialize as s;

$app = new App('/controller.php');

$input = $app->querySelector('input');
$label = $app->querySelector('label');
$button = $app->querySelector('button');

$button->onClick(function () use ($input, $label) {
    $label->innerHTML = "Hola {$input->value}";
});

session_start();
$_SESSION['app'] = s($app);

echo $app;

You can see that in the undersigned archive we define our application behavior.

5. Creating the processing controller.

In the case of our processing controller we do it next way:

<?php
// public/controller.php

require_once __DIR__.'/../vendor/autoload.php';

use ThenLabs\StratusPHP\StratusRequest;
use function Opis\Closure\{serialize as s, unserialize as u};

session_start();

$app = u($_SESSION['app']);
$request = StratusRequest::createFromJson($_REQUEST['stratus_request']);

$result = $app->run($request);

if ($result->isSuccessful()) {
    $_SESSION['app'] = s($app);
}

die();

6. Running the application.

To run demo:

$ php -S localhost:8000 -t public

Once we had visited the URL where our application have been served we can check that it works as we expect.

7. Conclusions.

At this point, the users will be ready to get contact with StratusPHP. They could try the new characteristics we will show soon with a similar project. We want to stress the StratusPHP abilities to modify the DOM elements of the page without reload the page. You can check that you could whrite or read about any dato. However we want you to remenber that StratusPHP is still developing and we will try a lot of things.

In the archive demo1.zip you could dowload an useful installation that we have reaized.

we thank you if you share this publication for another people to know about StratusPHP.

Publication Date: 2020-06-21

If you want to know by email about our next publications and news in general, subscribe to our newsletter.

Social Networks

Also subscribe to our social networks.
Also through these channels we will share our news.

Contact

To contact us you can write to:
thenlabs@gmail.com