简体   繁体   English

Phalcon中的微型集合(类的对象不能转换为字符串)

[英]Micro Collections in Phalcon (Object of class can't be converted to string)

I am trying to make a micro API in Phalcon that uses collections so I can separate logic into controllers. 我试图在Phalcon中制作一个使用集合的微型API,以便可以将逻辑分离为控制器。

use Phalcon\DI\FactoryDefault,
   Phalcon\Mvc\Micro,
   Phalcon\Mvc\Micro\Collection as MicroCollection;

// Use Loader() to autoload our model
$loader = new \Phalcon\Loader();

$loader->registerNamespaces(array(
  'BookApp\Controllers' => __DIR__ . '/bookapp/controllers/',
  'BookApp\Models' => __DIR__. '/bookapp/models/',
  'BookApp' => __DIR__ . '/bookapp/'
));

$loader->register();

....

$app = new Micro();
$app->setDI($di);

$users = new MicroCollection();
$users->setHandler(new \BookApp\Controllers\UserController(), true);
$users->setPrefix('/user');
$users->post('/', 'register');
$users->get('/{id}', 'getUser');
$users->delete('/{id}', 'deleteUser');
$users->put('/{id}', 'updateUser');
$users->post('/{code}/confirm', 'confirmEmail');
$users->post('/{id}/reset', 'resetPassword');
$app->mount($users);

This issue is on the $app->mount(). 此问题在$ app-> mount()上。 It is telling me it needs a collection from not \\Phalcon\\Mvc\\Micro\\Collection but a \\Phalcon\\Mvc\\Collection. 它告诉我它需要的不是\\ Phalcon \\ Mvc \\ Micro \\ Collection,而是\\ Phalcon \\ Mvc \\ Collection。 Yet the latter does not have the needed methods. 但是后者没有所需的方法。 If I try the above code I get: 如果尝试上面的代码,我会得到:

Catchable fatal error: Object of class BookApp\Controllers\UserController could not be converted to string

Thanks for any help anyone can give. 感谢您提供任何帮助。

The documentation of Class Phalcon\\Mvc\\Micro\\Collection states that the $handler argument is of type mixed : Class Phalcon\\Mvc\\Micro\\Collection的文档指出$handler参数的类型为mixed

public Phalcon\Mvc\Micro\CollectionInterface setHandler (mixed $handler, [boolean $lazy])

    |Sets the main handler

Try handing the name of your Controller as a string. 尝试将控制器的名称作为字符串处理。 Since you want lazy loading (2nd argument) there is no need to create an instance of the controller while registering it. 由于您要延迟加载(第二个参数),因此在注册控制器时无需创建该控制器的实例。

$users->setHandler('\BookApp\Controllers\UserController', true);

According to the documentation , you can either pass an object to setHandler , or a string name with true for the second parameter for lazy-loading. 根据文档 ,您可以将对象传递给setHandler ,也可以将第二个参数的true的字符串名称进行延迟加载。 You appear to have combined the two, so it's trying to resolve your object as a name to load. 您似乎已将两者结合在一起,因此它正在尝试将对象解析为要加载的名称。

Either pass the name of the class as a string, or remove the second parameter from your setHandler call. 可以将类的名称作为字符串传递,或者从setHandler调用中删除第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM