【问题标题】:All the APIs the laravel middleware is not workinglaravel 中间件的所有 API 都不起作用
【发布时间】:2019-07-16 08:44:26
【问题描述】:

我在 laravel 中使用 auth 中间件。如果我正在调用没有middleware('auth:api') 的路由,那么它工作正常但middleware('auth:api') 下的路由不工作。

我已尝试更新 htaccess 文件并尝试了所有解决方案,但没有结果。

我的路线代码是 -

Route::group(['middleware' => 'auth:api'], function(){
Route::post('unregistered', 'API\InterestController@getUnregistered');
    Route::post('new-campaign-listing','API\CampaignController@new_campaign_list');

    Route::post('registered-campaign-listing','API\CampaignController@registered_campaign_list');

    Route::get('past-campaign-listing','API\CampaignController@past_campaign_list');
    Route::get('getcampaignhistory','API\CampaignController@getCampaignhistory');

    Route::post('register-for-campaign', 'API\CampaignController@register');
    Route::post('top-campaign-photos', 'API\CampaignController@topPhotos');


    Route::get('user-detail', 'API\PassportController@getdetails');

    //update profile
    Route::post('update-profile', 'API\PassportController@update_profile');





    //update profile


    Route::post('shared-post-data','API\CampaignController@shared_post_data');
    Route::post('vip-campaigns','API\CampaignController@vip_campaigns');
    Route::post('vip_client','API\CampaignController@vip_client');

    Route::post('add_user_interests', 'API\InterestController@add_user_interests');
    Route::get('user-interests', 'API\InterestController@user_interests');

    Route::post('update-user-interests', 'API\InterestController@update_user_interest');

    //polls
    Route::post('polls-for-vote', 'API\PollController@pollsForVote');
    Route::post('vote', 'API\PollController@vote');

    Route::post('contact-us', 'API\PassportController@contactmail');
    Route::get('campaignpostimage','API\PassportController@campaignpostimage');

    Route::post('firebase-token', 'API\PassportController@get_token');

    //notifications
    Route::post('notifications', 'API\InterestController@notifcations');

    Route::post('unread_notifications', 'API\InterestController@unreadNotifications');

    Route::get('coupons','API\CampaignController@coupons_list');
    Route::post('redeem','API\CampaignController@redeem_points');

    Route::post('campaign-detail','API\CampaignController@campaign_detail');
    Route::get('rewards_history','API\CampaignController@rewards_history');


});

我在护照控制器中的功能是:

public function getDetails()
    {
dd("hello");
}

我的 htaccess 文件代码是:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#    RewriteCond %{HTTP:Authorization} ^(.*)
#    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

即使控制器中的 dd 也不打印。表示 API URL 不起作用

【问题讨论】:

  • 你是否在URI的请求中添加了api,例如http://localhost/api/user-detail

标签: laravel api authentication middleware


【解决方案1】:

错误信息是什么? 注意:你应该把路由放在 api.php 中

【讨论】:

  • 错误信息是:{ "error": "Unauthenticated."是的,所有的 api 都在 api.php
  • 您应该通过标头或像 example.com/api/?api_token=XXX 这样的参数发送令牌。或用于标头身份验证:'bearer your Token'以检查用户是否登录!首先通过您的 api 登录,然后获取令牌并放入您的标题
【解决方案2】:

请提供有关您如何使用 API 的更多信息? 如果您使用来自不同域(跨域)的 API,那么您将面临 CORS 问题,在这种情况下,您需要将以下 package 添加到您的 laravel 应用程序中

如果你在同一个域上,你可以关注 documentation.

就我而言this git hub 报告的问题解决了我的问题

【讨论】:

  • 不不。我从同一个域访问 api
猜你喜欢
  • 2020-04-28
  • 1970-01-01
  • 2011-07-22
  • 2014-05-24
  • 2015-04-03
  • 2012-04-28
  • 2020-10-02
  • 2018-07-14
  • 1970-01-01
相关资源
最近更新 更多