Skip to content

Commit

Permalink
it is now possible get access the route argument by using th same var…
Browse files Browse the repository at this point in the history
…iable name
  • Loading branch information
z7zmey committed Apr 2, 2016
1 parent 6ec1a76 commit 6a4d568
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# SlimFoundHandler

## Instalation
```
$ composer require z7zmey/slim-found-handler
```

## Application Configuration

```PHP
$app = new \Slim\App([
'foundHandler' => function () {
return new \z7zmey\SlimFoundHandler();
}
]);
```

## Examples

You may use only required parameters
```PHP
$app->get('/', function (\Slim\Http\Response $response) {
$response->getBody()->write("Hello");
return $response;
});
```

You can get access the route argument by using the same variable name
```PHP
$app->get('/example1/{name}', function ($name) {
echo "Hello, {$name}";
});
```

You can get access to all route arguments as array
```PHP
$app->get('/example2/{first}/{second}', function (array $routeArguments) {
echo "{$routeArguments['first']} {$routeArguments['second']}";
});
```

The sequence of parameters doesn't matter
```PHP
use \Slim\Http\Response;
use \Slim\Http\Request;

$routeHandler = function ($first, Response $res, array $params, Request $req) {
$second = $params['second'];
$third = $req->getAttribute('route')->getArgument('third');

$res->getBody()->write("{$first} {$second} {$third}");
return $res;
};

$app->get('/example3/{first}/{second}/{third}', $routeHandler);
```

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "z7zmey/slim-found-handler",
"description": "Adaptive Slim 3 found handler",
"description": "Slim 3 found handler",
"keywords": ["Slim", "SlimFramework", "Slim3", "FoundHandler"],
"type": "library",
"license": "BSD",
Expand All @@ -17,7 +17,7 @@
"issues": "https://github.com/z7zmey/SlimFoundHandler/issues"
},
"require": {
"php": ">=5.3",
"php": ">=5.5",
"slim/slim": "^3.0"
},
"require-dev": {
Expand Down
3 changes: 3 additions & 0 deletions src/SlimFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function __invoke(
} elseif ($class && $response instanceof $class->name) {
$args[] = $response;
continue;
} elseif (array_key_exists($parameterName = $parameter->getName(), $routeArguments)) {
$args[] = $routeArguments[$parameterName];
continue;
}

$args[] = null;
Expand Down

0 comments on commit 6a4d568

Please sign in to comment.