2016-10-29 6 views
0

Hallo ich bin ein Web-Service mit Slim aus einem Kurs von lynda "Building APIs in PHP Verwenden des Slim Micro Framework“, aber wenn ich anmelden möchten, dieser Fehler auftrittHinweis: Undefinierter Offset: 0 in C: wamp64 www lynda2 src Chatter Middleware Authentication.php on line 12

Notice: Undefined offset: 0 in C:\wamp64\www\lynda2\src\Chatter\Middleware\Authentication.php on line 12

Authentifizierung

 




    namespace Chatter\Middleware; 

    use Chatter\Models\User; 

    class Authentication 
    { 
     public function __invoke($request, $response, $next) 
     { 
      $auth = $request->getHeader('Authorization'); 
      $_apikey = $auth[0]; 
      $apikey = substr($_apikey, strpos($_apikey, ' ') + 1); 

      $user = new User(); 
      if (!$user->authenticate($apikey)) { 
       $response->withStatus(401); 

       return $response; 
      } 

      $response = $next($request, $response); 

      return $response; 
     } 
    } 
 
User.php 


<pre><code> 


namespace Chatter\Models; 

class User extends \Illuminate\Database\Eloquent\Model 
{ 
    public function authenticate($apikey) 
    { 
     $user = User::where('apikey', '=', $apikey)->take(1)->get(); 
     $this->details = $user[0]; 

     return ($user[0]->exists) ? true : false; 
    } 
} 

</code></pre> 

index.php 

<pre><code> 

require 'vendor/autoload.php'; 
include 'bootstrap.php'; 

use Chatter\Models\Message; 
use Chatter\Middleware\Logging as ChatterLogging; 
use Chatter\Middleware\Authentication as ChatterAuth; 

$app = new \Slim\App(); 
$app->add(new ChatterAuth()); 
$app->add(new ChatterLogging()); 

$app->get('/messages', function ($request, $response, $args) { 
    $_message = new Message(); 

    $messages = $_message->all(); 

    $payload = []; 
    foreach($messages as $_msg) { 
     $payload[$_msg->id] = ['body' => $_msg->body, 'user_id' => $_msg->user_id, 'created_at' => $_msg->created_at]; 
    } 

    return $response->withStatus(200)->withJson($payload); 
}); 


$app->get('/', function ($request, $response, $args) { 
    return "This is a catch all route for the root that doesn't do anything useful."; 
}); 

// Run app 
$app->run(); 



</code></pre> 
+2

Mögliche Duplikate von [Referenz - Was bedeutet dieser Fehler in PHP?] (Http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) –

+0

Nicht wirklich ein Duplikat. – geggleto

Antwort

1

Der Fehler wird die besagt, dass, wenn Sie "Login" kein vorhanden Authorization-Header ist .

$request->getHeader('Authorization') ein leeres Array zurück, so dass, wenn Sie das erste Element des Arrays versucht, zuzugreifen, können Sie Ihre Fehlermeldung erhalten:

$_apikey = $auth[0]; // Will trigger error, since there are no elements in the array 

So um diesen Fehler aviod, erhalten $apikey wie folgt aus:

public function __invoke($request, $response, $next) 
{ 
    $auth = $request->getHeader('Authorization'); 
    $_apikey = array_shift($auth); 
    if ($_apikey) { 
     $apikey = substr($_apikey, strpos($_apikey, ' ') + 1); 
     $user = new User(); 
     if (!$user->authenticate($apikey)) { 
      return $response->withStatus(401); 
     } else { 
      return $next($request, $response); 
     } 
    } else { 
     // Authorization header is missing, therefore unauthorized access 
     return $response->withStatus(401); 
    } 
} 
Verwandte Themen