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

codeigniter4 / CodeIgniter4 / 8586246081

07 Apr 2024 04:43AM UTC coverage: 86.602% (+1.0%) from 85.607%
8586246081

push

github

web-flow
Merge pull request #8720 from codeigniter4/4.5

Merge 4.5 into develop

2273 of 2603 new or added lines in 188 files covered. (87.32%)

53 existing lines in 18 files now uncovered.

19947 of 23033 relevant lines covered (86.6%)

189.35 hits per line

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

93.75
/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
use Config\Services;
19

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

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

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

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

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

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

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

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

85
    /**
86
     * Sets the current $_GET and $_POST arrays in the session.
87
     * This also saves the validation errors.
88
     *
89
     * It will then be available via the 'old()' helper function.
90
     *
91
     * @return $this
92
     */
93
    public function withInput()
94
    {
95
        $session = service('session');
5✔
96
        $session->setFlashdata('_ci_old_input', [
5✔
97
            'get'  => $_GET ?? [],
5✔
98
            'post' => $_POST ?? [],
5✔
99
        ]);
5✔
100

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

103
        return $this;
5✔
104
    }
105

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

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

124
        return $this;
5✔
125
    }
126

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

138
        return $this;
1✔
139
    }
140

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

153
        return $this;
2✔
154
    }
155

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

176
        return $this;
2✔
177
    }
178
}
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