Implement Laravel Facade to Silex
Last post, while I understanding how does laravel work behind, I said that I guess I can implement laravel facade to silex since they have similiar inner structure. And I just did it. This is how.
first, of course we need them both;
1{
2 "require": {
3 "php": ">=5.3.0",
4 "silex/silex": "1.*",
5 "illuminate/foundation": "4.0.*",
6 "illuminate/support": "4.0.*"
7 }
8}
and then lets do it. mmmm… hold on, other things that most important is Alias Loader, facade is just a class, and alias loader let us to naming our facades.
For now, we will only work with Twig, but i am pretty sure it’s more than enough to let you understand and then you can implement this to other Service Provider that compatible with Silex. Here it is.
1 '\Facades\Twig',
2 ))->register();
Then we have to create \Facades\Twig.php some where that accessed by composer;
Optionally for this article, you can create views/hello.twig and put anything on its file. e.g ‘Hello World !’ or whatever. Next we can create the silex application, don’t forget to register TwigServiceprovider since we need it.
1//create new application
2$app = new \Silex\Application();
3
4//register twig
5$app->register(new Silex\Provider\TwigServiceProvider(), array(
6 'twig.path' => __DIR__.'/views',
7 ));
By now, instead of using $app['twig']->render(), you can use laravel style: Twig::render().
1 $app->get('/', function() use ($app) {
2
3 return Twig::render('hello.twig');
4 });
5
6 $app->run();