Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kohana rest api实现:如何从超音速ADS/Kohana restful api开始?_Rest_Api_Kohana_Kohana Orm_Kohana 3.3 - Fatal编程技术网

Kohana rest api实现:如何从超音速ADS/Kohana restful api开始?

Kohana rest api实现:如何从超音速ADS/Kohana restful api开始?,rest,api,kohana,kohana-orm,kohana-3.3,Rest,Api,Kohana,Kohana Orm,Kohana 3.3,我不熟悉kohana框架。我需要为我的应用程序实现RESTAPI。 我已经从本地主机下载了RESTAPI并将其放入本地主机。在模块下。现在文件结构是 我已经在bootstrap.php中启用了模块 Kohana::modules(array( 'auth' => MODPATH.'auth', // Basic authentication 'rest' => MODPATH.'rest', // Basic Res

我不熟悉kohana框架。我需要为我的应用程序实现RESTAPI。 我已经从本地主机下载了RESTAPI并将其放入本地主机。在模块下。现在文件结构是 我已经在bootstrap.php中启用了模块

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
));
我已经通过扩展“controller\u Rest”创建了一个控制器,现在根据我应该能够访问“$this->\u user、$this->\u auth\u type和$this->\u auth\u source”变量,但在我的情况下,它不会发生,我做错了什么?
我检查了控制台网络,它总是显示状态为“401未授权”

要使用授权,您需要扩展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
注意:apiKey中的K是大写字母

我希望这有帮助
快乐编码:)

我认为上面的问题在于文件夹的位置。有什么帮助吗?要使用授权,您需要扩展Kohana_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
    }

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

}
{
     "user": {
        "name": "Harold Finch",
        "roles": [
            "admin",
            "login"
        ]
    }
   }