This site is built with Viewi itself. It is experimental and still in development. If you see any bugs please do not hesitate and open an issue or DM me on Twitter.

Caveats

Basics

Since Viewi is working with a limited subset of PHP and converts your code into the javascript to run it in the browser also, there are some differences and caveats that you should know about. I will list some of them here:

Converting boolean to string

In PHP you will get '' or '1', in javascript: 'false' 'true'. To avoid it use the conditional (ternary) operator:

$isMember ? 'true' : 'false'

Arrays

In javascript an array is an object and therefore is a reference type. In PHP, an array is a value type. Be careful passing an array as a parameter into the method or function. Javascript doesn’t have associative arrays, so it’s better to avoid it.

Async Programming

Since Javascript is executed in a single threaded async environment, there is one concept that you should be familiar with: callbacks and async flow.

// step 1: this will be executed first, on both: back and front sides
$this->http->post("/api/url", $model)
    ->then(
        function ($data) {
            // step 2: this will be executed as follow:
            // on backend side: after step 1
            // on front side: after the request comes back with the response
            // execution will not pause. Everything after "post"
            // and "then" will be executed right away (step 3)
            // without waiting for the response
        },
        function ($error) {
        }
    );
// step 3
// on frontend side - will be executed after step 1
// on backend - after step 2

Not everything can be converted into the javascript

For example:

There are no attributes. (Viewi cuts it, for now) There are no named arguments.

There are no types, reflection won’t work.

You can’t use functionality that is meant for the server side only, like working with files, etc. Keep your frontend logic isolated from the rest of the application. Consider it as a frontend source code that lives in the browser. Even though it still can be used on the backend side, (what a pleasure, I don’t have to write the code again for server side rendering).

There are no namespaces. Classes with the same name and different namespaces will not work.