Introduction to Silex - A Symfony Micro-framework

Hi there,
first, thanks a lot for the great tutorial. I was able to follow it easily, but when It comes to the controller-grouping part, things stopped working (for me).

I’ve copied the mentioned folder structure 1:1, but when I try to “mount” the controller:

$app->mount(“/users”, new \MyApp\Controller\Provider\User());

I get the error

Fatal error: Class ‘MyApp\Controller\Provider\User’ not found

It’s not clear to me from where Silex “knows” that the controller-provider is in the folder src/MyApp/Controller/Provider/ (and - obvisiusly - it doesn’t know it in my case ;))

So - simple or not - some working source code would be really great. It can help a lot to find little mistakes and get things working.
Many Thanks!

usually you set up an autoloader for your project. if you use composer you define that in composer.json’s autoload property.

"autoload": {
    "psr-4": {
        "MyApp\\": "src/MyApp/"
    }
},

Hi Dormilich,

thanks a lot, that put me on the right track. This link was very helpful:

[EDIT:cant insert link because I get error “Sorry, new users can only put 2 links in a post.” but there was only one…strange.]

The User.php class can now be autoloaded, but gives me the following error:

fatal error: Interface ‘ControllerProviderInterface’ not found

So I’ve put

use Silex\ControllerProviderInterface; 

at the top of Users.php, and end up with this error:

Fatal error: Declaration of User::connect() must be compatible with Silex\ControllerProviderInterface::connect(Silex\Application $app)

The Users.php looks like the following:

<?php namespace MyApp; use Silex\ControllerProviderInterface; class User implements ControllerProviderInterface{ public function connect(Application $app) { $users = $app["controllers_factory"]; $users->get("/", "MyApp\\Controller\\UserController::index"); $users->post("/", "MyApp\\Controller\\UserController::store"); $users->get("/{id}", "MyApp\\Controller\\UserController::show"); $users->get("/edit/{id}", "MyApp\\Controller\\UserController::edit"); $users->put("/{id}", "MyApp\\Controller\\UserController::update"); $users->delete("/{id}", "MyApp\\Controller\\UserController::destroy"); return $users; } } ?>

I tried to change public function connect(Application $app) to public function connect(Silex\Application $app), doesnt change anything…

Any ideas? I really want to get this running :wink:
Again, many thanks!

try connect(\Silex\Application $app), otherwise it gets the MyApp namespace prepended.

Thanks again.
Tried this, but then I get the

Fatal error: Class ‘MyApp\Controller\Provider\User’ not found

Looks like I’m missing something crucial here… :confused:

not sure where you defined that. the user class above resolves to MyApp\User.

Hi again,
I messed up my namespaces. Correcting them solved my issues.
This article was quite helpfull:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.