2017-02-02 18 views
0

私はkohanaフレームワークが初めてです。私は私のアプリケーションのための残りのAPIを実装する必要があります。 残りのAPIをhttps://github.com/SupersonicAds/kohana-restful-apiからダウンロードして、私のlocalhostに配置しました。モジュールの下。今ファイルstructreは、私は私が今wikiによると、私は "$これにアクセスすることができるはず「Controller_Rest」を拡張することで、コントローラを作成しているKohana rest api implementaion:SupersonicAds/kohana-restful-apiから始めるには?

Kohana::modules(array(
'auth'    => MODPATH.'auth',  // Basic authentication 
'rest'    => MODPATH.'rest', // Basic Rest example 
// 'cache'  => MODPATH.'cache',  // Caching with multiple backends 
// 'codebench' => MODPATH.'codebench', // Benchmarking tool 
'database' => MODPATH.'database', // Database access 
// 'image'  => MODPATH.'image',  // Image manipulation 
// 'minion'  => MODPATH.'minion',  // CLI Tasks 
'orm'  => MODPATH.'orm',  // Object Relationship Mapping 
// 'unittest' => MODPATH.'unittest', // Unit testing 
// 'userguide' => MODPATH.'userguide', // User guide and API documentation 
)); 

としてbootstrap.phpの中にモジュールを有効にしている enter image description here です - > _ユーザー、$ this - > _ auth_typeと$ this - > _ auth_source "というような変数がありますが、私の場合は何が起こっていないのですか? と私は、それは常に認証を使うに「無断401」

+0

コーディング ハッピーに役立ちます願っています。どんな助け? – Shridhar

+0

権限を使用するには、Kohana_RestUserクラスを拡張する必要があります。 https://github.com/SupersonicAds/kohana-restful-api/blob/master/classes/Kohana/Controller/Rest.php#L372 – bato3

答えて

0

としてのステータスを示すコンソールネットワークでチェック、あなたは、あなたが、あなたが使用しているモジュールは、抽象Kohana_RestUserクラスが付属していますKohana_RestUserクラスに

を拡張する必要がありますアプリ内で拡張する必要があります。実装が必要な唯一の関数は、保護された関数_find()です。関数の実装では、APIキーに基づいてユーザー関連のデータをロードすることが想定されています。

私は一例で

<?php 
// Model/RestUser.php 
class RestUser extends Kohana_RestUser { 
    protected $user=''; 
    protected function _find() 
    { 

    //generally these are stored in databases 
    $api_keys=array('abc','123','testkey'); 

    $users['abc']['name']='Harold Finch'; 
    $users['abc']['roles']=array('admin','login'); 

    $users['123']['name']='John Reese'; 
    $users['123']['roles']=array('login'); 

    $users['testkey']['name']='Fusco'; 
    $users['testkey']['roles']=array('login'); 

    foreach ($api_keys as $key => $value) { 
     if($value==$this->_api_key){ 
      //the key is validated which is authorized key 
      $this->_id = $key;//if this not null then controller thinks it is validated 
      //$this->_id must be set if key is valid. 
      //setting name 
      $this->user = $users[$value]; 
      $this->_roles = $users[$value]['roles']; 
      break; 

     } 
    } 


    }//end of _find 
    public function get_user() 
    { 
     return $this->name; 
    } 
}//end of RestUser 

今テストコントローラー

<?php defined('SYSPATH') or die('No direct script access.'); 
//Controller/Test.php 
class Controller_Test extends Controller_Rest 
{ 
    protected $_rest; 
    // saying the user must pass an API key.It is set according to the your requirement 
    protected $_auth_type = RestUser::AUTH_TYPE_APIKEY; 
    // saying the authorization data is expected to be found in the request's query parameters. 
    protected $_auth_source = RestUser::AUTH_SOURCE_GET;//depends on requirement/coding style 
    //note $this->_user is current Instance of RestUser Class 

    public function before() 
    { 
     parent::before(); 
     //An extension of the base model class with user and ACL integration. 
     $this->_rest = Model_RestAPI::factory('RestUserData', $this->_user); 

    } 
    //Get API Request 
    public function action_index() 
    { 

     try 
     { 

       $user = $this->_user->get_name(); 
       if ($user) 
       { 
        $this->rest_output(array(
         'user'=>$user, 

        )); 
       } 
       else 
       { 
        return array(
         'error' 
        ); 
       } 
     } 
     catch (Kohana_HTTP_Exception $khe) 
     { 
      $this->_error($khe); 
      return; 
     } 
     catch (Kohana_Exception $e) 
     { 
      $this->_error('An internal error has occurred', 500); 
      throw $e; 
     } 

    } 
    //POST API Request 
    public function action_create() 
    { 
     //logic to create 
     try 
     { 
      //create is a method in RestUserData Model 
      $this->rest_output($this->_rest->create($this->_params)); 
     } 
     catch (Kohana_HTTP_Exception $khe) 
     { 
      $this->_error($khe); 
      return; 
     } 
     catch (Kohana_Exception $e) 
     { 
      $this->_error('An internal error has occurred', 500); 
      throw $e; 
     } 
    } 
    //PUT API Request 
    public function action_update() 
    { 
     //logic to create 
    } 
    //DELETE API Request 
    public function action_delete() 
    { 
     //logic to create 
    } 

} 

今RestUserDataモデル

<?php 
//Model/RestUserData.php 
class Model_RestUserData extends Model_RestAPI { 

     public function create($params) 
     { 
      //logic to store data in db 
      //You can access $this->_user here 
     } 

} 

だから、index.phpを/テストをあなたに説明します?APIKEY = ABCは

を返します。
{ 
    "user": { 
     "name": "Harold Finch", 
     "roles": [ 
      "admin", 
      "login" 
     ] 
    } 
    } 

注:APIKEYでKは

資本/大文字である私は、これは私は上記の問題に問題がフォルダの配置だと思います:)

関連する問題