你可以在 Laravel 中拥有你的 API 并吃(消费)它吗? [英] You can have your API and eat (consume) it in Laravel?

查看:19
本文介绍了你可以在 Laravel 中拥有你的 API 并吃(消费)它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个在 Laravel 中返​​回 json 的 API.(routes/api.php)

I have made an API that returns json in Laravel. (routes/api.php)

现在我想在项目的 web 端使用上述 API(routes/web.php(包括中间件)、刀片视图强>等).

Now I want to consume said API inside my web-side of the project (routes/web.php (including middleware), blade views etc.).

我目前的解决方案是这样的:

The current solution that I have is something like this:

public function register(Request $request) {
    // password1 == password2 etc (form logic / validation)
    $internal_request = Request::create($this->base_api_url . 'register', 'POST');
    $internal_request->replace($request->input());
    $response = Route::dispatch($internal_request);
}

如果表单有效,它会将请求转发"给我的 api 的对应 api.但我感觉这不是真正的最佳实践或聪明.除了 loginregister 之外的其他路由使用存储在会话中的 api 令牌进行调用.他们将令牌x-token"作为标头附加到 $internal_request.在中间件中这样做更好吗?是否有一些最佳实施示例?

Which "forwards" the request to the api counterpart to my api if the form is valid. But I have the feeling this is not really best practice or smart. Other routes except login and register use the api token stored in session to make the calls. they append the token "x-token" as a header to $internal_request. Is it better to do this in middleware? Is there some example of a best implementation somewhere?

我的 API 有一个这样的方法:

My API has a method like this:

POST api/注册哪个检查必填字段是否存在并具有 rigt 格式(验证)

POST api/register Which check if the the required fields exist and have the rigt format (validation)

我的网络路由有 /register

这将首先检查密码验证输入(pass1 == pass2)中的密码是否匹配,然后将其传递给等效的 api.

This will first check if the passwords match from the password validation inputs (pass1 == pass2) and will then pass it to the api equivalent.

所以 web 应该是 api 的超集(验证方面).

So web should be a superset (validation wise) of api.

推荐答案

据我了解您的问题,您希望对 api 和 web 请求应用相同的逻辑,并且您可能只想 (a) 验证表单Web 请求和/或 (b) 将响应包装在 json 中以用于 API 请求.

As I understand your question, you want to apply the same logic to both api and web requests and you may just want to (a) validate the form for a web request and/or (b) wrap the response in json for an API request.

我认为最好的方法是为 web 和 api 请求引用相同的控制器和方法,例如:

I think the best way to do this would be to refer to the same controller and method for both web and api requests, so for example:

在你的 routes/web.php 中,添加 Route::post('/register', 'RegistrationController@register);`

In your routes/web.php, add Route::post('/register', 'RegistrationController@register);`

在你的路由/api.php中,添加Route::post('/register', 'RegistrationController@register)`

And in your routes/api.php, add Route::post('/register', 'RegistrationController@register)`

所以最终两个请求(api/register 和/register)都到达了同一个控制器和方法.

So eventually both requests (api/register and /register) hit the same controller and method.

现在,在您的控制器中,您可以根据请求执行额外的操作,如下所示:

Now, in your controller, you can perform the extra actions depending on request like so:

public function register(Request $request) {
    if(!$request->expectsJson()) { // you may want to swap this with $request->isJson() depending on the HTTP headers for your app
        $this->validateWebRegistration($request); // your validation logic specific to web requests here
    }

    // common logic here (including validation); store result as $result

    if($request->expectsJson()) { // based on HTTP headers as above
        return response()->json($result);
    } else {
        return view('register', $result);
    }
}

这篇关于你可以在 Laravel 中拥有你的 API 并吃(消费)它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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