首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kohana实现:如何从SupersonicAds/kohana开始?

Kohana实现:如何从SupersonicAds/kohana开始?
EN

Stack Overflow用户
提问于 2017-02-02 14:21:23
回答 1查看 757关注 0票数 0

我是科哈纳框架的新手。我需要为我的应用程序实现rest。我已经从https://github.com/SupersonicAds/kohana-restful-api下载了rest,并将其放在本地主机中。在模块下。现在文件结构是

我已经在bootstrap.php中启用了模块

代码语言:javascript
复制
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_Rest“创建了一个控制器,根据维基,我应该能够访问"$this->_user,$this->_auth_type和$this->_auth_”变量,但是在我的例子中,它没有发生我做错了什么?我检查了控制台网络,它总是显示为"401未经授权“的状态。

EN

回答 1

Stack Overflow用户

发布于 2017-07-21 09:08:17

为了使用授权,您需要扩展Kohana_RestUser类

您使用的模块附带了一个抽象的Kohana_RestUser类,您必须在应用程序中对其进行扩展。唯一需要实现的函数是受保护的函数_find()。该函数的实现将根据API键加载任何与用户相关的数据。

我会用一个例子来解释你

代码语言:javascript
复制
<?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

现在测试控制器

代码语言:javascript
复制
<?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模型

代码语言:javascript
复制
<?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/test?apiKey=abc返回

代码语言:javascript
复制
{
     "user": {
        "name": "Harold Finch",
        "roles": [
            "admin",
            "login"
        ]
    }
   }

注: apiKey中的K为大写/大写

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42004631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档