【发布时间】:2013-04-10 16:11:10
【问题描述】:
我尝试了 Silex 中的示例并将我的控制器放在单独的目录和类中。
默认情况下,控制器方法不会获取Request 和Application 对象。这适用于我的开发机器,它有 5.3.14,但不适用于默认的 Ubuntu 5.3.2。它给了我:
PHP 可捕获的致命错误:传递给 Sv\Controller\Index::index() 的参数 1 必须是 Symfony\Component\HttpFoundation\Request 的实例,没有给出,在 /site/include/app/bootstrap.php 中调用在第 23 行并在第 46 行的 /site/include/app/Sv/Controller/Index.php 中定义
这是我的引导 PHP:
<?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 composer.json,需求部分:
"php": ">=5.3.3" -
您正在运行一个 37 个月大的 PHP 版本! :) 2010 年 3 月发布..
-
@Evert 尝试在不自己编译的情况下将 Ubuntu 10.4 更新到更新版本!!!想想在服务器上运行的大量应用程序......非常聪明!
-
Servergrove 为 php 提供了 ubuntu 存储库,现在它位于 php5.4.6,请查看 this link。不过,我不确定是否支持 10.04。
-
@spankmaster79:我找到了这个 ubuntu 页面,这似乎也是我使用的过程:help.ubuntu.com/community/UpdatingADeb 这样做的好处,而不是手动编译自己,是你得到一个'真正的 .deb 以您期望的方式完全集成到 Ubuntu 中。