The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How to add 404 Template in Slim 3

lau.thiamkok's Avatar

lau.thiamkok

19 Sep, 2015 12:20 PM

In Slim 2, I can over write the default 404 page easily,

// @ref: http://help.slimframework.com/discussions/problems/4400-templatespath-doesnt-change
$app->notFound(function () use ($app) {
    $view = $app->view();
    $view->setTemplatesDirectory('./public/template/');
    $app->render('404.html');
});

But in Slim 3,

// ref: http://www.slimframework.com/docs/handlers/not-found.html
//Override the default Not Found Handler
$container['notFoundHandler'] = function ($c) {
    return function ($request, $response) use ($c) {
        return $c['response']
            ->withStatus(404)
            ->withHeader('Content-Type', 'text/html')
            ->write('Page not found');
    };
};

How can I add my 404 template ('404.html') in?

  1. 1 Posted by kaxias on 22 Sep, 2015 05:11 AM

    kaxias's Avatar

    u can extend the class NotFound like this

    <?php

    namespace App\Action;

    use Slim\Handlers\NotFound;
    use Slim\Views\Twig;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Message\ResponseInterface;

    class NotFoundHandler extends NotFound {

        private $view;

        public function __construct(Twig $view) {
            $this->view = $view;
        }

        public function __invoke(ServerRequestInterface $request, ResponseInterface $response) {
            parent::__invoke($request, $response);

            $this->view->render($response, '404.twig');

            return $response->withStatus(404);
        }

    }

    ?>

    and after add container like this

    $container['notFoundHandler'] = function ($c) {
        return new NotFoundHandler($c->get('view'), function ($request, $response) use ($c) {
            return $c['response']
                ->withStatus(404);
        });
    };

    sorry if my English it is good.

  2. 2 Posted by kaxias on 22 Sep, 2015 05:59 AM

    kaxias's Avatar
    <?php
    
    namespace App\Action;   // name espace you need set on project
    
    use Slim\Handlers\NotFound; 
    use Slim\Views\Twig; 
    use Psr\Http\Message\ServerRequestInterface; 
    use Psr\Http\Message\ResponseInterface;
    
    class NotFoundHandler extends NotFound {
    
        private $view;
    
        public function __construct(Twig $view) { 
            $this->view = $view; 
        }
    
        public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { 
            parent::__invoke($request, $response);
    
            $this->view->render($response, '404.twig');
    
            return $response->withStatus(404); 
        }
    
    }
    
    use Namespace\Yourproject\NotFoundHandler;
    
    $container['notFoundHandler'] = function ($c) { 
        return new NotFoundHandler($c->get('view'), function ($request, $response) use ($c) { 
            return $c['response'] 
                ->withStatus(404); 
        }); 
    };
    
  3. 3 Posted by lau.thiamkok on 22 Sep, 2015 06:08 AM

    lau.thiamkok's Avatar

    thank you very much!

  4. 4 Posted by Marco Cervellin on 10 Dec, 2015 02:13 PM

    Marco Cervellin's Avatar

    A variation of the previous class, you can extract the 404 template file.

    <?php
    namespace App\Action;
    
    use Slim\Handlers\NotFound;
    use Slim\Views\Twig;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Message\ResponseInterface;
    
    class NotFoundHandler extends NotFound {
    
        private $view;
        private $templateFile;
    
        public function __construct(Twig $view, $templateFile) {
            $this->view = $view;
            $this->templateFile = $templateFile;
        }
    
        public function __invoke(ServerRequestInterface $request, ResponseInterface $response) {
            parent::__invoke($request, $response);
    
            $this->view->render($response, $this->templateFile);
    
            return $response->withStatus(404);
        }
    
    }
    
    $container['notFoundHandler'] = function ($c) {
        return new App\Support\NotFoundHandler($c->get('view'), '404.twig', function ($request, $response) use ($c) {
            return $c['response']->withStatus(404);
        });
    };
    
    
    
  5. 5 Posted by blrik on 27 Dec, 2015 08:04 AM

    blrik's Avatar

    $container['notFoundHandler'] = function ($c) {
    return function ($request, $response) use ($c) {
    return $c['view']->render($response, 'errors/404.twig')->withStatus(404);
    };
    };

  6. 6 Posted by Jairo on 15 Jan, 2016 05:58 PM

    Jairo's Avatar
  7. 7 Posted by kayvan on 17 Jan, 2016 04:08 PM

    kayvan's Avatar

    thank you so much , i will try this

    http://www.nezarati.ir

  8. 8 Posted by Svet on 15 Sep, 2016 04:19 PM

    Svet's Avatar

    Thank you ;))

  9. 9 Posted by mirezeri on 03 May, 2017 03:40 AM

    mirezeri's Avatar

    When we see there is written 404 template error. http://getexboyfriendguide.com/

  10. 10 Posted by qipoxipo on 04 May, 2017 03:15 AM

    qipoxipo's Avatar

    Put these codes in your nameserver.
    http://thediabetesloopholepdf.com/

  11. 11 Posted by Showbox for iOS on 09 May, 2017 08:15 AM

    Showbox for iOS's Avatar

    I hope I get some help from Showbox for iOS

  12. 12 Posted by brd on 05 Jun, 2017 04:50 AM

    brd's Avatar

    Thanks for showing this to me. I am really impressed. Get Rid of Hemorrhoids - RemoveHemorrhoidsPlan

  13. 13 Posted by spor on 08 Jun, 2017 04:52 AM

    spor's Avatar

    404 Template is must for every site. I am saying this to you again and again. Pregnancy Miracle Review

  14. 14 Posted by mall on 28 Sep, 2017 02:57 AM

    mall's Avatar

    Tell me some great tips about these template.
    http://www.reviewprotocol.com/red-smoothie-detox-factor/

  15. 15 Posted by wofiwale on 29 Sep, 2017 02:46 AM

    wofiwale's Avatar

    Using free templates have many errors.
    http://thethoughtelevators.com/

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac