Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:30:04+00:00 2026-06-18T02:30:04+00:00

It has come to my attention that the redirect controller method is not working

  • 0

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”.

Let’s take a look at my ArticlesController add action where redirect works in one instance but not in another.

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->CustomPage->AccessMessage(4, $this->viewVars['access']);

    if($access_message){

        // Flash works but redirect does not
        $this->Session->setFlash(__($access_message));
        $this->redirect(array('action' => 'index', 'page' => $page));
        // Also tried
        // $this->redirect(array('controller'=>'articles', 'action' => 'index', 'page' => $page), null, true);
    } else
    {

    if ($this->request->is('post')) {

        $this->Article->create();
                if ($this->Article->save($this->request->data)) {

                    // BLAH BLAH save post, do other stuff
                    // BLAH BLAH save post, do other stuff

                    // This flash and redirect works
                    $this->Session->setFlash(__('The article has been saved'));
                    $this->redirect(array('action' => 'view', 'id' => $article_id, 'page' => $page));

                    } else {
                    $this->Session->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

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.

My component code is:

public function AccessMessage($required_level, $user_level) {

    if(!$user_level && $this->_View->viewVars['access']){
        $user_level = $this->_View->viewVars['access'];
    }

    if(!$required_level || !$user_level || $user_level != $required_level){
        $accessModel = ClassRegistry::init('Access');
        $access_message = $accessModel->field('access_message', array('Access.id' => $required_level));
     }

      return $access_message;
}

Edit 1: 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

public function AccessMessage($required_level, $user_level) {

    if(!$user_level && $this->_View->viewVars['access']){
        $user_level = $this->_View->viewVars['access'];
    }

    if(!$required_level || !$user_level || $user_level != $required_level){
        $access_message = 1;
     }

      return $access_message;
}

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?

        $accessModel = ClassRegistry::init('Access');
        $access_message = $accessModel->field('access_message', array('Access.id' => $required_level));

Please note I have also tried:

        $access_message = ClassRegistry::init('Access')->field('access_message', array('Access.id' => $required_level));

And

        $this->loadModel('Access');
        $access_message = $this->Access->field('access_message', array('Access.id' => $required_level));

Gists:

component gist: https://gist.github.com/970a951715205c222348

controller gist: https://gist.github.com/2b90e5af2518a81672fb

access model gist: https://gist.github.com/bhndbrwneyes/f333a93f0a21302d832f

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:30:05+00:00Added an answer on June 18, 2026 at 2:30 am

    You may have space before/after php Opening/Closing tags in controller and models. Remove all the closing tags from all controllers and models and any whitespace before opening tags. Then check the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It has come to my attention that a user has been trying to create
It has come down from high places that a webapp I've been working on
The time has come for me to understand MVC, so that's what I'm trying
I have an issue that has just recently come to my attention. We have
It has come to my attention that there is no standard form of regular
It has come to my attention that http://www.example.com/Home/About is considered completely different from http://www.example.com/homE/abouT
This is not a coding question, but am hoping that someone has come across
I'm want to use RedirectAttibutes property that has come up in Spring 3.1, I
I have a data frame in R that has come about from running some
I'm currently working on the internationalization of a product and an issue has come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.