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!
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);
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.