popRequest() public static method

Pops a request off of the request stack. Used when doing requestAction
See also: Router::setRequestInfo()
See also: Object::requestAction()
public static popRequest ( ) : CakeRequest
return CakeRequest The request removed from the stack.
Example #1
0
 /**
  * Test that Router::url() uses the first request
  */
 public function testUrlWithRequestAction()
 {
     $firstRequest = new CakeRequest('/posts/index');
     $firstRequest->addParams(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'))->addPaths(array('base' => ''));
     $secondRequest = new CakeRequest('/posts/index');
     $secondRequest->addParams(array('requested' => 1, 'plugin' => null, 'controller' => 'comments', 'action' => 'listing'))->addPaths(array('base' => ''));
     Router::setRequestInfo($firstRequest);
     Router::setRequestInfo($secondRequest);
     $result = Router::url(array('base' => false));
     $this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.');
     Router::popRequest();
     $result = Router::url(array('base' => false));
     $this->assertEquals('/posts', $result, 'with second requests, the last should win.');
 }
Example #2
0
 /**
  * Calls a controller's method from any location. Can be used to connect controllers together
  * or tie plugins into a main application. requestAction can be used to return rendered views
  * or fetch the return value from controller actions.
  *
  * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  * URL. You should use URL formats that are compatible with Router::reverse()
  *
  * #### Passing POST and GET data
  *
  * POST and GET data can be simulated in requestAction. Use `$extra['url']` for
  * GET data. The `$extra['data']` parameter allows POST data simulation.
  *
  * @param string|array $url String or array-based URL. Unlike other URL arrays in CakePHP, this
  *    URL will not automatically handle passed and named arguments in the $url parameter.
  * @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
  *    also be used to submit GET/POST data, and named/passed arguments.
  * @return mixed Boolean true or false on success/failure, or contents
  *    of rendered action if 'return' is set in $extra.
  */
 public function requestAction($url, $extra = array())
 {
     if (empty($url)) {
         return false;
     }
     if (($index = array_search('return', $extra)) !== false) {
         $extra['return'] = 0;
         $extra['autoRender'] = 1;
         unset($extra[$index]);
     }
     $arrayUrl = is_array($url);
     if ($arrayUrl && !isset($extra['url'])) {
         $extra['url'] = array();
     }
     if ($arrayUrl && !isset($extra['data'])) {
         $extra['data'] = array();
     }
     $extra += array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1);
     $data = isset($extra['data']) ? $extra['data'] : null;
     unset($extra['data']);
     if (is_string($url) && strpos($url, Router::fullBaseUrl()) === 0) {
         $url = Router::normalize(str_replace(Router::fullBaseUrl(), '', $url));
     }
     if (is_string($url)) {
         $request = new CakeRequest($url);
     } elseif (is_array($url)) {
         $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
         $params = $extra + $params;
         $request = new CakeRequest(Router::reverse($params));
     }
     if (isset($data)) {
         $request->data = $data;
     }
     $dispatcher = new Dispatcher();
     $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
     Router::popRequest();
     return $result;
 }
 /**
  * Calls a controller's method from any location. Can be used to connect controllers together
  * or tie plugins into a main application. requestAction can be used to return rendered views
  * or fetch the return value from controller actions.
  *
  * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  * URL.  You should use URL formats that are compatible with Router::reverse()
  *
  * #### Passing POST and GET data
  *
  * POST and GET data can be simulated in requestAction.  Use `$extra['url']` for
  * GET data.  The `$extra['data']` parameter allows POST data simulation.
  *
  * @param mixed $url String or array-based url.  Unlike other url arrays in CakePHP, this
  *    url will not automatically handle passed and named arguments in the $url parameter.
  * @param array $extra if array includes the key "return" it sets the AutoRender to true.  Can
  *    also be used to submit GET/POST data, and named/passed arguments.
  * @return mixed Boolean true or false on success/failure, or contents
  *    of rendered action if 'return' is set in $extra.
  */
 public function requestAction($url, $extra = array())
 {
     if (empty($url)) {
         return false;
     }
     App::uses('Dispatcher', 'Routing');
     if (($index = array_search('return', $extra)) !== false) {
         $extra['return'] = 0;
         $extra['autoRender'] = 1;
         unset($extra[$index]);
     }
     if (is_array($url) && !isset($extra['url'])) {
         $extra['url'] = array();
     }
     $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
     $data = isset($extra['data']) ? $extra['data'] : null;
     unset($extra['data']);
     if (is_string($url)) {
         $request = new CakeRequest($url);
     } elseif (is_array($url)) {
         $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
         $params = array_merge($params, $extra);
         $request = new CakeRequest(Router::reverse($params), false);
     }
     if (isset($data)) {
         $request->data = $data;
     }
     $dispatcher = new Dispatcher();
     $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
     Router::popRequest();
     return $result;
 }
