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

codeigniter4 / CodeIgniter4 / 11878724576

17 Nov 2024 12:12PM UTC coverage: 84.428% (+0.001%) from 84.427%
11878724576

push

github

web-flow
fix: code issues after merging develop (#9284)

* cs-fix

* fix services call

* apply rector rules

3 of 3 new or added lines in 2 files covered. (100.0%)

361 existing lines in 20 files now uncovered.

20597 of 24396 relevant lines covered (84.43%)

189.64 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

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'  => $_GET ?? [],
5✔
97
            'post' => $_POST ?? [],
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
            $session = service('session');
1✔
120
            $session->setFlashdata('_ci_validation_errors', $validation->getErrors());
1✔
121
        }
122

123
        return $this;
5✔
124
    }
125

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

137
        return $this;
1✔
138
    }
139

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

152
        return $this;
2✔
153
    }
154

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

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