# Create middleware to replace slim.before hook

**URL:** <https://discourse.slimframework.com/t/create-middleware-to-replace-slim-before-hook/5084>\
**Category:** Questions\
**Created:** [January 18, 2022, 11:35am UTC](https://discourse.slimframework.com/t/create-middleware-to-replace-slim-before-hook/5084 "2022-01-18T11:35:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 18, 2022, 11:35am UTC](https://discourse.slimframework.com/t/create-middleware-to-replace-slim-before-hook/5084/1 "2022-01-18T11:35:56Z")

</div>

Hello all! , i am upgrading my old application from slim 2 to slim 3

in my application i added the one value to the route before the output buffer using **slim.before** hook

for example code and route

```auto
$app->hook('slim.before', function () use ($app) {
    $env = $app->environment();
    $path = $env['PATH_INFO'];
    
    // spliting the route and adding the dynamic value to the route
    $uriArray = explode('/', $path);
    $dynamicvalue = 'value';
    if(array_key_exists($uriArray[1], array)) {
        $dynamicvalue = $uriArray[1];
        //we are trimming the api route
        $path_trimmed = substr($path, strlen($dynamicvalue) + 1); 
        $env['PATH_INFO'] = $path_trimmed;
    }
  
});

```

i have one route like this it returns json response data

```auto
https://api.fakedata.com/fakeid

```

if i hit the route with adding extra parameter in the search bar to route like this

```auto
https://api.fakedata.com/en/fakeid

```

it will also return same response as the response which return from first route  
i achieved this by adding the **en** parameter using slim.before hook in slim 2

from slim 3 hooks is deprecated , and replaced with middleware

how can we achieve this in slim 3 or 4 using middleware

can anyone help me with the code

Thank you!

---

<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:** [January 18, 2022, 12:33pm UTC](https://discourse.slimframework.com/t/create-middleware-to-replace-slim-before-hook/5084/2 "2022-01-18T12:33:26Z")

</div>

It’s hard to guess how your source code looks like. So it’s even harder to help here. Could you please show us your current routes for this example?

---

<div class="post-metadata">

**Author:** ![Dinesh](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dinesh](https://discourse.slimframework.com/u/Dinesh)\
**Post date:** [January 18, 2022, 12:42pm UTC](https://discourse.slimframework.com/t/create-middleware-to-replace-slim-before-hook/5084/3 "2022-01-18T12:42:27Z")

</div>

@odan Added the example code please let me know if any other requirements

---

<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:** [January 18, 2022, 6:56pm UTC](https://discourse.slimframework.com/t/create-middleware-to-replace-slim-before-hook/5084/4 "2022-01-18T18:56:54Z")

</div>

I would suggest a different approach, that should behave like you expected.

Instead of using a middleware for it, I would try to create a [route with a placeholder](https://www.slimframework.com/docs/v4/objects/routing.html#route-placeholders) for the additional parameters.

Have you tried this?
