Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

Symfony 4 : good practice controller render

$
0
0

I just begun to use Symfony, and I came from CodeIgniter. In this framwork you pass parameters to the view with an array called $data. So if you want to pass the same variables in each views of a controller, you simply instanciate the $data array in the consctrutor and add what ever you want.

For symfony it's a bit different, and I have imagine something to do the same. It's work great, but I ask myself if they doesn't exist a better way to do it. Or do I have to rethink the way I design my code to avoid this sort of things, for example add another twig layout for specify title in each route of the controller, but what if it's a parameter than I have to use for each render ?

First I define this function :

    public function default_data(array $parameters):array{
    $default_parameters = array(
        'controller_name' => 'AdminController',
        'navbar_title' => 'Suivis Satisfaction Utilisateurs',
    );

    return array_merge($default_parameters, $parameters);
}

Then I call render this way :

return $this->render(my_view.html.twig, $this->default_data(array('my_new_parameter' => 'new_data')));

Thanks for reading.


Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>