Example #4
0
 /**
  * Lets you do functional tests of a controller action.
  *
  * ### Options:
  *
  * - `data` Will be used as the request data. If the `method` is GET,
  *   data will be used a GET params. If the `method` is POST, it will be used
  *   as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
  *   payloads to your controllers allowing you to test REST webservices.
  * - `method` POST or GET. Defaults to POST.
  * - `return` Specify the return type you want. Choose from:
  *     - `vars` Get the set view variables.
  *     - `view` Get the rendered view, without a layout.
  *     - `contents` Get the rendered view including the layout.
  *     - `result` Get the return value of the controller action. Useful
  *       for testing requestAction methods.
  *
  * @param string|array $url The URL to test.
  * @param array $options See options
  * @return mixed The specified return type.
  * @triggers ControllerTestCase $Dispatch, array('request' => $request)
  */
 protected function _testAction($url, $options = array())
 {
     $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
     $options += array('data' => array(), 'method' => 'POST', 'return' => 'result');
     if (is_array($url)) {
         $url = Router::url($url);
     }
     $restore = array('get' => $_GET, 'post' => $_POST);
     $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
     if (is_array($options['data'])) {
         if (strtoupper($options['method']) === 'GET') {
             $_GET = $options['data'];
             $_POST = array();
         } else {
             $_POST = $options['data'];
             $_GET = array();
         }
     }
     $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
     if (is_string($options['data'])) {
         $request->expects($this->any())->method('_readInput')->will($this->returnValue($options['data']));
     }
     $Dispatch = new ControllerTestDispatcher();
     foreach (Router::$routes as $route) {
         if ($route instanceof RedirectRoute) {
             $route->response = $this->getMock('CakeResponse', array('send'));
         }
     }
     $Dispatch->loadRoutes = $this->loadRoutes;
     $Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
     if (!isset($request->params['controller']) && Router::currentRoute()) {
         $this->headers = Router::currentRoute()->response->header();
         return null;
     }
     if ($this->_dirtyController) {
         $this->controller = null;
     }
     $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
     if ($this->controller === null && $this->autoMock) {
         $this->generate($plugin . Inflector::camelize($request->params['controller']));
     }
     $params = array();
     if ($options['return'] === 'result') {
         $params['return'] = 1;
         $params['bare'] = 1;
         $params['requested'] = 1;
     }
     $Dispatch->testController = $this->controller;
     $Dispatch->response = $this->getMock($this->_responseClass, array('send', '_clearBuffer'));
     $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
     // Clear out any stored requests.
     while (Router::getRequest()) {
         Router::popRequest();
     }
     $this->controller = $Dispatch->testController;
     $this->vars = $this->controller->viewVars;
     $this->contents = $this->controller->response->body();
     if (isset($this->controller->View)) {
         $this->view = $this->controller->View->fetch('__view_no_layout__');
     }
     $this->_dirtyController = true;
     $this->headers = $Dispatch->response->header();
     $_GET = $restore['get'];
     $_POST = $restore['post'];
     return $this->{$options['return']};
 }
