{JA} - 3.5.3.4.6 requestAction
requestAction(string $url, array $options)
この関数は、なんらかのロケーションを使用してコントローラのアクションを呼び出し、そのアクションの実行結果のデータを返します。$url に渡すのは CakePHP の相対 URL (/controllername/actionname/params) です。受信コントローラのアクションに特別なデータを渡すには、 $options 配列に追加します。
オプションに 'return' を渡すことで requestAction() を使用して、完全にレンダリングされたビューを取得することができます。: requestAction($url, array('return'));
キャッシュせずに使用すると、requestAction はパフォーマンスが悪くなります。まれにコントローラやモデルで使用することが適切な場合があります。
requestAction は(キャッシュされた)エレメントとともに使用されることが一番多いです。 - レンダリングする前にエレメント用のデータを取り出すために使用されるからです。レイアウト内で "latest comments" エレメントを設置する例を見てみましょう。初めにデータを返すコントローラの関数を作成する必要があります。
// controllers/comments_controller.php
class CommentsController extends AppController {
function latest() {
return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));
}
}
// controllers/comments_controller.phpclass CommentsController extends AppController {function latest() {return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));}}
上記の関数を呼び出す簡単なエレメントを作成するには:
// views/elements/latest_comments.ctp
$comments = $this->requestAction('/comments/latest');
foreach($comments as $comment) {
echo $comment['Comment']['title'];
}
// views/elements/latest_comments.ctp$comments = $this->requestAction('/comments/latest');foreach($comments as $comment) {echo $comment['Comment']['title'];}
どこにでもエレメントを設置することができます。出力を取得して使用します:
echo $this->element('latest_comments'); echo $this->element('latest_comments');
このように記述すると、エレメントがレンダリングされるときはいつでも、リクエストが生成されコントローラにデータが渡されます。データは処理されてから返されます。しかし注意しなればならないのは、不必要な処理を防ぐためにエレメントのキャッシュを使用することが重要です。エレメントの呼び出しを変更することによって次のようになります。:
echo $this->element('latest_comments', array('cache'=>'+1 hour')); echo $this->element('latest_comments', array('cache'=>'+1 hour'));
requestAction の呼び出しは、キャッシュされたエレメントのビューファイルが存在し有効である間は生成されなくなります。
{EN} - 3.5.3.4.6 requestAction
requestAction(string $url, array $options)
This function calls a controller's action from any location and returns data from the action. The $url passed is a CakePHP-relative URL (/controllername/actionname/params). To pass extra data to the receiving controller action add to the $options array.
You can use requestAction() to retrieve a fully rendered view by passing 'return' in the options: requestAction($url, array('return'));
If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model.
requestAction is best used in conjunction with (cached) elements – as a way to fetch data for an element before rendering. Let's use the example of putting a "latest comments" element in the layout. First we need to create a controller function that will return the data.
// controllers/comments_controller.php
class CommentsController extends AppController {
function latest() {
return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));
}
}
// controllers/comments_controller.phpclass CommentsController extends AppController {function latest() {return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));}}
If we now create a simple element to call that function:
// views/elements/latest_comments.ctp
$comments = $this->requestAction('/comments/latest');
foreach($comments as $comment) {
echo $comment['Comment']['title'];
}
// views/elements/latest_comments.ctp$comments = $this->requestAction('/comments/latest');foreach($comments as $comment) {echo $comment['Comment']['title'];}
We can then place that element anywhere at all to get the output using:
echo $this->element('latest_comments'); echo $this->element('latest_comments');
Written in this way, whenever the element is rendered, a request will be made to the controller to get the data, the data will be processed, and returned. However in accordance with the warning above it's best to make use of element caching to prevent needless processing. By modifying the call to element to look like this:
echo $this->element('latest_comments', array('cache'=>'+1 hour')); echo $this->element('latest_comments', array('cache'=>'+1 hour'));
The requestAction call will not be made while the cached element view file exists and is valid.
In addition, requestAction now takes array based cake style urls:
echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('return')); echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('return'));
This allows the requestAction call to bypass the usage of Router::url which can increase performance. The url based arrays are the same as the ones that HtmlHelper::link uses with one difference. If you are using named params in your url then the requestAction url array must wrap the named params in the key 'named'. This is because requestAction only merges the named args array into the Controller::params member array and does not place the named args in the key 'named'.
echo $this->requestAction('/articles/featured/limit:3'); echo $this->requestAction('/articles/featured/limit:3');
This as an array in the requestAction would then be:
echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured', 'named' => array('limit' => 3))); echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured', 'named' => array('limit' => 3)));
Unlike other places where array urls are analogous to string urls, requestAction treats them differently.
When using an array url in conjunction with requestAction() you must specify all parameters that you will need in the requested action. This includes parameters like $this->data and $this->params['form']
Differences
| Lines: 1-36 | Lines: 1-28 | ||
| <title>requestAction</title> | <title>requestAction</title> | ||
| <p class="method"><code>requestAction(string $url, array $options)</code></p> | <p class="method"><code>requestAction(string $url, array $options)</code></p> | ||
| - | <p>This function calls a controller's action from any location and returns data from the action. The <code>$url</code> passed is a CakePHP-relative URL (<kbd>/controllername/actionname/params</kbd>). To pass extra data to the receiving controller action add to the $options array.</p> <p class="note">You can use <code>requestAction()</code> to retrieve a fully rendered view by passing 'return' in the options: <code>requestAction($url, array('return'));</code></p> <p class="warning">If used without caching <code>requestAction</code> can lead to poor performance. It is rarely appropriate to use in a controller or model.</p> <p><code>requestAction</code> is best used in conjunction with (cached) elements as a way to fetch data for an element before rendering. Let's use the example of putting a "latest comments" element in the layout. First we need to create a controller function that will return the data.</p> |
+ | <p>この関数は、なんらかのロケーションを使用してコントローラのアクションを呼び出し、そのアクションの実行結果のデータを返します。<code>$url</code> に渡すのは CakePHP の相対 URL (<kbd>/controllername/actionname/params</kbd>) です。受信コントローラのアクションに特別なデータを渡すには、 $options 配列に追加します。</p> <p class="note">オプションに 'return' を渡すことで <code>requestAction()</code> を使用して、完全にレンダリングされたビューを取得することができます。: <code>requestAction($url, array('return'));</code></p> <p class="warning">キャッシュせずに使用すると、<code>requestAction</code> はパフォーマンスが悪くなります。まれにコントローラやモデルで使用することが適切な場合があります。</p> <p><code>requestAction</code> は(キャッシュされた)エレメントとともに使用されることが一番多いです。 - レンリングする前にエレメント用のデータを取り出すために使用されるからです。レイアウト内で "latest comments" エレメントを設置する例を見てみましょう。初めにデータを返すコントローラの関数を作成する必要があります。</p> |
| <pre> | <pre> | ||
| // controllers/comments_controller.php | // controllers/comments_controller.php | ||
| class CommentsController extends AppController { | class CommentsController extends AppController { | ||
| function latest() { | function latest() { | ||
| return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10)); | return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10)); | ||
| } | } | ||
| } | } | ||
| </pre> | </pre> | ||
| - | <p>If we now create a simple element to call that function:</p> | + | <p>上記の関数を呼び出す簡単なエレメントを作成するには:</p> |
| <pre> | <pre> | ||
| // views/elements/latest_comments.ctp | // views/elements/latest_comments.ctp | ||
| $comments = $this->requestAction('/comments/latest'); | $comments = $this->requestAction('/comments/latest'); | ||
| foreach($comments as $comment) { | foreach($comments as $comment) { | ||
| echo $comment['Comment']['title']; | echo $comment['Comment']['title']; | ||
| } | } | ||
| </pre> | </pre> | ||
| - | <p>We can then place that element anywhere at all to get the output using:</p> | + | <p>どこにでもエレメントを設置することができます。出力を取得して使用します:</p> |
| <pre>echo $this->element('latest_comments');</pre> | <pre>echo $this->element('latest_comments');</pre> | ||
| - | <p>Written in this way, whenever the element is rendered, a request will be made to the controller to get the data, the data will be processed, and returned. However in accordance with the warning above it's best to make use of element caching to prevent needless processing. By modifying the call to element to look like this:</p> | + | <p>このように記述すると、エレメントがレンダリングされるときはいつでも、リクエストが生成されコントローラにデータが渡されます。データは処理されてから返されます。しかし注意しなればならないのは、不必要な処理を防ぐためにエレメントのキャッシュを使用することが重要です。エレメントの呼び出しを変更することによって次のようになります。:</p> |
| <pre>echo $this->element('latest_comments', array('cache'=>'+1 hour'));</pre> | <pre>echo $this->element('latest_comments', array('cache'=>'+1 hour'));</pre> | ||
| - | <p>The <code>requestAction</code> call will not be made while the cached element view file exists and is valid.</p> <p>In addition, requestAction now takes array based cake style urls:</p> <pre>echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('return'));</pre> <p>This allows the requestAction call to bypass the usage of Router::url which can increase performance. The url based arrays are the same as the ones that HtmlHelper::link uses with one difference. If you are using named params in your url then the requestAction url array must wrap the named params in the key 'named'. This is because requestAction only merges the named args array into the Controller::params member array and does not place the named args in the key 'named'.</p> <pre>echo $this->requestAction('/articles/featured/limit:3');</pre> <p>This as an array in the requestAction would then be:</p> <pre>echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured', 'named' => array('limit' => 3)));</pre> <p class="note">Unlike other places where array urls are analogous to string urls, requestAction treats them differently.</p> <p>When using an array url in conjunction with requestAction() you must specify <strong>all</strong> parameters that you will need in the requested action. This includes parameters like <code>$this->data</code> and <code>$this->params['form']</code> |
+ | <p><code>requestAction</code> の呼び出しは、キャッシュされたエレメントのビューファイルが存在し有効である間は生成されなくなります。</p> |
