2014-01-18 3 views
-4

tutorial에서는 PHP에서 RESTful API를 만드는 방법을 설명합니다. 내 문제는 섹션의 코드와 관련이 있습니다. .I 복사/넷빈즈의 코드를 붙여 넣어이 코드에 대한 메시지를 얻을 :코드에서 구문 오류가있는 곳을 찾을 수 없습니다

$this->User = $User; 

내가 편집기에서 얻을 메시지는 변수 $이 예기치 않은 것을입니다. 여기서 오류가 어디 있는지 찾을 수 없습니다. 감사합니다. .

class MyAPI extends API 
{ 
protected $User; 

public function __construct($request, $origin) { 
    parent::__construct($request); 

    // Abstracted out for example 
    $APIKey = new Models\APIKey(); 
    $User = new Models\User(); 

    if (!array_key_exists('apiKey', $this->request)) { 
     throw new Exception('No API Key provided'); 
    } else if (!$APIKey->verifyKey($this->request['apiKey'], $origin)) { 
     throw new Exception('Invalid API Key'); 
    } else if (array_key_exists('token', $this->request) && 
     !$User->get('token', $this->request['token'])) 

     throw new Exception('Invalid User Token'); 
    } 

    $this->User = $User; 
    } 

/** 
* Example of an Endpoint 
*/ 
protected function example() { 
    if ($this->method == 'GET') { 
     return "Your name is " . $this->User->name; 
    } else { 
     return "Only accepts GET requests"; 
    } 
} 
} 
+0

그 줄 앞에 코드를 추가하십시오. 아마';'또는'}'가 누락되었을 것입니다. 이것이'$ this'가 예기치 않은 이유입니다. – Niels

+0

더 많은 코드를 게시하십시오 (오류 주위에 5-10 줄). – Jonathan

+0

무슨 코드입니까? } –

답변

3

후에는 {

} else if (array_key_exists('token', $this->request) && 
     !$User->get('token', $this->request['token'])) // HERE 
+0

예. 오류가 발생했습니다. 감사합니다. –

0

$this는 당신이 클래스 내에있는 경우에만 허용된다 : 편집기에 표시되는 다음

는 코드입니다.

클래스 외부에 $this을 사용해야하므로 오류가 발생합니다.

편집 :

가 열린 중괄호 { 없다

else if (array_key_exists('token', $this->request) && 
     !$User->get('token', $this->request['token'])) 
+0

아니요, $ this는 클래스 내부에 있으므로 오류 소스가 아니어야합니다. –

+1

다음 코드를 게시하십시오. 정확한 오류는 무엇입니까? – Manu

+0

나는 baloon window이기 때문에 에디터에서 에러 메시지를 복사/붙여 넣기 할 수는 없다.하지만 여기서 말하는 것은 다음과 같다 : unexpexted variable $ this} –

0

이 교체 누락, 당신은 제대로 _construct 함수에서 else if 조건을 닫지 않은

  public function __construct($request, $origin) { 
       parent::__construct($request); 

       // Abstracted out for example 
       $APIKey = new Models\APIKey(); 
       $User = new Models\User(); 

       if (!array_key_exists('apiKey', $this->request)) { 
        throw new Exception('No API Key provided'); 
       } else if (!$APIKey->verifyKey($this->request['apiKey'], $origin)) { 
        throw new Exception('Invalid API Key'); 
       } else if (array_key_exists('token', $this->request) && !$User->get('token', $this->request['token'])){   
        throw new Exception('Invalid User Token');      
       } 

      $this->User = $User; 
     } 
관련 문제