Introduction to Silex - A Symfony Micro-framework

Correct, but it’s not feasible to supply a configuration file for every server, given that every reader may put his app in a different location and inside that location the index.php file into a different subfolder. It’s an interesting thought, though. Maybe we should have a post describing the process of configuring Nginx and Apache for all the most popular frameworks and just link to it every time.

I think that if you need a .htaccess file, then you should really be looking at tutorials for that. This is explaining about Silex and therefore shouldn’t need any explanation about NGINX or Apache settings.

This is a pretty good tutorial and I feel that it doesn’t need any explanation of where you should point your server’s config files or place your htaccess as everyone does work differently.

If you were to look at any documentation for Laravel, Symfony, Silex etc, you will not find anything which goes into detail about how you do this as it is completely up to the user.

Nope. Not at all. A tutorial, is to have the reader enabled to run it and try it, with not much additional external reference.

SF2 comes with a .htaccess file so it is not right to say find nothing on the details.

In particular, this tutorial says it does not like to use web folder as the entry folder, and changed to public. So it is worth mentioning.

I would like to see an article like this written for Slim as well. One of my favorite CMSes is likely moving to Slim so I may start using it in projects from now on.

It’s planned : )

at first I thought it was Slim by the router styling.

Very nice article.
Where can I download the working files from the Silex example?

Thanks, I didn’t put the example on Github because it doesn’t contain too much code.

Yeah, it wasn’t anything too “publishable”, being so simple. Just follow the instructions and you should have an identical copy within 5 minutes.

Note on some Service providers:

If you use the Validation Service and the Security Service, you need to register the Validation Service first, otherwise the user password validator doesn’t get added.

1 Like

Cool, thanks for the heads up

It would also make sense to extend Silex’s ControllerResolver to inject the DI container when creating router classes, as the container is (at that point) already available.

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.