Introduction to Silex - A Symfony Micro-framework

article
#1

Originally published at: http://www.sitepoint.com/introduction-silex-symfony-micro-framework/

Silex is a PHP micro-framework based on Symfony components and inspired by the Sinatra Ruby framework. In this article, we are going to get started with the framework and see the how it fits our needs.

Logo

Installation

The best and recommended way to install Silex is through composer:

// composer.json
{
    "require": {
        "silex/silex": "1.3.*@dev",
        "twig/twig": "1.17.*@dev"
    },
    "require-dev": {
        "symfony/var-dumper": "dev-master"
    }
}

Run composer update --dev to load the dependencies and generate the autoloader. We also required twig because we want to use it as our template engine, and the new var-dumper from Symfony as a development dependency – read more about it here.

Creating a Folder Structure

One of the things I like about Silex is that it gives you a bare bones framework that you can organize in any way you want.

Continue reading this article on SitePoint

#2

Very nice article. Lately I have been looking into Slim and Silex and taking a break from the larger frameworks. This article cleared up many questions I had concerning controller providers and how to use them. Thank you very very much and PLEASE make another Silex article.

Best Regards,
TJ Nine

1 Like
#3

I have a really basic problem setting this up. I can't manage to make the .htaccess working properly. I have the folder structure as in the example above, when I point the browser lo localhost/silex/public it shows the Hello World route just fine. But when I go to the localhost/silex/public/user/2 it says that there is no route found. Changing the #RewriteBase doesn't help. Can you please explain how to make this work so the app can be in a separate folder (I was only able to make it work when the index.php was in the main project's folder, not in the /public subfolder)?

#4

Ideally, your apache virtual host should be pointing to your public folder, and the .htaccess file will use index.php as the main controller.
Try public/index.php/users/1. You can visit the doc for more info about configuring your web server.

1 Like
#5

There will be more : )

#6

You see, that is what I mentioned in the Github PR. .htaccess is of great importance.

#7

Only with Apache. This is exactly why I prefer demos to be shown on Homestead Improved and similar unified environments which make sure nothing can go wrong, and why I'd rather see PHP's built-in server used for hosting demo apps locally than nginx and apache - server configuration is outside the scope of code demos, and only causes confusion due to the vast differences among readers' machines and configurations.

#8

Agreed but not totally agreed. For the purpose of this particular article, the right .htaccess is missing or not properly listed, which causes the confusion to one reader.

It will be ideal to have a unified environment but we have to agree that no one is actually using PHP built-in server to run a production app...

#9

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.

#10

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.

#11

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.

#12

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.

#13

It's planned : )

#14

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

#15

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

#16

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

#17

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

#18

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
#19

Cool, thanks for the heads up

#20

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.