EDIT:
Okay, I've figured out what the error related to the handler was, at this point I just need help getting my directory autoloader to work(the part of the code that's commented out), I'm on XAMPP under Windows, if that might play a role, I've tried absolute paths, relative paths, verified that __DIR__
points where I think it should, reversing the slashes(with escaping), and haven't gotten anything to load the classes automatically.
Using $loader->registerClasses
works fine, so I know that the class is named properly and works, but for some reason it's not detected by the registerDirs
autoloader.
Original Post:
So my directory structure is pretty simple:
/
controllers/
users.php
api.php
My code is taken almost verbatim from the Micro section of the docs:
api.php:
<?php
$loader = new \Phalcon\Loader();
/*$loader->registerDirs(
[
__DIR__ . "/models/",
__DIR__ . "/controllers/"
]
)->register();*/
$loader->registerClasses(
[
"UsersController" => "controllers/users.php",
]
)->register();
$app = new \Phalcon\Mvc\Micro();
use Phalcon\Mvc\Micro\Collection as MicroCollection;
// Users handler
$users = new MicroCollection();
$users->setHandler(new UsersController(), true);
$users->setPrefix('/users');
$users->get('/get/{id}', 'get');
$users->get('/add/{payload}', 'add');
$app->mount($users);
$app->after(
function () use ($app) {
$app->response->setContentType('application/json');
$app->response->sendHeaders();
echo json_encode($app->getReturnedValue());
}
);
try {
$app->handle();
} catch (\Exception $e) {
echo "Exception: ", $e->getMessage();
}
controllers/users.php:
<?php
use Phalcon\Mvc\Controller;
class UsersController extends Controller
{
public function index()
{
return ["hai", "test"];
}
public function get($id)
{
return [$id, "testget"];
}
public function add($payload)
{
return [$payload, "testadd"];
}
}
For one, I've commented out the Directory loading, because the UsersController class was failing to load, so I've loaded it directly just for testing.
Second, now that it IS loading, I get this error, regardless of what page I visit (/users, /, whatever):
Fatal error: Uncaught InvalidArgumentException: Parameter 'definition' must be a string in C:\Code\stud\xampp\htdocs\api.php:30 Stack trace: #0 [internal function]: Phalcon\Mvc\Micro\LazyLoader->__construct(Object(UsersController)) #1 C:\Code\stud\xampp\htdocs\api.php(30): Phalcon\Mvc\Micro->mount(Object(Phalcon\Mvc\Micro\Collection)) #2 {main} thrown in C:\Code\stud\xampp\htdocs\api.php on line 30
Anyone know why this isn't working? I'm using PHP 7.1.4 on XAMPP with Phalcon 3.2, I've tried using forward slashes(Windows problem?) and removing the first / to fix the autoloader, but still the class doesn't load, and I don't have any idea how to fix the above error.