【问题标题】:How can I get the api_token in the controller?如何在控制器中获取 api_token?
【发布时间】:2021-03-25 10:48:31
【问题描述】:

我有这个 Vuejs 代码:

axios.get('/api/electronic_collection?page='+this.currentPage+'&api_token='+App.apiToken)
                .then(response => {
                    this.posts = response.data.data.data;
                    this.total = response.data.data.last_page;
                    this.currentPage = response.data.data.current_page;
                    this.rowsQuantity = response.data.data.total;
                });

您如何看到我正在 seding api_token,因为它连接到 API Restful,但我想做的是我需要在控制器中获取此 api_token。

我的控制器是这样的:

public function index(Request $request)
{
     but I'd like to get the api_token here, how can I do that I mean $_GET['api_token'] is it like this?
}

【问题讨论】:

  • 这能回答你的问题吗? Access query string values from Laravel
  • 为什么你认为无论如何都需要获取查询字符串值?您应该只使用Auth::user() 来获取经过身份验证的用户。

标签: laravel vue.js


【解决方案1】:

查询字符串是输入源。您可以向请求请求此输入:

$request->input('api_token')

Laravel 8.x Docs - Requests - Retrieving Input - Retrieving and Input valueinput

【讨论】:

    【解决方案2】:

    你可以从 $request 对象中得到它

    public function index(Request $request)
    {
         $apiToken = $request->api_token;
    
        //OR
    
        $apiToken = $request->query('api_token');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 2021-07-25
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多