{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');
}
}
?>
<?phpfunction placeOrder() {//ここは注文完了のロジックif($success) {$this->redirect('/orders/thanks');} else {$this->redirect('/orders/confirm');}}?>
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'));
}
}
function placeOrder() {//Logic for finalizing order goes hereif($success) {$this->redirect(array('controller' => 'orders', 'action' => 'thanks'));} else {$this->redirect(array('controller' => 'orders', 'action' => 'confirm'));}}
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-17 | Lines: 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> | ||
| + | <?php | ||
| + | |||
| function placeOrder() { | function placeOrder() { | ||
| - | //Logic for finalizing order goes here | + | //ここは注文完了のロジック |
| if($success) { | if($success) { | ||
| - | $this->redirect(array('controller' => 'orders', 'action' => 'thanks')); | + | $this->redirect('/orders/thanks'); |
| } else { | } else { | ||
| - | $this->redirect(array('controller' => 'orders', 'action' => 'confirm')); | + | $this->redirect('/orders/confirm'); |
| } | } | ||
| } | } | ||
| + | ?> | ||
| </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> |
