我尝试了Silex中的示例,并将我的控制器放在一个单独的目录和类中.
默认情况下,控制器方法不会传递Request和Application对象.这适用于我的开发机器,它有5.3.14但不是默认的Ubuntu 5.3.2.它给了我:
PHP Catchable fatal error: Argument 1 passed to Sv\Controller\Index::index() must be an instance of Symfony\Component\HttpFoundation\Request, none given, called in /site/include/app/bootstrap.php on line 23 and defined in /site/include/app/Sv/Controller/Index.php on line 46
这是我的引导程序PHP:
require_once __DIR__ . '/../vendor/autoload.php';
use Sv\Repository\PostRepository;
use Sv\Controller;
$app = new Silex\Application();
// define global service for db access
$app['posts.repository'] = $app->share( function () {
return new Sv\Repository\PostRepository;
});
// register controller as a service
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app['default.controller'] = $app->share(function () use ($app) {
return new Controller\Index();
});
$app['service.controller'] = $app->share(function () use ($app) {
return new Controller\Service($app['posts.repository']);
});
// define routes
$app->get('/', 'default.controller:index');
$app->get('/next', 'default.controller:next');
$service = $app['controllers_factory'];
$service->get('/', "service.controller:indexJsonAction");
// mount routes
$app->mount('/service', $service);
// definitions
$app->run();
这是控制器代码:
namespace Sv\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
class Index
{
public function index(Request $request, Application $app)
{
return 'Route /index reached';
}
public function next(Request $request, Application $app)
{
return 'Route /next reached';
}
}
为什么这不起作用?
我希望这不是阻止我在PHP 5.3.2下使用ZF2的问题…
解决方法:
Silex需要PHP 5.3.3,如您在their composer.json中所见:
"require": {
"php": ">=5.3.3",
...
Silex works with PHP 5.3.3 or later.
这是因为Symfony2不再支持PHP 5.3.2.
标签:php,silex
来源: https://codeday.me/bug/20190529/1178773.html