soutoner
soutoner

Reputation: 559

Phalcon Micro app doesn't route correctly with query strings

I'm trying to develop a RESTful API with a Micro app in Phalcon. Now I'm working in the social login with the Facebook SDK for PHP, but I find that when Facebook redirects me to my callback (with a query string inside obviously) the router doesn't find any handler method.

GET: http://localhost:8000/api/v1/register/facebook/callback?code=...&state=...#_=_

Not-Found handler is not callable or is not defined
#0 /home/adrian/PhpstormProjects/myproject/index.php(39): Phalcon\Mvc\Micro->handle()
#1 {main}

All my other routes work properly, but this is the first time I face query strings so I don't know if I'm doing something wrong.

I'm using Micro Collections for routing and the PHP built-in server.

This is my .htrouter.php. When I suppress the query string from $_SERVER['REQUEST_URI'] the route works correctly, but the Facebook SDK fails because it's expecting the parameters. I've tried to find if Phalcon saves the URL and the query string in different $_GET variables, but I think it's not the case.

<?php
if (!file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])) {
    $_GET['_url'] = $_SERVER['REQUEST_URI'];
}
return false;

My micro-collection.php

<?php

use \Phalcon\Mvc\Micro\Collection as MicroCollection;

$register = new MicroCollection();
$register->setHandler('App\Controllers\V1\RegisterController', true);
$register->setPrefix('/api/v1/register');
$register->get('/facebook', 'getAuthFacebook');
$register->get('/facebook/callback', 'facebookCallback');

return $register;

This is my RegisterController.php

<?php

namespace App\Controllers\V1;

use App\Controllers\ControllerBase;
use Phalcon\Http\Response;

class RegisterController extends ControllerBase
{
    public function getAuthFacebook()
    {
        $helper = $this->facebook->getRedirectLoginHelper();
        $permissions = ['email', 'user_likes'];
        $loginUrl = $helper->getLoginUrl('http://localhost:8000/api/v1/register/facebook/callback', $permissions);

        return new Response($loginUrl);
    }

    public function facebookCallback()
    {
        ....
    }
}

My index.php is exactly the same as the one that phalcon-devtools generates for a new project except that I mount my micro collections, so I think that the problem is not there.

Thank you in advance!

Upvotes: 2

Views: 1777

Answers (3)

accexs
accexs

Reputation: 108

Making my own implementation of a micro app, i did found that php version affects the behavior of the request url parsing. On php 5.6 i had to use the answer by @soutoner

For a request /service/endpoint?somefilter=something

$app->getRouter()->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

Without this dumping the request shows params as endpoint?somefilter=something

On php 7 dumps shows endpoint which is perfect

Interesting thing is that adding this doesn't brake the app on php 7 and makes the fix for php 5.6. So its a good tip for retro compatibility. Didn't tested on 7.2 but i suppose it would be the same.

Upvotes: 1

soutoner
soutoner

Reputation: 559

Finally, after a lot of research by the forums I've found the solution. I had to add this to my index.php:

Just after:

$app = new \Phalcon\Mvc\Micro($di);

This:

$app->getRouter()->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

Source

I don't know why it works, because as the documentation says, it changes the router behaviour in order to use $_SERVER['REQUEST_URI'] instead of $_GET['_url'] but that's actually what .htrouter.php does (or at least try to do).

Anyways, as I'm not very experienced with Phalcon, it's very likely that I've misunderstood something. Hope it helps.

Upvotes: 6

M2sh
M2sh

Reputation: 761

First of all check that the /facebook/callback works correctly or not?
This url : /facebook/callback
Then in your function use getQuery method :

public function facebookCallback() {
   if($this->request->getQuery('code') !== null) {
        $code = $this->request->getQuery('code');
        // your other codes here
   }
}

Upvotes: 0

Related Questions