Skip to content

Latest commit

 

History

History
65 lines (43 loc) · 1.66 KB

File metadata and controls

65 lines (43 loc) · 1.66 KB

Route format

Specifiyng the format of a route will determine the extention of the output file during the build.

By default, all routes are treated as HTML and therefore dumped as .html files.

Phpillip rely on the Response Content-Type header to determine the format of a route.

To control the output format of a route, you just need to configure the Response with the desired content type.

There are 3 ways to do it:

Do it, literally:

function () {
    // Will output a '.txt' file
    return new Response('Hello', 200, ['Content-Type' => 'text/plain']);
}

function () {
    // Will output a '.json' file
    return new JsonResponse($data);
}

Set the _format attribute of the Request

In Symfony, Response content type is by default determined by the format of the Request.

So you can define the output format of a route by setting the Request attribute _format. Phpillip will provide you with a format method on the route to do just that:

// Will output a '.txt' file
$app->get('/hello')->format('txt');

Note: Remember that the Response expects a Mime-Type (e.g text/html) but the Request expects a format (e.g. html).

Finally you can set the format of the route by explicitly naming the file in the url pattern:

// Will output a '.json' file
$app->get('/hello.json');

File name

Additionaly, you can choose a custom name for the output file.

With the setFileName method:

// Will output a '404.html' file
$app->get('/404')->setFilename('404');

Or directly in url pattern:

// Will output a 'feed.rss' file
$app->get('/feed.rss');

Note: The default output file name is index.