Index

The index view is the main index file, and it can contain one view and multiple partials. It also has access to the "Service Provider." MaplePHP comes with a default index file that you can edit according to your needs. The following is the default index file and will be loaded if you do not manually change it, which is possible in your controllers and middlewares.

Let´s take a look at the resources/index.php file.

<!DOCTYPE html>
<html lang="<?php echo $this->provider()->lang()->prefix(); ?>">
<head>
    <?php echo $this->partial("head")->get(); ?>
</head>
<body>
    <?php echo $this->partial("navigation")->get(); ?>
    <main>
        <?php echo $this->view()->get($args); ?>
    </main>
    <?php echo $this->partial("footer")->get(); ?>
</body>
</html>

You can create multiple index file but you can only load one at the time. You might want to change the template file if you want to use completely change the HTML document or read in a different template engine.

Change index - example 1

The bellow example expects that the file "resources/private.php" exists.

class PrivatePage
{
    private $provider;
    
    public function __construct(Provider $provider)
    {
        $this->provider = $provider;
        $this->provider->view()->setIndex("private");
    }
    
    public function profile(ResponseInterface $response, RequestInterface $request): ResponseInterface
    {
        // Your profile view here
        return $response;
    }
...

Change index - example 2

The bellow code will take over the output buffer and you could if you want attach a different template engine to it.

namespace Http\Middlewares;

use MaplePHP\Handler\Interfaces\MiddlewareInterface;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Foundation\Http\Provider;

class TemplateEngine implements MiddlewareInterface
{
    private $provider;
    
    public function __construct(Provider $provider)
    {
        $this->provider = $provider;
    }
    
    public function after(ResponseInterface $response, RequestInterface $request): ResponseInterface
    {
        $this->provider->view()->setIndex(function() {
            echo "Echo your returned custom Template engine";
        });
        return $response;
    }
...

Last updated