Commit cc4b5758 authored by Igor Wiedler's avatar Igor Wiedler

[docs] better examples for the extensions chapter

parent e0bfc17f
...@@ -10,18 +10,20 @@ Loading extensions ...@@ -10,18 +10,20 @@ Loading extensions
In order to load and use an extension, you must register it In order to load and use an extension, you must register it
on the application. :: on the application. ::
use Acme\GodExtension; use Acme\DatabaseExtension;
$app = new Application(); $app = new Application();
$app->register(new GodExtension()); $app->register(new DatabaseExtension());
You can also provide some parameters as a second argument. You can also provide some parameters as a second argument.
:: ::
$app->register(new GodExtension(), array( $app->register(new DatabaseExtension(), array(
'god.deity' => 'thor', 'database.dsn' => 'mysql:host=localhost;dbname=myapp',
'database.user' => 'root',
'database.password' => 'secret_root_password',
)); ));
Included extensions Included extensions
...@@ -104,20 +106,20 @@ classes using the ``UniversalClassLoader``. ...@@ -104,20 +106,20 @@ classes using the ``UniversalClassLoader``.
As described in the *Services* chapter, there is an As described in the *Services* chapter, there is an
*autoloader* service that you can use for this. *autoloader* service that you can use for this.
Here is an example of how to use it:: Here is an example of how to use it (based on `Buzz <https://github.com/kriswallsmith/Buzz>`_)::
namespace Acme; namespace Acme;
use Silex\ExtensionInterface; use Silex\ExtensionInterface;
class GodExtension implements ExtensionInterface class BuzzExtension implements ExtensionInterface
{ {
public function register(Application $app) public function register(Application $app)
{ {
$app['god'] = $app->share(function() { ... }); $app['buzz'] = $app->share(function() { ... });
if (isset($app['god.class_path'])) { if (isset($app['buzz.class_path'])) {
$app['autoloader']->registerPrefix('God_', $app['god.class_path']); $app['autoloader']->registerNamespace('Buzz', $app['buzz.class_path']);
} }
} }
} }
...@@ -125,6 +127,12 @@ Here is an example of how to use it:: ...@@ -125,6 +127,12 @@ Here is an example of how to use it::
This allows you to simply provide the class path as an This allows you to simply provide the class path as an
option when registering the extension:: option when registering the extension::
$app->register(new GodExtension(), array( $app->register(new BuzzExtension(), array(
'god.class_path' => __DIR__.'/vendor/god/src', 'buzz.class_path' => __DIR__.'/vendor/buzz/lib',
)); ));
.. note::
For libraries that do not use PHP 5.3 namespaces you can use ``registerPrefix``
instead of ``registerNamespace``, which will use an underscore as directory
delimiter.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment