4
votes

I tried the examples from Silex and put my Controllers in a seperate directory and class.

No the controller method will get the Request and Application objects passed by default. This works on my dev machine which has 5.3.14 but not on the default Ubuntu 5.3.2. It give me:

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

Here's my bootstrap 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();

and here's the Controller code:

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';
    }
}

Why does this not work?

I hope this is not the same problem that prevents me from using ZF2 under PHP 5.3.2...

1
The answer lies in silex composer.json, requirements section: "php": ">=5.3.3" - dev-null-dweller
You're running a PHP version that's 37 months old! :) Released March 2010.. - Evert
@Evert try updating Ubuntu 10.4 to a newer version without compiling it yourself!!! And think about tons af applications running on the server.. very smart! - spankmaster79
Servergrove provides ubuntu repositories for php, right now it's at php5.4.6, check out this link. I'm not sure if 10.04 is supported, though. - Maerlyn
@spankmaster79: I found this ubuntu page, which appears to be the process I use too: help.ubuntu.com/community/UpdatingADeb The benefit of doing it this way, rather than manually compiling yourself, is that you get a 'real' .deb which integrates exactly in Ubuntu the way you expect. - Evert

1 Answers

6
votes

Silex requires PHP 5.3.3, as you can see in their composer.json:

"require": {
    "php": ">=5.3.3",
    ...

And it also stated in the README file:

Silex works with PHP 5.3.3 or later.

This is due to the fact that Symfony2 is no longer supporting PHP 5.3.2.