We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon post static function

Good day!

I'm writting backend on Phalcon with JWT. But have some problem.

When i trying test with this code

class AuthController extends RESTController {

    public function getToken() {
        $request = $this->di->getShared('request');
    }
    ...

I've got an error

*2016/10/08 14:12:00 [error] 5688#5688: 398 FastCGI sent in stderr: "PHP message: PHP Warning: non-static method MyRest\Controllers\AuthController::getToken() should not be called statically**

my routes

return call_user_func(function() {

    $AuthCollection = new \Phalcon\Mvc\Micro\Collection();

    $AuthCollection
        ->setPrefix('/v1/auth')
        ->setHandler('\MyRest\Controllers\AuthController');

    $AuthCollection->options('/', 'optionsBase');
    $AuthCollection->options('/{id}', 'optionsOne');

    $AuthCollection->post('/', 'getToken');

    return $AuthCollection;
});

With static function OK.

namespace MyRest\Controllers;

use MyRest\Exceptions\HTTPException;
use MyRest\Models\Auth;
use MyRest\JWT\JWT;
use Phalcon\Http\Request as Request;
use Phalcon\Http\Response as Response;

class AuthController extends RESTController {

    public static function getToken() {
        $request = new Request();
        $response = new Response();
        $params = $request->getJsonRawBody();
        $response->setJsonContent(array('error'=>'No params'));
        if(isset($params->{'email'}) && isset($params->{'pwd'})) {
            if($params->{'email'} == '[email protected]' && $params->{'pwd'} == 'mypwd') {
                $user = (object) array(
                    'iat' => 1356999524,
                    'nbf' => 1357000000
                );
                $user->name = 'User';
                $user->mail = '[email protected]';
                $response->setJsonContent(array('token' => JWT::encode($user, JWT::options()['secret'])));
            } else {
                $response->setJsonContent(array('error' => 'Wrong!'));
            }
        }
        return $response;
    }
}

Check

[email protected]:/var/log/nginx$ curl -i -X POST -d '{"email":"[email protected]", "pwd":"mypdw"}' https://localhost:5000/v1/auth
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 08 Oct 2016 11:42:11 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, Authorization
Access-Control-Allow-Credentials: 1
E-Tag: b8d362e919cb7a83e30bc79fa68d5c6c

{"_meta":{"status":"SUCCESS","count":1},"records":{}}{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjEzNTY5OTk1MjQsIm5iZiI6MTM1NzAwMDAwMCwibmFtZSI6IkRlbmlzIiwibWFpbCI6ImRlbmlzLmtvcm5leUBsaWRza29lLmJ5In0.e8LnEVnGw8OwOwsNRGDEaNWI_hkGsbK-zjxVXXD8v2E"}

How i can use not static function? Look at my menu controller

namespace MyRest\Controllers;

use MyRest\Exceptions\HTTPException;
use MyRest\Models\Menu;

class MenuController extends RESTController {

    public $menu = array();

    public function __construct() {
        $this->menu = new Menu();
    }

    public function getMenu($id) {
        $results = $this->menu->getMenu($id);
        return $this->respond($results);
    }   
}

Not static function and everything OK. Why need using static when POST? All difference in auth route GET, in auth - POST.



2.8k
Accepted
answer

I forgotten add lazy load in my routes :) It is ok.