我嘗試了Silex的例子,並把我的控制器放在一個單獨的目錄和類中。PHP 5.3.2的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:
<?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」' – 2013-04-10 16:58:39
您正在運行一個37個月大的PHP版本! :)發佈2010年3月.. – Evert 2013-04-10 17:10:16
@Evert嘗試更新Ubuntu 10.4到一個較新的版本,而無需自己編譯!想想在服務器上運行的應用程序噸非常聰明! – spankmaster79 2013-04-11 07:56:57