• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 14438828772

14 Apr 2025 06:19AM UTC coverage: 84.439% (+0.001%) from 84.438%
14438828772

Pull #9524

github

web-flow
Merge 15b3540ca into e4ff0530c
Pull Request #9524: refactor: fix nullCoalesce.variable errors

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

20815 of 24651 relevant lines covered (84.44%)

191.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/system/HTTP/RedirectResponse.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\HTTP;
15

16
use CodeIgniter\Cookie\CookieStore;
17
use CodeIgniter\HTTP\Exceptions\HTTPException;
18

19
/**
20
 * Handle a redirect response
21
 *
22
 * @see \CodeIgniter\HTTP\RedirectResponseTest
23
 */
24
class RedirectResponse extends Response
25
{
26
    /**
27
     * Sets the URI to redirect to and, optionally, the HTTP status code to use.
28
     * If no code is provided it will be automatically determined.
29
     *
30
     * @param string   $uri  The URI path (relative to baseURL) to redirect to
31
     * @param int|null $code HTTP status code
32
     *
33
     * @return $this
34
     */
35
    public function to(string $uri, ?int $code = null, string $method = 'auto')
36
    {
37
        // If it appears to be a relative URL, then convert to full URL
38
        // for better security.
39
        if (! str_starts_with($uri, 'http')) {
5✔
40
            $uri = site_url($uri);
4✔
41
        }
42

43
        return $this->redirect($uri, $method, $code);
5✔
44
    }
45

46
    /**
47
     * Sets the URI to redirect to but as a reverse-routed or named route
48
     * instead of a raw URI.
49
     *
50
     * @param string $route Route name or Controller::method
51
     *
52
     * @return $this
53
     *
54
     * @throws HTTPException
55
     */
56
    public function route(string $route, array $params = [], ?int $code = null, string $method = 'auto')
57
    {
58
        $namedRoute = $route;
8✔
59

60
        $route = service('routes')->reverseRoute($route, ...$params);
8✔
61

62
        if (! $route) {
8✔
63
            throw HTTPException::forInvalidRedirectRoute($namedRoute);
2✔
64
        }
65

66
        return $this->redirect(site_url($route), $method, $code);
6✔
67
    }
68

69
    /**
70
     * Helper function to return to previous page.
71
     *
72
     * Example:
73
     *  return redirect()->back();
74
     *
75
     * @return $this
76
     */
77
    public function back(?int $code = null, string $method = 'auto')
78
    {
79
        service('session');
2✔
80

81
        return $this->redirect(previous_url(), $method, $code);
2✔
82
    }
83

84
    /**
85
     * Sets the current $_GET and $_POST arrays in the session.
86
     * This also saves the validation errors.
87
     *
88
     * It will then be available via the 'old()' helper function.
89
     *
90
     * @return $this
91
     */
92
    public function withInput()
93
    {
94
        $session = service('session');
5✔
95

96
        // @phpstan-ignore-next-line
97
        $get = $_GET ?? [];
5✔
98
        // @phpstan-ignore-next-line
99
        $post = $_POST ?? [];
5✔
100

101
        $session->setFlashdata('_ci_old_input', [
5✔
102
            'get'  => $get,
5✔
103
            'post' => $post,
5✔
104
        ]);
5✔
105

106
        $this->withErrors();
5✔
107

108
        return $this;
5✔
109
    }
110

111
    /**
112
     * Sets validation errors in the session.
113
     *
114
     * If the validation has any errors, transmit those back
115
     * so they can be displayed when the validation is handled
116
     * within a method different than displaying the form.
117
     *
118
     * @return $this
119
     */
120
    private function withErrors(): self
121
    {
122
        $validation = service('validation');
5✔
123

124
        if ($validation->getErrors() !== []) {
5✔
125
            service('session')->setFlashdata('_ci_validation_errors', $validation->getErrors());
1✔
126
        }
127

128
        return $this;
5✔
129
    }
130

131
    /**
132
     * Adds a key and message to the session as Flashdata.
133
     *
134
     * @param array|string $message
135
     *
136
     * @return $this
137
     */
138
    public function with(string $key, $message)
139
    {
140
        service('session')->setFlashdata($key, $message);
1✔
141

142
        return $this;
1✔
143
    }
144

145
    /**
146
     * Copies any cookies from the global Response instance
147
     * into this RedirectResponse. Useful when you've just
148
     * set a cookie but need ensure that's actually sent
149
     * with the response instead of lost.
150
     *
151
     * @return $this|RedirectResponse
152
     */
153
    public function withCookies()
154
    {
155
        $this->cookieStore = new CookieStore(service('response')->getCookies());
2✔
156

157
        return $this;
2✔
158
    }
159

160
    /**
161
     * Copies any headers from the global Response instance
162
     * into this RedirectResponse. Useful when you've just
163
     * set a header be need to ensure its actually sent
164
     * with the redirect response.
165
     *
166
     * @return $this|RedirectResponse
167
     */
168
    public function withHeaders()
169
    {
170
        foreach (service('response')->headers() as $name => $value) {
2✔
171
            if ($value instanceof Header) {
1✔
172
                $this->setHeader($name, $value->getValue());
1✔
173
            } else {
UNCOV
174
                foreach ($value as $header) {
×
UNCOV
175
                    $this->addHeader($name, $header->getValue());
×
176
                }
177
            }
178
        }
179

180
        return $this;
2✔
181
    }
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc