title: CakePHP 2.2.4: Redirect not Working When Using Component
body: <p>It has come to my attention that the redirect controller method is not working part of the time. No message appears when I set debug > 0. I don't echo any code before calling the redirect method so it shouldn't be because of "headers already sent".</p>
  
  <p>Let's take a look at my ArticlesController add action where redirect works in one instance but not in another.</p>
  
  <pre><code>public function add($page = null) {
      // Custom component to get if user has required access level
      // of page to write an article. If not, setflash to an error message
      // specific to user's access level and redirect.
  
      $access_message = $this-&gt;CustomPage-&gt;AccessMessage(4, $this-&gt;viewVars['access']);
  
      if($access_message){
  
          // Flash works but redirect does not
          $this-&gt;Session-&gt;setFlash(__($access_message));
          $this-&gt;redirect(array('action' =&gt; 'index', 'page' =&gt; $page));
          // Also tried
          // $this-&gt;redirect(array('controller'=&gt;'articles', 'action' =&gt; 'index', 'page' =&gt; $page), null, true);
      } else
      {
  
      if ($this-&gt;request-&gt;is('post')) {
  
          $this-&gt;Article-&gt;create();
                  if ($this-&gt;Article-&gt;save($this-&gt;request-&gt;data)) {
  
                      // BLAH BLAH save post, do other stuff
                      // BLAH BLAH save post, do other stuff
  
                      // This flash and redirect works
                      $this-&gt;Session-&gt;setFlash(__('The article has been saved'));
                      $this-&gt;redirect(array('action' =&gt; 'view', 'id' =&gt; $article_id, 'page' =&gt; $page));
  
                      } else {
                      $this-&gt;Session-&gt;setFlash(__('The article could not be saved. Please, try again.'));
                      } // end else if article cannot be saved
          } // if method is post
      } // end if user has access
  } // end add action
  </code></pre>
  
  <p>It definitely has something to do with the component but I'm not sure what. Maybe since redirect is called right after the component is used, the "$this" is trying to do the redirect method on the component instead of the controller. I tried $this->Article->redirect and reloading the Article model before the redirect but neither of those worked.</p>
  
  <p>My component code is:</p>
  
  <pre><code>public function AccessMessage($required_level, $user_level) {
  
      if(!$user_level &amp;&amp; $this-&gt;_View-&gt;viewVars['access']){
          $user_level = $this-&gt;_View-&gt;viewVars['access'];
      }
  
      if(!$required_level || !$user_level || $user_level != $required_level){
          $accessModel = ClassRegistry::init('Access');
          $access_message = $accessModel-&gt;field('access_message', array('Access.id' =&gt; $required_level));
       }
  
        return $access_message;
  }
  </code></pre>
  
  <p><strong>Edit 1:</strong> Ok so I did some digging to find exactly where the problem is stemming from. The USE of the component is not the problem which I thought it was before. If all I have in my component is</p>
  
  <pre><code>public function AccessMessage($required_level, $user_level) {
  
      if(!$user_level &amp;&amp; $this-&gt;_View-&gt;viewVars['access']){
          $user_level = $this-&gt;_View-&gt;viewVars['access'];
      }
  
      if(!$required_level || !$user_level || $user_level != $required_level){
          $access_message = 1;
       }
  
        return $access_message;
  }
  </code></pre>
  
  <p>Then it works. The issue is with these two lines which are correctly implemented because they return the value for $access_message I am expecting, but they are interfering with the ability to redirect. Perhaps headers are already sent out?</p>
  
  <pre><code>        $accessModel = ClassRegistry::init('Access');
          $access_message = $accessModel-&gt;field('access_message', array('Access.id' =&gt; $required_level));
  </code></pre>
  
  <p>Please note I have also tried:</p>
  
  <pre><code>        $access_message = ClassRegistry::init('Access')-&gt;field('access_message', array('Access.id' =&gt; $required_level));
  </code></pre>
  
  <p>And</p>
  
  <pre><code>        $this-&gt;loadModel('Access');
          $access_message = $this-&gt;Access-&gt;field('access_message', array('Access.id' =&gt; $required_level));
  </code></pre>
  
  <p><strong>Gists:</strong></p>
  
  <blockquote>
    <p>component gist: <a href="https://gist.github.com/970a951715205c222348" rel="nofollow">https://gist.github.com/970a951715205c222348</a></p>
    
    <p>controller gist: <a href="https://gist.github.com/2b90e5af2518a81672fb" rel="nofollow">https://gist.github.com/2b90e5af2518a81672fb</a></p>
    
    <p>access model gist: <a href="https://gist.github.com/bhndbrwneyes/f333a93f0a21302d832f" rel="nofollow">https://gist.github.com/bhndbrwneyes/f333a93f0a21302d832f</a></p>
  </blockquote>