Nisarg Bhavsar
Nisarg Bhavsar

Reputation: 956

Can not access method in CakePHP

I am working in CakePHP 2.6.1 and I have a project in which I have to create an API. So I have created a function and its working fine when I am logged in but when I try to access without login, it redirects to the login page.

My function looks like :

class AndroidController extends AppController {



public function admin_survey_question()
{
        $this->loadModel('Question');
        Configure::write('debug', '2');
        $survey_id = $_REQUEST['survey_id'];
        $this->layout = "";
        //$condition = "Question.survey_id = '".$survey_id."'";
        $this->Question->unbindModel(
            array('hasMany' => array('Result'))
        );

        $info = $this->Question->find('all', array(
            'fields' => array('Question.id,Question.question, Question.options'),
            'conditions' => array(
                        "Question.survey_id" => $survey_id  /*dont use array() */
                )
            ));
        echo json_encode($info);
        exit;
    }
}

Here,In core.php there is a Routing.prefixes used as admin.

Configure::write('Routing.prefixes', array('admin','services'));

When I call this api

http://navyon.com/dev/mt/admin/android/survey_question?survey_id=2

then it redirects to the login page.

I need access api without login.So how can I resolve this problem?

Upvotes: 1

Views: 58

Answers (1)

kamal pal
kamal pal

Reputation: 4207

To make accessible this method admin_survey_question without authentication, you need to allow it in beforeFilter

class AndroidController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('admin_survey_question');
    }

    public function admin_survey_question()
    {
        $this->loadModel('Question');
        Configure::write('debug', '2');
        $survey_id = $_REQUEST['survey_id'];
        $this->layout = "";
        //$condition = "Question.survey_id = '".$survey_id."'";
        $this->Question->unbindModel(
            array('hasMany' => array('Result'))
        );

        $info = $this->Question->find('all', array(
            'fields' => array('Question.id,Question.question, Question.options'),
            'conditions' => array(
                        "Question.survey_id" => $survey_id  /*dont use array() */
                )
            ));
        echo json_encode($info);
        exit;
    }
}

See Docs

Upvotes: 3

Related Questions