# Implementing slim/twig-view with php-di/slim-bridge

**URL:** <https://discourse.slimframework.com/t/implementing-slim-twig-view-with-php-di-slim-bridge/5822>\
**Category:** Questions\
**Created:** [February 27, 2024, 6:25pm UTC](https://discourse.slimframework.com/t/implementing-slim-twig-view-with-php-di-slim-bridge/5822 "2024-02-27T18:25:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Pallieguy](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@Pallieguy](https://discourse.slimframework.com/u/Pallieguy)\
**Post date:** [February 27, 2024, 10:06pm UTC](https://discourse.slimframework.com/t/implementing-slim-twig-view-with-php-di-slim-bridge/5822/2 "2024-02-27T22:06:42Z")

</div>

I parsed out the two different view outputs to different controllers, jsut in case there was some logic contradiction I didn’t know about.

**Updated routes.php**

```auto
<?php

//Uses
use Slim\App;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Controllers\ToolController;
use App\Controllers\TwigController;

//Returns the call based on the route. This is where the new tools get listed
return function (App $app) {
	//Testing twig
	$app->get ('/twig[/{name}]', [TwigController::class, 'twigTest']);
	//Undefined route
	$app->get ('/{tool}[/{step}]', [ToolController::class, 'selectTool']);
};

```

**separated TwigController.php**

```auto
<?php

//Uses and namespaces
namespace App\Controllers;
use Slim\Views\Twig;

//Define the class
class TwigController {
	protected $view;

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

	public function twigTest ($request, $response, $name){
        return $this->view->render ($response, 'auth\home.twig', ['name' => $name]);
    }
}

```

Entering anything in my URL other than …/twig/foo shows the expected text output  
Entering a twig URL gets the same error. I tried loading Twig directly from the request variable as I had seen some tutorials do and so the code then looked like this:

```auto
<?php

//Uses and namespaces
namespace App\Controllers;
use Slim\Views\Twig;

//Define the class
class TwigController {
	protected $view;

    public function __construct () {
    }

	public function twigTest ($request, $response, $name){
        return Twig::fromRequest ($request)->render ($response, 'auth\home.twig', ['name' => $name]);
    }
}

```

and that changed my error message to  
`Twig could not be found in the server request attributes using the key "view".`  
Which this post: [Adding Twig to Slim4 Skeleton](https://discourse.slimframework.com/t/adding-twig-to-slim4-skeleton/5384)  
suggests is an issue of when I’m adding the TwigMiddleware, but given how php-di/slim-bridge changed the middleware implementation I have no idea how I can restructure it.

---

_[View the full topic](https://discourse.slimframework.com/t/implementing-slim-twig-view-with-php-di-slim-bridge/5822)._
