2016-07-01 5 views
1

私はCakePHP 2.6.1で作業しており、私はAPIを作成する必要があるプロジェクトを持っています。だから私はログインしているときに機能し、正常に動作していますが、ログインせずにアクセスしようとすると、ログインページにリダイレクトされます。ここでCakePHPのメソッドにアクセスできない

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; 
    } 
} 

、core.phpのではRouting.prefixesは、管理者として使用している:

My機能は次のようになります。

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

私は

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

このAPIを呼び出すと、それは、ログインページにリダイレクトされます。

ログインせずにapiにアクセスする必要があります。どうすればこの問題を解決できますか?

+0

'$ survey_id = $ _REQUEST [ 'survey_id']を参照してくださいにそれを許可する必要があります;'任意のがあるべきではありませんCakePHPアプリケーションコード内のグローバル - '$ this-> request-> query( 'survey_id')' [docs](http://book.cakephp.org/3.0/en/controllers/request-response.html#クエリ文字列パラメータ)。あなたの質問には常に使用しているバージョンをタグ付けしてください。 – AD7six

答えて

3

認証なしでアクセスできるこの方法admin_survey_questionを行うには、あなたが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; 
    } 
} 

Docs

+1

その作業。どうもありがとうございます。 –

関連する問題