Skip to content

Commit 6d11bda

Browse files
authored
Update usage with DI container (#323)
1 parent 8bc5ef8 commit 6d11bda

1 file changed

Lines changed: 33 additions & 26 deletions

File tree

README.md

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,60 +20,70 @@ Requires Slim Framework 4, Twig 3 and PHP 7.4 or newer.
2020

2121
## Usage
2222

23+
### With DI Container
24+
2325
```php
2426
use DI\Container;
2527
use Slim\Factory\AppFactory;
2628
use Slim\Views\Twig;
2729
use Slim\Views\TwigMiddleware;
2830

29-
require __DIR__ . '/vendor/autoload.php';
31+
require __DIR__ . '/../vendor/autoload.php';
3032

3133
// Create Container
3234
$container = new Container();
33-
AppFactory::setContainer($container);
3435

3536
// Set view in Container
36-
$container->set('view', function() {
37-
return Twig::create('path/to/templates', ['cache' => 'path/to/cache']);
37+
$container->set(Twig::class, function() {
38+
return Twig::create(__DIR__ . '/../templates', ['cache' => 'path/to/cache']);
3839
});
3940

40-
// Create App
41-
$app = AppFactory::create();
41+
// Create App from container
42+
$app = AppFactory::createFromContainer($container);
4243

4344
// Add Twig-View Middleware
44-
$app->add(TwigMiddleware::createFromContainer($app));
45+
$app->add(TwigMiddleware::create($app, $container->get(Twig::class)));
4546

46-
// Define named route
47+
// Add other middleware
48+
$app->addRoutingMiddleware();
49+
$app->addErrorMiddleware(true, true, true);
50+
51+
// Render from template file templates/profile.html.twig
4752
$app->get('/hello/{name}', function ($request, $response, $args) {
48-
return $this->get('view')->render($response, 'profile.html', [
49-
'name' => $args['name']
50-
]);
53+
$viewData = [
54+
'name' => $args['name'],
55+
];
56+
57+
$twig = $this->get(Twig::class);
58+
59+
return $twig->render($response, 'profile.html.twig', $viewData);
5160
})->setName('profile');
5261

5362
// Render from string
5463
$app->get('/hi/{name}', function ($request, $response, $args) {
55-
$str = $this->get('view')->fetchFromString(
56-
'<p>Hi, my name is {{ name }}.</p>',
57-
[
58-
'name' => $args['name']
59-
]
60-
);
64+
$viewData = [
65+
'name' => $args['name'],
66+
];
67+
68+
$twig = $this->get(Twig::class);
69+
$str = $twig->fetchFromString('<p>Hi, my name is {{ name }}.</p>', $viewData);
6170
$response->getBody()->write($str);
71+
6272
return $response;
6373
});
6474

6575
// Run app
6676
$app->run();
6777
```
6878

69-
### Without container
79+
### Without DI container
7080

7181
```php
7282
use Slim\Factory\AppFactory;
7383
use Slim\Views\Twig;
7484
use Slim\Views\TwigMiddleware;
7585

76-
require __DIR__ . '/vendor/autoload.php';
86+
require __DIR__ . '/../vendor/autoload.php';
7787

7888
// Create App
7989
$app = AppFactory::create();
@@ -87,7 +97,7 @@ $app->add(TwigMiddleware::create($app, $twig));
8797
// Define named route
8898
$app->get('/hello/{name}', function ($request, $response, $args) {
8999
$view = Twig::fromRequest($request);
90-
return $view->render($response, 'profile.html', [
100+
return $view->render($response, 'profile.html.twig', [
91101
'name' => $args['name']
92102
]);
93103
})->setName('profile');
@@ -102,6 +112,7 @@ $app->get('/hi/{name}', function ($request, $response, $args) {
102112
]
103113
);
104114
$response->getBody()->write($str);
115+
105116
return $response;
106117
});
107118

@@ -114,24 +125,20 @@ $app->run();
114125
`TwigExtension` provides these functions to your Twig templates:
115126

116127
* `url_for()` - returns the URL for a given route. e.g.: /hello/world
117-
* `full_url_for()` - returns the URL for a given route. e.g.: http://www.example.com/hello/world
128+
* `full_url_for()` - returns the URL for a given route. e.g.: https://www.example.com/hello/world
118129
* `is_current_url()` - returns true is the provided route name and parameters are valid for the current path.
119130
* `current_url()` - returns the current path, with or without the query string.
120131
* `get_uri()` - returns the `UriInterface` object from the incoming `ServerRequestInterface` object
121132
* `base_path()` - returns the base path.
122133

123134
You can use `url_for` to generate complete URLs to any Slim application named route and use `is_current_url` to determine if you need to mark a link as active as shown in this example Twig template:
124135

125-
```php
126-
{% extends "layout.html" %}
127-
128-
{% block body %}
136+
```html
129137
<h1>User List</h1>
130138
<ul>
131139
<li><a href="{{ url_for('profile', { 'name': 'josh' }) }}" {% if is_current_url('profile', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li>
132140
<li><a href="{{ url_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li>
133141
</ul>
134-
{% endblock %}
135142
```
136143

137144
## Tests

0 commit comments

Comments
 (0)