V
V
Vladislav2016-08-14 20:31:56
symfony
Vladislav, 2016-08-14 20:31:56

Why doesn't Twig see the variable?

Understanding FOSRestBundle in Symfony 3.
Made a controller:

<?php

namespace AppBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;

class UsersController extends FOSRestController
{
    public function getUsersAction()
    {
        $data = ['name'=>'John']; // get data, in this case list of users.
        $view = $this->view($data, 200)
            ->setTemplateData(['role'=>'user'])
            ->setTemplate("AppBundle:UsersController:get_users.html.twig")
            ->setTemplateVar('users');

        return $this->handleView($view);
    }
}

and such a route
home:
    path:     /
    defaults: { _controller: AppBundle:Default:index }

test:
    path:     /test
    defaults: { _controller: AppBundle:Users:getUsers }

test_api:
    path:     /api/test
    defaults: { _controller: AppBundle:Users:getUsers }

When I request /test, I get an error:
Variable "users" does not exist in AppBundle:UsersController:get_users.html.twig at line 7
But when I request /api/test, everything works and the users variable is displayed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Romanenko, 2016-08-15
@slimus

The problem lies in the fosrestbundle with it and needs to be dealt with: stackoverflow.com/questions/12457683/fosrestbundle...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question