Closed ntvsx193 closed 11 years ago
I don't get the question. What do you mean?
So, look.
I have done without a controller
Server::create('/')
->addGetRoute('stats/([0-9]+)', function($id){
// .. do something
})
But I decided to use the Auto-Collection
Server::create('/', new MyNameSpace\MyController)
->collectRoutes()
->run();
// Controller
namespace Host;
class Router {
public function getStats(){
$data = \DB::Select("SELECT * FROM stats");
return ['stats' => $data];
}
public function getStatsInt(int $id = null){
if (!$id)
return false;
$data = \DB::Select("SELECT * FROM stats WHERE id = $id");
return ['stats' => $data];
}
}
Query execution + GET /stats
However, request + GET /stats/183
return error with message There is no route for 'stats\/183'.
How do I make a controller method to get the request? + GET /stats/183
Well, that is not possible through the collection mode. But you can add rules ->collectRoutes()
call.
Shit. Even the idea to implement, but it quickly failed. Thank you.
Yeah, a idea would be to make it through annotations. But thats not possible right now, but seems as a good idea.
/**
*
* @param string $param1
* @url stats/([0-9]+)
*/
public function getStats($param1 = '') {
}
give me a minute, I'll implement it quick :-P
Okay :+1:
Well, done. You can try it out as in the test class https://github.com/marcj/php-rest-service/blob/master/Test/Controller/MyRoutes.php#L17
It's really cool, but I do not work :(
Response is There is no route for 'stats\/1'.
for request GET /stats/1
I see more errors in error.log
how looks your method with the phpDoc?
public function getStats($server = 0){
return sprintf("get id is: %s", $server);
}
Errors with a single request GET /stats/123
2013/08/27 20:08:45 [error] 31608#0: *335 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined offset: 1 in /foldertowww/lib/RestService/Server.php on line 1042
PHP message: PHP Notice: Undefined index: param in /foldertowww/lib/RestService/Server.php on line 978
PHP message: PHP Warning: key() expects parameter 1 to be array, null given in /foldertowww/lib/RestService/Server.php on line 992
PHP message: PHP Warning: Invalid argument supplied for foreach() in /foldertowww/lib/RestService/Server.php on line 996
PHP message: PHP Notice: Undefined offset: 1 in /foldertowww/lib/RestService/Server.php on line 1042
PHP message: PHP Notice: Undefined index: param in /foldertowww/lib/RestService/Server.php on line 978
PHP message: PHP Warning: key() expects parameter 1 to be array, null given in /foldertowww/lib/RestService/Server.php on line 992
PHP message: PHP Warning: Invalid argument supplied for foreach() in /foldertowww/lib/RestService/Server.php on line 996
PHP message: PHP Notice: Undefined offset: 1 in /foldertowww/lib/RestService/Server.php on line 1042
PHP message: PHP Notice: Undefined index: param in /foldertowww/lib/RestService/Server.php on line 978
PHP message: PHP Warning: key() expects parameter 1 to be array, null given in /foldertowww/lib/RestService/Server.php on line 992
PHP message: PHP Warning: Invalid argument supplied for foreach() in /foldertowww/lib/RestService/Server.php on line 996
PHP message: PHP Notice: Undefined offset: 1 in /foldertowww/lib/RestService/Server.php on line 1042
PHP message: PHP Notice: Undefined index:
I'm tested on dev server with nginx + php-fpm 5.5.1-1
Ah, it seems you don't have a docBlock above your method?
Oh, I had not noticed. :/ However, my error log shit hard!
I've updated it again to suppress more notices for methods without docBlock.
Nice work! Thank you! :+1:
Does it work now?
No, it works and how! I'll add to the readme for installation of nginx. 2 min
How can i use the controller with no custom rules?
It's controller with custom rules
It's auto convert routes
How do I get a class method? from
to