{JA} - 3.5.3.2.1 redirect

redirect(string $url, integer $status, boolean $exit)

もっともよく使用するフロー制御のメソッドは、redirect() です。このメソッドは、第1引数に CakePHP 流の相対 URL を指定します。 ユーザーが首尾よく注文したとき、領収書の画面にリダイレクトさせたいかもしれません。

<?php
	
function placeOrder() {

    //ここは注文完了のロジック

    if($success) {
        $this->redirect('/orders/thanks');
    } else {
        $this->redirect('/orders/confirm');
    }
}

?>
  1. <?php
  2. function placeOrder() {
  3. //ここは注文完了のロジック
  4. if($success) {
  5. $this->redirect('/orders/thanks');
  6. } else {
  7. $this->redirect('/orders/confirm');
  8. }
  9. }
  10. ?>

redirect() の第2引数は、リダイレクトの際の HTTP ステータスコードを指定します。リダイレクトの状況によっては、 301 (永久的な移転)や 303 (see other) を指定したくなるかもしれません。

このメソッドは、第3引数に false をセットしなければ、リダイレクト後に exit() が実行されます。

{EN} - 3.5.3.2.1 redirect

redirect(string $url, integer $status, boolean $exit)

The flow control method you’ll use most often is redirect(). This method takes its first parameter in the form of a CakePHP-relative URL. When a user has successfully placed an order, you might wish to redirect them to a receipt screen.

function placeOrder() {

    //Logic for finalizing order goes here

    if($success) {
        $this->redirect(array('controller' => 'orders', 'action' => 'thanks'));
    } else {
        $this->redirect(array('controller' => 'orders', 'action' => 'confirm'));
    }
}
  1. function placeOrder() {
  2. //Logic for finalizing order goes here
  3. if($success) {
  4. $this->redirect(array('controller' => 'orders', 'action' => 'thanks'));
  5. } else {
  6. $this->redirect(array('controller' => 'orders', 'action' => 'confirm'));
  7. }
  8. }

The second parameter of redirect() allows you to define an HTTP status code to accompany the redirect. You may want to use 301 (moved permanently) or 303 (see other), depending on the nature of the redirect.

The method will issue an exit() after the redirect unless you set the third parameter to false.

Differences

Lines: 1-17Lines: 1-21
 <title>redirect</title> <title>redirect</title>
 <p class="method"><code>redirect(string $url, integer $status, boolean $exit)</code></p> <p class="method"><code>redirect(string $url, integer $status, boolean $exit)</code></p>
-<p>The flow control method youll use most often is <code>redirect()</code>. This method takes its first parameter in the form of a CakePHP-relative URL. When a user has successfully placed an order, you might wish to redirect them to a receipt screen.</p> +<p>もっともよく使用するフロー制御のメソッドはredirect() です。このメソッドは、第1引数に CakePHP 流の相対 URL を指定します。 ユーザーが首尾よく注文したとき、領収書の画面にリダイレクトさせたいかもしれません。</p>
 <pre> <pre>
 +&lt;?php
 +
 function placeOrder() { function placeOrder() {
- //Logic for finalizing order goes here + //ここは注文完了のロジック
  if($success) {  if($success) {
- $this-&gt;redirect(array('controller' =&gt; 'orders', 'action' =&gt; 'thanks')); + $this-&gt;redirect('/orders/thanks');
  } else {  } else {
- $this-&gt;redirect(array('controller' =&gt; 'orders', 'action' =&gt; 'confirm')); + $this-&gt;redirect('/orders/confirm');
  }  }
 } }
 +?&gt;
 </pre> </pre>
-<p>The second parameter of <code>redirect()</code> allows you to define an HTTP status code to accompany the redirect. You may want to use 301 (moved permanently) or 303 (see other), depending on the nature of the redirect.</p>
<p>The method will issue an <code>exit()</code> after the redirect unless you set the third parameter to <code>false</code>.</p>
+<p>redirect() の第2引数は、リダイレクトの際の HTTP ステータスコードを指定します。リダイレクトの状況によっては、 301 (永久的な移転) 303 (see other) を指定したくなるかもしれません。</p>
<p>このメソッドは、第3引数に false をセットしなければ、リダイレクト後に exit() が実行されます。</p>