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

codeigniter4 / CodeIgniter4 / 29271483819

13 Jul 2026 05:42PM UTC coverage: 88.71% (+0.006%) from 88.704%
29271483819

Pull #10374

github

web-flow
Merge 3e59f033c into 8c164d05c
Pull Request #10374: fix: resolve race conditions in Redis TTL and prevent cache test state leakage

22322 of 25163 relevant lines covered (88.71%)

213.82 hits per line

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

93.55
/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
    private function withErrors(): self
113
    {
114
        $validation = service('validation');
5✔
115

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

120
        return $this;
5✔
121
    }
122

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

134
        return $this;
1✔
135
    }
136

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

149
        return $this;
2✔
150
    }
151

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

172
        return $this;
2✔
173
    }
174
}
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