Views

Phenomine architecture is based on the MVC pattern. Of course, it's not practical to return entire HTML documents strings directly from your routes and controllers. Thankfully, views provide a convenient way to place all of our HTML in separate files.

Creating Views

Views separate your controller / application logic from your presentation logic and are stored in the res/views directory. When using Phenomine, view templates are usually written using the Latte templating language. A simple view might look something like this:

{* View stored in res/views/greeting.latte.php *}

<!DOCTYPE html>
<html>
<head>
    <title>Phenomine</title>
</head>
<body>
    <h1>Hello, {$name}!</h1>
</body>
</html>

Since this view is stored at res/views/greeting.latte.php, we may return it using the global view helper like so:

Route::get('/greeting', function () {
    return view('greeting', ['name' => 'Fahli']);
});

Nested View Directories

Views may also be nested within subdirectories of the res/views directory. "Dot" notation may be used to reference nested views. For example, if your view is stored at res/views/admin/profile.latte.php, you may return it from one of your application's routes / controllers like so:

return view('admin.profile', $data);

Passing Data To Views

As you saw in the previous examples, you may pass an array of data to views:

return view('greeting', ['name' => 'Fahli']);

When passing information in this manner, the data should be an array with key / value pairs. Inside your view, you can then access each value using its corresponding key, such as {$name}.

Caching

By default, Latte template views are compiled on demand. When a request is executed that renders a view, Phenomine will determine if a compiled version of the view exists. If the file exists, Phenomine will then determine if the uncompiled view has been modified more recently than the compiled view. If the compiled view either does not exist, or the uncompiled view has been modified, Phenomine will recompile the view. Compiled views are stored in the storage/framework/views directory. So, make sure this directory is writable by the web server.

However, the compiled view will take up a lot of storage. Phenomine provides a command that may be used to clear all cache of your views:

php phenomine view:clear