Slim 3 - How to add 404 Template?

前端 未结 2 1434
栀梦
栀梦 2021-02-10 02:30

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

// @ref: http://help.slimframework.com/discussions/problems/4400-templatespath-doesnt-change
$app->no         


        
相关标签:
2条回答
  • 2021-02-10 02:50

    Create your container:

    // Create container
    $container = new \Slim\Container;
    
    // Register component on container
    $container['view'] = function ($c) {
        $view = new \Slim\Views\Twig('./public/template/');
        $view->addExtension(new \Slim\Views\TwigExtension(
            $c['router'],
            $c['request']->getUri()
        ));
        return $view;
    };
    
    //Override the default Not Found Handler
    $container['notFoundHandler'] = function ($c) {
        return function ($request, $response) use ($c) {
            return $c['view']->render($response->withStatus(404), '404.html', [
                "myMagic" => "Let's roll"
            ]);
        };
    };
    

    Construct the \Slim\App object using the $container and run:

    $app = new \Slim\App($container);
    $app->run();
    
    0 讨论(0)
  • 2021-02-10 03:07

    Option 1:

    use Twig (or any other templating engine)

    Option 2:

    $notFoundPage = file_get_contents($path_to_404_html);
    $response->write($notFoundPage);
    
    0 讨论(0)
提交回复
热议问题