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

Use the controller with no custom rules #2

Closed ntvsx193 closed 11 years ago

ntvsx193 commented 11 years ago

How can i use the controller with no custom rules?

It's controller with custom rules

use RestService\Server;

Server::create('/', new Host\Router)
    ->setDebugMode(true) 
    ->addGetRoute('stats/([0-9]+)', 'getStats')

It's auto convert routes

use RestService\Server;

Server::create('/', 'Host\Router')
    ->collectRoutes()
->run();

// Router class
namespace Host;

class Router {

    public function getStats(int $server_id = null){
        if ($server_id) {
            // .. get more info by server_id
            // return [..data..];
        }
        // ... get list stats
        // return [..data..];
    }

}

How do I get a class method? from

->addGetRoute('stats/([0-9]+)', 'getStats')

to

public function getStats(int $server_id = null) ...
marcj commented 11 years ago

I don't get the question. What do you mean?

ntvsx193 commented 11 years ago

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

marcj commented 11 years ago

Well, that is not possible through the collection mode. But you can add rules ->collectRoutes() call.

ntvsx193 commented 11 years ago

Shit. Even the idea to implement, but it quickly failed. Thank you.

marcj commented 11 years ago

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 = '') {

}
marcj commented 11 years ago

give me a minute, I'll implement it quick :-P

ntvsx193 commented 11 years ago

Okay :+1:

marcj commented 11 years ago

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

ntvsx193 commented 11 years ago

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

marcj commented 11 years ago

how looks your method with the phpDoc?

ntvsx193 commented 11 years ago
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:
ntvsx193 commented 11 years ago

I'm tested on dev server with nginx + php-fpm 5.5.1-1

marcj commented 11 years ago

Ah, it seems you don't have a docBlock above your method?

ntvsx193 commented 11 years ago

Oh, I had not noticed. :/ However, my error log shit hard!

marcj commented 11 years ago

I've updated it again to suppress more notices for methods without docBlock.

ntvsx193 commented 11 years ago

Nice work! Thank you! :+1:

marcj commented 11 years ago

Does it work now?

ntvsx193 commented 11 years ago

No, it works and how! I'll add to the readme for installation of nginx. 2 min