2014-12-24 68 views
4

我在Laravel有一个简单的API。该路线文件是像这样:服务器上不允许标题?

<?php 

Route::resource('airports', 'AirportController'); 

Route::resource('flights', 'FlightController'); 

Route::resource('reservations', 'ReservationController'); 

Route::get('auth', '[email protected]'); 
Route::post('auth', '[email protected]'); 
Route::delete('auth', '[email protected]'); 

过滤的文件中有一个自定义过滤器添加像这样:

Route::filter('auth_token', function() 
{ 
    $auth_token = Request::header('Authorization'); 

    if(!AuthToken::where('auth_token', '=', $auth_token)->first()){ 
     return Response::json([], 401); 
    } 
}); 

所有资源需要auth_token过滤器前通过。现在,这在我的本地机器上效果很好,但只要在我的服务器上尝试,即使通过有效的令牌,一切都是未经授权的。在我的自定义过滤器中,我通过dd($auth_token)得出的问题是它返回null,这意味着我的服务器由于某种原因不接受标头。

我的.htaccess文件看起来像这样:

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

    RewriteEngine On 

    # Redirect Trailing Slashes... 
    RewriteRule ^(.*)/$ /$1 [L,R=301] 

    # Handle Front Controller... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 
    Header set Access-Control-Allow-Origin "*" 
    Header set Access-Control-Allow-Headers "*" 
</IfModule> 

我使用REST邮差客户端来测试我的应用程序。系统中只有一个用户使用电子邮件“[email protected]”和密码“admin12345”。您可以将POST这些详细信息添加到/auth路由中,并获取授予的访问令牌,然后可以使用该令牌来获取系统中的其他资源。

该应用程序托管here。我究竟做错了什么?

回答

1

路线的顺序可能是一个问题。尝试像这样反转,看看它是否工作...

Route::get('auth', '[email protected]'); 
Route::post('auth', '[email protected]'); 
Route::delete('auth', '[email protected]'); 

Route::resource('airports', 'AirportController'); 
Route::resource('flights', 'FlightController'); 
Route::resource('reservations', 'ReservationController');