Closed jpl12 closed 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.
Add:
$loader = include DIR . '/vendor/autoload.php'; $loader->add('MyRestApi',DIR.'/');
Now all responses empty.
What's in the error.log?
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.
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"
}
Fixed, thanks.
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(); }
}
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.