xref: /webtrees/app/Http/ViewResponseTrait.php (revision 982e6c5587f167a514f0634a1e3d44fc2835f003)
1606471d5SGreg Roach<?php
23976b470SGreg Roach
3606471d5SGreg Roach/**
4606471d5SGreg Roach * webtrees: online genealogy
5606471d5SGreg Roach * Copyright (C) 2019 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
15606471d5SGreg Roach * along with this program. If not, see <http://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;
243976b470SGreg Roach
25606471d5SGreg Roachuse function response;
26606471d5SGreg Roachuse function view;
27606471d5SGreg Roach
28606471d5SGreg Roach/**
29606471d5SGreg Roach * Allows a page fragment to be embedded in a page layout and converted to an HTTP response.
30606471d5SGreg Roach * Used by controllers, request-handlers, modules, etc.
31606471d5SGreg Roach */
32606471d5SGreg Roachtrait ViewResponseTrait
33606471d5SGreg Roach{
34*982e6c55SGreg Roach    /** @var string */
351c69478cSGreg Roach    protected $layout = 'layouts/default';
361c69478cSGreg Roach
37606471d5SGreg Roach    /**
38606471d5SGreg Roach     * @param string  $view_name
39606471d5SGreg Roach     * @param mixed[] $view_data
40606471d5SGreg Roach     * @param int     $status
41606471d5SGreg Roach     *
42606471d5SGreg Roach     * @return ResponseInterface
43606471d5SGreg Roach     */
44606471d5SGreg Roach    protected function viewResponse(string $view_name, array $view_data, $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);
51606471d5SGreg Roach
52606471d5SGreg Roach        // Most pages use the default layout.  Other built-in layouts include admin and ajax.
531c69478cSGreg Roach        $layout = $this->layout;
54606471d5SGreg Roach
55606471d5SGreg Roach        // Insert the view into the layout
56606471d5SGreg Roach        $html = view($layout, $layout_data);
57606471d5SGreg Roach
58606471d5SGreg Roach        return response($html, $status);
59606471d5SGreg Roach    }
60606471d5SGreg Roach}
61