Skip to content
StefansArya edited this page Sep 20, 2020 · 2 revisions

You may need to import this feature on top of your code.

use Scarlets\Route\Serve;

Handle HTML header and footer with Middleware

This is very useful for simplify your /resource/views/ because you don't need to write HTML header for every views manually.

It's better if you see the example of this usage.

From the example above you could see that this function is returning a function.

public static function html(){
    // Pending all output
    ob_start();
    
    return function($headerData = [], $footerData = []){
        $body = ob_get_clean(); // Get all output

        Serve::view("static.header", $headerData, true);
        Serve::raw($body);
        Serve::view("static.footer", $footerData, true);

        // Skip other routes
        Serve::end();
    }
}

The function will be saved by the framework and being executed after your script was finished. And to pass a parameter to that function you will need to use Middleware::pending(...args)

Route::middleware('html', function(){
    Route::get('/', function(){
        // Here you can send parameter to your pending function
        Middleware::pending($header, $footer);
    });
});

Serve views

The views folder are located on /resources/views/.
And you don't need to write .php when serving, and all dots . are converted into backslashes.

Serve::view($path, $values = [], $isMVW = false);

# Example
Route::get('/', function(){
    Serve::view('hello', ['message' => 'World']);
}, 'name:home');

When you're using Serve::view you can easily access the message parameter from target views like below.

<!-- hello.php -->
<p class="msg"> <?= $message ?> </p>

<!-- Obtaining `POST, GET` request -->
<p class="msg"> <?= $p['anything'] ?> </p>
<p class="msg"> <?= $q::post('anything') ?> </p>

Serve static page

The plate folder are located on /resources/plate/.
The usage is similar with Serve::view, but this one only have simple processing.

Serve::plate($path);

Serve HTTP status

When using this function, the framework will set the HTTP status and call the related router.

Serve::status($statusCode, $headerOnly = false);

Serve raw text

When using Console and the framework's build-in server, you need to use this instead of echo-ing your data.

Serve::raw($text);

End of serving

This maybe similar with die() function, but you could also set the http status code.

Serve::end($text = false, $statusCode = 200);

Redirect the request to another URL

The $data will be passed as html GET query, but if you want to use POST query you should set $httpCode to false.

Serve::redirect($url, $httpCode = 301, $data = false);

Serve minimal response for the views

If you're using a MVW frontend framework, you can obtain dynamic view only by set isMVW to true.

Serve::view($path, $values = [], $isMVW = true);

Send data to ScarletsFrame route handler

If you're using ScarletsFrame, you're able to pass data to the route handler's special data listener.

Serve::routeData([
    'key'=>'value'
]);