summaryrefslogtreecommitdiffstats
path: root/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/doc/book/index.html
blob: 64f69c9eb5d230aa137133e8c1c3ca9b50a61e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<div class="container">
  <div class="jumbotron">
    <h1>zend-mvc-plugin-prg</h1>
    
    <p>Post/Redirect/Get plugin for zend-mvc controllers.</p>

    <pre><code class="language-bash">$ composer require zendframework/zend-mvc-plugin-prg</code></pre>
  </div>
</div>

<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-6">
      <div class="panel panel-info">
        <div class="panel-heading">
          <h2 class="panel-title">Installation</h2>
        </div>

        <div class="panel-body">
          <p>
            Install via composer:
          </p>

          <pre><code class="lang-bash" data-trim>
$ composer require zendframework/zend-mvc-plugin-prg
          </code></pre>

          <p>
            If you are using the <a href="https://zendframework.github.io/zend-component-installer">zend-component-installer</a>,
            you're done!
          </p>

          <p>
            If not, you will need to add the component as a module to your
            application. Add the entry <code>'Zend\Mvc\Plugin\Prg'</code> to
            your list of modules in your application configuration (typically
            one of <code>config/application.config.php</code> or
            <code>config/modules.config.php</code>).
          </p>
        </div>
      </div>
    </div>

    <div class="col-xs-12 col-sm-6">
      <h2>Usage</h2>

      <p>
        When a user sends a POST request (e.g. after submitting a form), their
        browser will try to protect them from sending the POST again, breaking
        the back button, causing browser warnings and pop-ups, and sometimes
        reposting the form. Instead, when receiving a POST, we should store the
        data in a session container and redirect the user to a GET request.
      </p>

      <p>This plugin can be invoked with two arguments:</p>

      <ul>
        <li><code>$redirect</code>, a string containing the redirect location,
          which can either be a named route or a URL, based on the contents of
          the second parameter.</li>
        <li><code>$redirectToUrl</code>, a boolean that when set to
          <code>TRUE</code>, causes the first parameter to be treated as a URL
          instead of a route name (this is required when redirecting to a URL
          instead of a route). This argument defaults to <code>FALSE</code>.</li>
      </ul>

      <p>When no arguments are provided, the current matched route is used.</p>

      <h3>Example Usage</h3>

      <pre><code class="lang-php" data-trim>
// Pass in the route/url you want to redirect to after the POST
$prg = $this->prg('/user/register', true);

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
    // Returned a response to redirect us.
    return $prg;
}

if ($prg === false) {
    // This wasn't a POST request, but there were no params in the flash
    // messenger; this is probably the first time the form was loaded.
    return ['form' => $myForm];
}

// $prg is an array containing the POST params from the previous request
$form->setData($prg);

// ... your form processing code here
      </code></pre>
    </div>
  </div>
</div>