# How to check if route has placeholder

**URL:** <https://discourse.slimframework.com/t/how-to-check-if-route-has-placeholder/5737>\
**Category:** Questions\
**Created:** [November 4, 2023, 1:57am UTC](https://discourse.slimframework.com/t/how-to-check-if-route-has-placeholder/5737 "2023-11-04T01:57:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghabriel](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@ghabriel](https://discourse.slimframework.com/u/ghabriel)\
**Post date:** [November 4, 2023, 1:57am UTC](https://discourse.slimframework.com/t/how-to-check-if-route-has-placeholder/5737/1 "2023-11-04T01:57:19Z")

</div>

I’m using Slim 4, php-di, and eloquent orm currently. So

If I have route like this

```auto
$app->get('users/{user}', [UserController::class, 'show'])->setName('show.user');

$app->get('posts/{post}', [PostController::class, 'show'])->setName('show.post');

```

And some mapping like this

```auto
$routeToModel = [
  'user' => User::class,
  'post' => Post::class
];

```

How to check if current route has placeholder and match one of item in $routeToModel. So I can retrieve the data within controller/middleware

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [November 4, 2023, 9:25am UTC](https://discourse.slimframework.com/t/how-to-check-if-route-has-placeholder/5737/2 "2023-11-04T09:25:49Z")

</div>

To retrieve the route parameter values within a controller, you can use the 3. argument (`array $args`).

Pseudo example:

```php
public function show(Request $request, Response $response, array $args)
{
    $name = (string)$args['user'];

    $response->getBody()->write("Hello, $user");

    return $response;
}

```

To retrieve the route parameter values within a middleware (PSR-15), you may use the RouteContext class.

```php
use Slim\Routing\RouteContext;
// ...

$routeArguments = RouteContext::fromRequest($request)
    ->getRoute()
    ->getArguments();

```

---

<div class="post-metadata">

**Author:** ![ghabriel](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@ghabriel](https://discourse.slimframework.com/u/ghabriel)\
**Post date:** [November 4, 2023, 10:20am UTC](https://discourse.slimframework.com/t/how-to-check-if-route-has-placeholder/5737/3 "2023-11-04T10:20:50Z")

</div>

I’ll try this in middleware then

```auto
use Slim\Routing\RouteContext;
// ...

$routeArguments = RouteContext::fromRequest($request)
    ->getRoute()
    ->getArguments();

foreach ($routeArguments as $key => $argument) {
  if (!is_null($model = $routeToModel[$key])) {
    $request = $request->withAttribute($key, $model::find($argument));
  }
}

```

and in controller

```auto
use App\Models\User;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

public function show(Request $request, Response $response)
{
  return $this->view->render($response, 'auth/user.twig', [
    'user' => $request->getAttribute('user')
  ]);
}

```

---

<div class="post-metadata">

**Author:** ![ghabriel](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@ghabriel](https://discourse.slimframework.com/u/ghabriel)\
**Post date:** [November 4, 2023, 12:05pm UTC](https://discourse.slimframework.com/t/how-to-check-if-route-has-placeholder/5737/4 "2023-11-04T12:05:41Z")

</div>

if I use this

```auto
use Slim\Routing\RouteContext;
// ...

$routeArguments = RouteContext::fromRequest($request)
    ->getRoute()
    ->getArguments();

foreach ($routeArguments as $key => $argument) {
  if (!is_null($model = $routeToModel[$key])) {
    $request = $request->withAttribute($key, $model::find($argument));
  }
}

```

and do

```auto
var_dump($request->getAttribute('user'));

```

it shows the argument value not the result of

```auto
$model::find($argument)

```

but if I use

```auto
$request = $request->withParsedBody([
  $key => $model::find($argument)
]);

```

It works fine, I just don’t understand why.
