xref: /webtrees/app/Http/ViewResponseTrait.php (revision 09482a558a7989d76059e7f9911605cf836b77ba)
1606471d5SGreg Roach<?php
23976b470SGreg Roach
3606471d5SGreg Roach/**
4606471d5SGreg Roach * webtrees: online genealogy
5a612ab9fSGreg Roach * Copyright (C) 2021 webtrees development team
6606471d5SGreg Roach * This program is free software: you can redistribute it and/or modify
7606471d5SGreg Roach * it under the terms of the GNU General Public License as published by
8606471d5SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9606471d5SGreg Roach * (at your option) any later version.
10606471d5SGreg Roach * This program is distributed in the hope that it will be useful,
11606471d5SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12606471d5SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13606471d5SGreg Roach * GNU General Public License for more details.
14606471d5SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16606471d5SGreg Roach */
17fcfa147eSGreg Roach
18606471d5SGreg Roachdeclare(strict_types=1);
19606471d5SGreg Roach
20606471d5SGreg Roachnamespace Fisharebest\Webtrees\Http;
21606471d5SGreg Roach
22606471d5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23606471d5SGreg Roachuse Psr\Http\Message\ResponseInterface;
24ef5d23f1SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
253976b470SGreg Roach
26606471d5SGreg Roachuse function response;
27606471d5SGreg Roachuse function view;
28606471d5SGreg Roach
29606471d5SGreg Roach/**
30606471d5SGreg Roach * Allows a page fragment to be embedded in a page layout and converted to an HTTP response.
31606471d5SGreg Roach * Used by controllers, request-handlers, modules, etc.
32606471d5SGreg Roach */
33606471d5SGreg Roachtrait ViewResponseTrait
34606471d5SGreg Roach{
35c4943cffSGreg Roach    protected string $layout = 'layouts/default';
361c69478cSGreg Roach
37606471d5SGreg Roach    /**
38606471d5SGreg Roach     * @param string       $view_name
39*09482a55SGreg Roach     * @param array<mixed> $view_data
40606471d5SGreg Roach     * @param int          $status
41606471d5SGreg Roach     *
42606471d5SGreg Roach     * @return ResponseInterface
43606471d5SGreg Roach     */
44970b132cSGreg Roach    protected function viewResponse(string $view_name, array $view_data, int $status = StatusCodeInterface::STATUS_OK): ResponseInterface
45606471d5SGreg Roach    {
46606471d5SGreg Roach        // Make the view's data available to the layout.
47606471d5SGreg Roach        $layout_data = $view_data;
48606471d5SGreg Roach
49606471d5SGreg Roach        // Render the view
50606471d5SGreg Roach        $layout_data['content'] = view($view_name, $view_data);
51ef5d23f1SGreg Roach        $layout_data['request'] = app(ServerRequestInterface::class);
52606471d5SGreg Roach
53606471d5SGreg Roach        // Insert the view into the layout
54a612ab9fSGreg Roach        $html = view($this->layout, $layout_data);
55606471d5SGreg Roach
56606471d5SGreg Roach        return response($html, $status);
57606471d5SGreg Roach    }
58606471d5SGreg Roach}
59