Example #5
0
 /**
  * 指定ブログで公開状態の記事を取得
  *
  * ページ編集画面等で利用する事ができる。
  * ビュー: lib/Baser/Plugin/Blog/View/blog/{コンテンツテンプレート名}/posts.php
  *
  * 《利用例》
  * $this->BcBaser->allBlogPosts('news', 3)
  *
  * @param int $contentsName 管理システムで指定したコンテンツ名
  * @param int $num 記事件数(初期値 : 5)
  * @param array $options オプション(初期値 : array())
  *	- `tag` : タグで絞り込む場合にタグ名を指定(初期値 : null)
  *	- `content` : content.id(ブログのID) で絞り込む場合に id を指定(初期値 : null)
  *	- `post` : post.id(ブログ記事のID) で絞り込む場合に id を指定(初期値 : null)
  *	- `category` : カテゴリで絞り込む場合にアルファベットのカテゴリ名指定(初期値 : null)
  *	- `year` : 年で絞り込む場合に年を指定(初期値 : null)
  *	- `month` : 月で絞り込む場合に月を指定(初期値 : null)
  *	- `day` : 日で絞り込む場合に日を指定(初期値 : null)
  *	- `keyword` : キーワードで絞り込む場合にキーワードを指定(初期値 : null)
  *	- `template` : 読み込むテンプレート名を指定する場合にテンプレート名を指定(初期値 : null)
  *	- `direction` : 並び順の方向を指定 [昇順:ASC or 降順:DESC](初期値 : null)
  *	- `sort` : 並び替えの基準となるフィールドを指定(初期値 : null)
  *	- `page` : ページ数を指定(初期値 : null)
  * @return void
  */
 public function allBlogPosts($options = array())
 {
     $options = array_merge(array('category' => null, 'tag' => null, 'year' => null, 'month' => null, 'day' => null, 'content' => null, 'post' => null, 'keyword' => null, 'template' => null, 'direction' => null, 'page' => null, 'sort' => null), $options);
     $BlogPost = ClassRegistry::init('Blog.BlogPost');
     $conditions = $BlogPost->getConditionAllowPublish();
     if ($options['content'] !== null) {
         $conditions['BlogContent.id'] = $options['content'];
     }
     if ($options['post'] !== null) {
         $conditions['BlogPost.id'] = $options['post'];
     }
     if ($options['tag'] !== null) {
         //SELECT blog_post_id FROM mysite_pg_blog_posts_blog_tags WHERE blog_tag_id IN (1,2,3) GROUP BY blog_post_id;
         $BlogPostsBlogTag = ClassRegistry::init('ExHelper.BlogPostsBlogTag');
         $tags = $BlogPostsBlogTag->find('all', array('fields' => array('BlogPostsBlogTag.blog_post_id'), 'conditions' => array('BlogPostsBlogTag.blog_tag_id' => $options['tag']), 'group' => 'BlogPostsBlogTag.blog_post_id', 'cache' => false));
         if (is_array($tags)) {
             $tagId = null;
             foreach ($tags as $val) {
                 $tagId[] = $val['BlogPostsBlogTag']['blog_post_id'];
             }
         }
         if (!empty($conditions['BlogPost.id'])) {
             $postId = null;
             if (is_numeric($conditions['BlogPost.id'])) {
                 foreach ($tagId as $val) {
                     if ($val == $conditions['BlogPost.id']) {
                         $postId = $val;
                         break;
                     }
                 }
             } else {
                 if (is_array($conditions['BlogPost.id'])) {
                     foreach ($tagId as $val) {
                         if (in_array($val, $conditions['BlogPost.id'])) {
                             $postId[] = $val;
                         }
                     }
                 }
             }
             $conditions['BlogPost.id'] = $postId;
         } else {
             $conditions['BlogPost.id'] = $tagId;
         }
     }
     //PetitCustomField に対応。。。 alter table形式のヤツがいいなやっぱり。
     $posts = $BlogPost->find('all', array('fields' => array('BlogPost.id', 'BlogPost.name'), 'conditions' => $conditions, 'order' => array('BlogPost.posts_date DESC'), 'limit' => 100, 'cache' => false));
     $ExHelperController = new ExHelperController($this->createDummyRequest());
     //依存しているModel、Componentの読込
     $ExHelperController->constructClasses();
     //pagination用セッティング
     $ExHelperController->Paginator->settings = array('page' => 1, 'limit' => 2, 'maxLimit' => 100, 'paramType' => 'querystring');
     $posts = $ExHelperController->Paginator->paginate('BlogPost', $conditions);
     //ページネーションの出力に使うPaginatorHelper::$requestをダミーのCakeRequestに一旦差し替え
     $this->Paginator->request = $ExHelperController->request;
     //ルータの中身も差し替え
     Router::setRequestInfo($ExHelperController->request);
     // 出力関数しかないので一回制御
     ob_start();
     $this->BcBaser->pagination();
     // @todo 第2引数のdataの中身を解析する必要有
     $this->allBlogPagination = ob_get_contents();
     ob_end_clean();
     //CakeRequestを元に戻す
     $this->Paginator->request = $this->request;
     Router::popRequest();
     return $posts;
 }