-
-
Notifications
You must be signed in to change notification settings - Fork 1
Serve
You may need to import this feature on top of your code.
use Scarlets\Route\Serve;
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);
});
});
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>
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);
When using this function, the framework will set the HTTP status and call the related router.
Serve::status($statusCode, $headerOnly = false);
When using Console and the framework's build-in server, you need to use this instead of echo-ing your data.
Serve::raw($text);
This maybe similar with die()
function, but you could also set the http status code.
Serve::end($text = false, $statusCode = 200);
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);
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);
If you're using ScarletsFrame, you're able to pass data to the route handler's special data listener.
Serve::routeData([
'key'=>'value'
]);