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

codeigniter4 / CodeIgniter4 / 28685001618

03 Jul 2026 09:55PM UTC coverage: 88.703% (-0.001%) from 88.704%
28685001618

Pull #10372

github

web-flow
Merge 857504f28 into 5e7c6efbd
Pull Request #10372: fix: resolve global state pollution causing random-order test failures in HTTP suite

11 of 12 new or added lines in 3 files covered. (91.67%)

3 existing lines in 2 files now uncovered.

22275 of 25112 relevant lines covered (88.7%)

213.42 hits per line

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

94.12
/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
        $session->setFlashdata('_ci_old_input', [
5✔
96
            'get'  => service('superglobals')->getGetArray(),
5✔
97
            'post' => service('superglobals')->getPostArray(),
5✔
98
        ]);
5✔
99

100
        $this->withErrors();
5✔
101

102
        return $this;
5✔
103
    }
104

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

118
        if ($validation->getErrors() !== []) {
5✔
119
            service('session')->setFlashdata('_ci_validation_errors', $validation->getErrors());
1✔
120
        }
121

122
        return $this;
5✔
123
    }
124

125
    /**
126
     * Adds a key and message to the session as Flashdata.
127
     *
128
     * @param array|string $message
129
     *
130
     * @return $this
131
     */
132
    public function with(string $key, $message)
133
    {
134
        service('session')->setFlashdata($key, $message);
1✔
135

136
        return $this;
1✔
137
    }
138

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

151
        return $this;
2✔
152
    }
153

154
    /**
155
     * Copies any headers from the global Response instance
156
     * into this RedirectResponse. Useful when you've just
157
     * set a header be need to ensure its actually sent
158
     * with the redirect response.
159
     *
160
     * @return $this|RedirectResponse
161
     */
162
    public function withHeaders()
163
    {
164
        $source = service('response');
2✔
165

166
        foreach ($source->headers() as $name => $value) {
2✔
167
            if ($value instanceof Header) {
1✔
168
                $this->setHeader($name, $value->getValue());
1✔
169
            } else {
UNCOV
170
                foreach ($value as $header) {
×
UNCOV
171
                    $this->addHeader($name, $header->getValue());
×
172
                }
173
            }
174
        }
175

176
        // Ensure source response remains empty after copying to satisfy tests that expect no residual headers.
177
        foreach (array_keys($source->headers()) as $key) {
2✔
178
            $source->removeHeader($key);
1✔
179
        }
180

181
        return $this;
2✔
182
    }
183
}
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

© 2026 Coveralls, Inc