Skip to content

Commit

Permalink
💥 Disable method injection by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jan 14, 2019
1 parent 87784f2 commit f098f38
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
32 changes: 30 additions & 2 deletions README.md
Expand Up @@ -29,7 +29,7 @@ $app->get('/hello/{name}', function (Request $request, Response $response, array
$app->run();
```

Behind the scenes a Slim application is bootstrapped by adding all of the required Slim components to League's container.
Behind the scenes, a Slim application is bootstrapped by adding all of the required Slim components to League's container.

## Service Providers

Expand Down Expand Up @@ -96,6 +96,34 @@ $app->getContainer()->get(\Slim\Settings::class)->set('displayErrorDetails', tru

Read more about the available configuration options [here](https://www.slimframework.com/docs/v3/objects/application.html#slim-default-settings).

# Routes

By default, Lean will pass the `Request`, `Response` and route parameters to your routes. Alternatively, you can access the route parameters through the `getAttribute` method of the request.

```php
$app->get('/books/{id}', function (Request $request, Response $response, array $args) {
$id = $args['id'];
// Or
$id = $request->getAttribute('id');
});
```

Read more about routes [here](http://www.slimframework.com/docs/v3/objects/router.html).

# Method Injection

Method injection allows you to define dependencies on a method level, rather than in the constructor (similar to the Laravel framework). If you want to enable method injection you can enable setting the `methodInjection` setting:

```php
$app->getContainer()->get(\Slim\Settings::class)->set('methodInjection', true);

$app->get('/books/{id}', function (Request $request, string $id) {
// ...
});
```

Route attributes are only available as method parameters and will not be accessible through the `getAttribute` method on the request object.

## Error Handlers

By default, Lean uses Slim's error handlers. There are different ways to implement an error handler for Slim, read more about them [here](https://www.slimframework.com/docs/v3/handlers/error.html).
Expand Down Expand Up @@ -124,7 +152,7 @@ $app->getContainer()->share('errorHandler', function () {
});
```

Ideally you would put this code inside a service provider. Read more about service providers above.
Ideally, you would put this code inside a service provider. Read more about service providers above.

## Testing

Expand Down
8 changes: 7 additions & 1 deletion src/Lean/SlimDefinitionAggregateBuilder.php
Expand Up @@ -13,6 +13,7 @@
use Slim\Handlers\NotAllowed;
use Slim\Handlers\NotFound;
use Slim\Handlers\PhpError;
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
Expand Down Expand Up @@ -48,6 +49,7 @@ class SlimDefinitionAggregateBuilder
'displayErrorDetails' => false,
'addContentLengthHeader' => true,
'routerCacheFile' => false,
'methodInjection' => false,
];

public static function build(ContainerInterface $container): DefinitionAggregateInterface
Expand Down Expand Up @@ -89,7 +91,11 @@ public static function build(ContainerInterface $container): DefinitionAggregate
}, true);

$aggregate->add('foundHandler', function () use ($container) {
return new MethodInjection($container);
if ($container->get('settings')['methodInjection']) {
return new MethodInjection($container);
}

return new RequestResponse();
}, true);

$aggregate->add('phpErrorHandler', function () use ($container) {
Expand Down

0 comments on commit f098f38

Please sign in to comment.