xref: /webtrees/app/Http/ViewResponseTrait.php (revision 1c69478c193d11e32dda50a8b8c200fc78ea1035)
1606471d5SGreg Roach<?php
2606471d5SGreg Roach/**
3606471d5SGreg Roach * webtrees: online genealogy
4606471d5SGreg Roach * Copyright (C) 2019 webtrees development team
5606471d5SGreg Roach * This program is free software: you can redistribute it and/or modify
6606471d5SGreg Roach * it under the terms of the GNU General Public License as published by
7606471d5SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8606471d5SGreg Roach * (at your option) any later version.
9606471d5SGreg Roach * This program is distributed in the hope that it will be useful,
10606471d5SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11606471d5SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12606471d5SGreg Roach * GNU General Public License for more details.
13606471d5SGreg Roach * You should have received a copy of the GNU General Public License
14606471d5SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15606471d5SGreg Roach */
16606471d5SGreg Roachdeclare(strict_types=1);
17606471d5SGreg Roach
18606471d5SGreg Roachnamespace Fisharebest\Webtrees\Http;
19606471d5SGreg Roach
20606471d5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
21606471d5SGreg Roachuse Psr\Http\Message\ResponseInterface;
22606471d5SGreg Roachuse function response;
23606471d5SGreg Roachuse function view;
24606471d5SGreg Roach
25606471d5SGreg Roach/**
26606471d5SGreg Roach * Allows a page fragment to be embedded in a page layout and converted to an HTTP response.
27606471d5SGreg Roach * Used by controllers, request-handlers, modules, etc.
28606471d5SGreg Roach */
29606471d5SGreg Roachtrait ViewResponseTrait
30606471d5SGreg Roach{
31*1c69478cSGreg Roach    protected $layout = 'layouts/default';
32*1c69478cSGreg Roach
33606471d5SGreg Roach    /**
34606471d5SGreg Roach     * @param string  $view_name
35606471d5SGreg Roach     * @param mixed[] $view_data
36606471d5SGreg Roach     * @param int     $status
37606471d5SGreg Roach     *
38606471d5SGreg Roach     * @return ResponseInterface
39606471d5SGreg Roach     */
40606471d5SGreg Roach    protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface
41606471d5SGreg Roach    {
42606471d5SGreg Roach        // Make the view's data available to the layout.
43606471d5SGreg Roach        $layout_data = $view_data;
44606471d5SGreg Roach
45606471d5SGreg Roach        // Render the view
46606471d5SGreg Roach        $layout_data['content'] = view($view_name, $view_data);
47606471d5SGreg Roach
48606471d5SGreg Roach        // Most pages use the default layout.  Other built-in layouts include admin and ajax.
49*1c69478cSGreg Roach        $layout = $this->layout;
50606471d5SGreg Roach
51606471d5SGreg Roach        // Insert the view into the layout
52606471d5SGreg Roach        $html = view($layout, $layout_data);
53606471d5SGreg Roach
54606471d5SGreg Roach        return response($html, $status);
55606471d5SGreg Roach    }
56606471d5SGreg Roach}
57