无需数据库的雄辩查询(数据是从外部API检索的) [英] Eloquent queries without database (data is retrieved from external API)

查看:106
本文介绍了无需数据库的雄辩查询(数据是从外部API检索的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel Web应用程序(website.nl)和一个Laravel API(api.website.nl)。

I have a Laravel web application (website.nl) AND an Laravel API (api.website.nl).

当用户使用登录表单登录时在网站上。向API发出了发布请求。登录请求成功后,将以JSON字符串的形式发送回令牌AND用户对象。登录方法如下所示。

When the user logs in with the login form on the website. A post request is done to the API. When the login request is successfull a token AND User object is send back in the form of a JSON string. The login method looks like this.

public function login(Request $request)
{
    $email = $request->input('email');
    $password = $request->input('password');

    $response = $this->client->request('POST', self::$apiRootUrl . '/login', [
        'headers'     => [
            'Accept' => 'application/json',
        ],
        'form_params' => [
            'email'    => $email,
            'password' => $password
        ],
    ]);

    $result = json_decode($response->getBody()->getContents());

    if (isset($result->success->token))
    {
        $user = User::create([
            'id'    => $result->success->user->id,
            'name'  => $result->success->user->name,
            'email' => $result->success->user->email,
            'password' => $password
        ]);
        Auth::login($user);
        $this->apiToken = $result->success->token;

        return $this->sendLoginResponse($request);
    }

    return $this->sendFailedLoginResponse($request);
}

我希望此登录功能尽可能愚蠢,也就是说,让API会确定这是否是有效的登录请求。此方法仅传递参数。

Where I want this login function to be as stupid as possible, meaning, let the API figure out if this is a valid login request. This method only passes the arguments.

主网站应该没有数据库。它仅使用API​​登录,获取令牌并使用该令牌进行请求以收集数据,以供用户在主网站的信息中心上查看。

The main website should not have a database. It only uses the API to login, get a token, and do requests with that token to gather data for the user to see on the main website's dashboard.

出于这个原因,我的问题是:如何存储从API检索到的User对象,使其像Eloquent模型一样可查询。但无需将其存储在数据库中。同样,主要网站不需要数据库。

For this reason my question is this: How can I store the User object retrieved from the API in such a manner that it will be query-able like an Eloquent model. But without the need to store it in a database. Again, the main website shouldn't need a database. It only communicates with the API for it's data.

我希望这个问题是有意义的,如果没有,请询​​问更多细节。

I hope this question makes sense, if not, please ask for more details.

我不想在主网站上创建带有迁移的重复数据库。因为这就是 .api 子域的工作。同样,所有模型都在会话中使用。永远不会存储更长的时间,因为它也位于 .api 子域的存储方法中。

I don't want to create a duplicate database with migrations on the main website. Because that's the job of the .api subdomain. Also, all the models are used within a session. Never being stored for a longer time, because that's also located in a store method on the .api subdomain.

推荐答案

这将需要您为Eloquent编写自己的语法,Eloquent是您正在使用的API的ActiveRecord实现。
从理论上讲,它是有可能的,实际上它需要做很多工作。

This would require you to write your own grammar for Eloquent, an ActiveRecord implementation for the API you're using. In theory it's possible, in practice it's a lot of work.

在这里看看Laravel如何实现当前支持的数据库类型。
https://laravel.com/api/5.8/ Illuminate / Database / Schema / Grammars.html

Take a look here for how Laravel implements the database types it currently supports. https://laravel.com/api/5.8/Illuminate/Database/Schema/Grammars.html

这篇关于无需数据库的雄辩查询(数据是从外部API检索的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