$ curl -sS https://getcomposer.org/installer | php{
"name": "neusta/hello-silex",
"require": {
"php": ">=5.3.3",
"silex/silex": "~1.2"
}
}$ php composer.phar install
oder
$ composer install$ composer create-project fabpot/silex-skeleton/* Add session support */
$app->register(new Silex\Provider\SessionProvider());
/* Save something in the session */
$app['session']->set('user', ['username' => 'johndoe']);
/* Fetch something in the session */
$user = $ap['session']->get('user');/* Add twig support */
$app->register(new Silex\Provider\TwigServiceProvider(), [
'twig.path' => __DIR__ . '/../templates',
]);
/* Add controller to the app */
$app->get('/', function() use ($app) {
return $app['twig']->render(
'index.html.twig',
['foo' => 'bar']
);
})->bind('homepage');/* Add doctrine dbal support */
$app->register(new Silex\Provider\DoctrineServiceProvider(),
[
'db.options' => array(
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/app.db',
],
));
/* Execute query */
$data = $app['db']->fetchAssoc(
'SELECT * FROM table WHERE id = :id',
['id' => 1]
);$app->get('/todo/{id}', function ($id) use ($app) {
$sql = 'SELECT * FROM todo WHERE id = :id';
$todo = $app['db']->fetchAssoc(
$sql,
['id' => (int)$id]
);
if (!$todo) {
$app->abort(
404,
sprintf('Todo %s does not exists.', $id)
);
}
return $app->json($todo);
});$ ./composer.phar require pimple/pimple ~3.0
oder sogar als PHP C Extension
$ cd ext/pimple
$ phpize
$ ./configure
$ make
$ make installuse Pimple\Container;
$container = new Container();// define some services
$container['session_storage'] = function ($c) {
return new SessionStorage('SESSION_ID');
};
$container['session'] = function ($c) {
return new Session($c['session_storage']);
};
// get the session object
$session = $container['session'];Singleton
$container['session'] = $container->factory(function ($c) {
return new Session($c['session_storage']);
});Neue Instanzen
// define some parameters
$container['cookie_name'] = 'SESSION_ID';
$container['session_storage_class'] = 'SessionStorage';
// and use it
$container['session_storage'] = function ($c) {
return new $c['session_storage_class']($c['cookie_name']);
};$container['random_func'] = $container->protect(function () {
return rand();
});$container['session_storage'] = function ($c) {
return new $c['session_storage_class']($c['cookie_name']);
};
$container->extend('session_storage', function ($storage, $c) {
$storage->...();
return $storage;
};$container['session'] = function ($c) {
return new Session($c['session_storage']);
};
$sessionFunction = $container->raw('session');use Pimple\Container;
class FooProvider implements Pimple\ServiceProviderInterface
{
public function register(Container $pimple)
{
$pimple['foo'] = 'bar'
}
}
// ... use it
$pimple->register(new FooProvider());use Pimple\Container as Pimple;
class MyContainer extends Pimple
{
public function __construct() {
parent::__construct([
'foo' => 'bar'
]);
$this->register(new FooProvider());
}
}
interface ServiceProviderInterface
{
// executed after registering the Provider
public function register(Application $app);
// executed on $app->run();
public function boot(Application $app);
}