Silex Application Unable to find Controller #919

Closed
russellseymour opened this Issue Mar 7, 2014 · 2 comments

Projects

None yet

2 participants

@russellseymour

Hello,

So that everyone knows I am cross posting this from Stack Overflow. (http://stackoverflow.com/questions/22247377/unable-to-find-controller-in-silex-application)

I have been developing an application using Silex for the past couple of weeks and last night I either made a change to my code or something was updated as part of updating composer but it will not work. I had not updated composer for about a week.

I realise that this might not be a Silex issue, but I cannot find out what the problem is when it was working so recently so any pointers are gratefully received.

I am using the 'Igorw\ConfigServiceProvider' to load up my routes which link to my configured controllers. But when I access the webpage I get the error message:

InvalidArgumentException: Unable to find controller "controllers.admin:index".

My files are as follows

composer.json

{
    "require": {
        "silex/silex": "1.2.*@dev",
        "igorw/config-service-provider": "1.2.*@dev",
        "symfony/yaml": "2.5.*@dev"
    },

    "autoload": {
        "psr-4": {
            "Turtle\\Controllers\\": "src/turtle/controllers"
        }
    }
}

config/routes.yml

config.routes:
  admin:
    pattern: /admin
    defaults: { _controller: 'controllers.admin:index' }
    method: GET

web/index.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use \Igorw\Silex\ConfigServiceProvider;
use \Turtle\Controllers\AdminController;

$app = new Silex\Application;

$app["debug"] = true;

// load the routes
$app -> register (new ConfigServiceProvider(__DIR__ . "/../config/routes.yml"));
foreach ($app["config.routes"] as $name => $route) {
    $app -> match($route["pattern"], $route["defaults"]["_controller"]) -> bind($name) -> method(isset($route["method"]) ? $route["method"] : "GET");
}

// register the classes
$app["controllers.admin"] = $app -> share(function($app) {
    return new AdminController($app);
});

$app -> run();

src/turtle/controllers/AdminController.php

<?php

namespace Turtle\Controllers;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;

class AdminController {

    protected $app;

    public function __construct(Application $app) {
        $this -> app = $app;
    }

    public function index (Request $request) {
        return "Hello World!";
    }

}

I have checked the $app variable and it contains an instantiated AdminController class, but for some reason the system is picking up the controller properly. I really do not understand what has happened and can only put it down to an obscure mistake or an update.

Can anyone shed any light on this please?

Thanks very much, Russell

Contributor

You probably need to register the service controller service provider

<?php

$app->register(new Silex\Provider\ServiceControllerServiceProvider());

@davedevelopment Thank you very much for spotting this
I have just added it to my test app and it now works.
Apologies for such a trivial issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment