# 404 notFoundHandler - different template for routes

**URL:** <https://discourse.slimframework.com/t/404-notfoundhandler-different-template-for-routes/1281>\
**Category:** Questions\
**Created:** [March 15, 2017, 4:04pm UTC](https://discourse.slimframework.com/t/404-notfoundhandler-different-template-for-routes/1281 "2017-03-15T16:04:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@Dark](https://discourse.slimframework.com/u/Dark)\
**Post date:** [March 15, 2017, 4:04pm UTC](https://discourse.slimframework.com/t/404-notfoundhandler-different-template-for-routes/1281/1 "2017-03-15T16:04:35Z")

</div>

I’m wondering if there is any way to run other 404 notFoundHandler or different twig template depending on route.

I have 3 routing groups:  
Website - '/'  
Website admin panel - '/admin’  
Api - ‘/api’

I have working notFoundHandler for 404 but i want to use other notFoundHandler or render different template depending on where 404 was returned. For example:

/admin/badUrl - should run adminNotFoundHandler or at least render templates/admin/404.twig  
/api/badUrl - should run apiNotFoundHandler without rendering template - just 404  
/anyOtherBadUrl - should run websiteNotFoundHandler or at least render templates/website/404.twig

At this point im thinking if it would be a good idea to actually create 3 app entrypoints with .htaccess and just split each app specific classes, dependencies, settings etc. which would resolve this problem.

I have seen a workaround there: [Handling multiple 404’s?](http://discourse.slimframework.com/t/handling-multiple-404s/972) but it doesn’t really feel right to change handler behaviour depending on URI path.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [March 19, 2017, 10:59am UTC](https://discourse.slimframework.com/t/404-notfoundhandler-different-template-for-routes/1281/2 "2017-03-19T10:59:49Z")

</div>

You can add an application middleware to replace ‘notFoundHandler’ in the container:

```php
$container = [
    'admin.notFoundHandler' => function($container) { /* ... */ },
    'api.notFoundHandler' => function($container) { /* ... */ }
];

$app = new Slim\App($container);

$app->add(function($request, $response, $next) use ($app) {
    // change notFoundHandler based on the request path match
    $container = $app->getContainer();
    $path = $request->getUri()->getPath();

    if (preg_match('#/admin(/.*)?$#', $path)) {
        $container['notFoundHandler'] = function ($container) { 
            return $container->get('admin.notFoundHandler'); 
        };
    } elseif (preg_match('#/api(/.*)?$#', $path)) {
        $container['notFoundHandler'] = function ($container) { 
            return $container->get('api.notFoundHandler'); 
        }; 
    }

    return $next($request, $response);
});

$app->run();

```
