marcj / php-rest-service

Php-Rest-Service is a very simple and fast PHP class for server-side RESTful JSON APIs.
MIT License
216 stars 74 forks source link

Error, Class not found #6

Closed jpl12 closed 11 years ago

jpl12 commented 11 years ago

The example "Way 1. The dirty & fast" OK, but I get the following error with "Way 2. Auto-Collection", :

Fatal error: Class 'MyRestApi\Admin' not found in /usr/local/www/apache22/data/webservice/vendor/marcj/php-rest-service/RestService/Server.php on line 597

Folder Tree

webservice ----MyRestApi --------Admin.php ----vendor --------composer --------marcj --------autoload.php ----index.php

index.php

include 'vendor/autoload.php'; use RestService\Server;

Server::create('/admin', 'MyRestApi\Admin') ->collectRoutes() ->run();

MyRestApi/Admin.php

namespace MyRestApi;

class Admin { public function getLoggedIn(){ return $this->getContainer('auth')->isLoggedIn(); }

  public function postLogin($username, $password){
      return $this->getContainer('auth')->doLogin($username, $password);
  }

  public function getStats($server = '1'){
      return $this->getServerStats($server);
  }

}

Fatal error: Class 'MyRestApi\Admin' not found in /usr/local/www/apache22/data/webservice/vendor/marcj/php-rest-service/RestService/Server.php on line 597

I follow the installation instructions. What is wrong? Thanks.

ntvsx193 commented 11 years ago

You need add folder with namespace,

$loader = include __DIR__ . 'vendor/autoload.php';
$loader->add('MyRestApi\\'. __DIR__.'/');

I'm using other autoload manager.

jpl12 commented 11 years ago

Add:

$loader = include DIR . '/vendor/autoload.php'; $loader->add('MyRestApi',DIR.'/');

Now all responses empty.

ntvsx193 commented 11 years ago

What's in the error.log?

jpl12 commented 11 years ago

I wanted the latest changes. Now it works, basing on your example:

$loader = include DIR . '/vendor/autoload.php'; $loader->add('Api',DIR.'/');

RestService\Server::create('/v1', new Api\Router) ->setDebugMode(true) ->addGetRoute('stats', 'getStats') ->addGetRoute('stats/([0-9]+)', 'getStats') ->run();

OK GET /v1/stats OK GET /v1/stats/24

but, for the same queries with collectRoutes():

RestService\Server::create('/v1', new Api\Router) ->setDebugMode(true) ->collectRoutes() ->run();

OK GET /v1/stats Error GET /v1/stats/24

{ "status": 400, "error": "RouteNotFoundException", "message": "There is no route for 'stats\/24'." }

Any idea?. Thank you.

ntvsx193 commented 11 years ago

If you look carefully at the example, you'll see there using phpDocs in collectRoutes:

    /**
     * @param string $server
     * @url stats/([0-9]+)
     * @url stats
     * @return string
     */
    public function getStats($server = '1')
    {
        return sprintf('Stats for %s', $server);
    }

Then, it's will be work. GET /stats

{
    "status": 200,
    "data": "Stats for 1"
}

GET /stats/24

{
    "status": 200,
    "data": "Stats for 24"
}
jpl12 commented 11 years ago

Fixed, thanks.