How would I go about executing a database query on every page within a Symfony2 bundle?
I'm attempting to create a who's online functionality but this requires a query to be executed globally.
Thanks! :)
You can render a sub-template, for example (in a project of mine) in my layout.html.twig I have this:
{% render "EUPaNdataBundle:Home:listAllInstitutes" %}
This will render a list of all institutes in a drop down menu in the top navigation bar.
listAllInstitutes is an action in the Home controller which gets a list of institutes from the DB and then returns the listAllInstitutes.html.twig template
Create an event listener that listens for the kernel.request event and updates the current user's last seen time if she's logged in. See my another answer for an example of a listener and adapt it to your needs.
Interesting question.
If you only need it inside a view (probably layout) I suggest to create Twig Helper. You can have access to Doctrine in helper class. Check this link:
http://www.kiwwito.com/article/extending-twig-in-symfony-2-add-custom-functions
Related
I've recently started creating a layout template for my Laravel project following the method stated on here:
https://selftaughtcoders.com/from-idea-to-launch/lesson-20/creating-a-laravel-5-blade-layout-to-standardize-your-pages-appearance/
It seems easy enough to wrap a view with a 'master' layout. However, my layout includes a userbar in the top right (kind of like SO's) which has the user's avatar and username.
What is the best way to do this? I can't imagine I have to pass it through to every view that extends my master layout, but at the moment, it seems that is the only answer.
In your master layout you can simply add the user bar and wrap it in the blade helper #auth like this
#auth
This will only be shown when a user is logged in.
#endauth
Check the control structures from the documentation for more detailed information.
I think you will first need to add an separate element in some element folder. This could be the first step to perform.
Secondly, once you are done with the first step, you will also have to extend the element with #extends() function in main layout.
I hope this helps.
I have code that I want to run on every page load, such as looking up menu items, looking up the users details etc. These will be displayed on partial views that make up the main view.
Where do I place this code so that it can fill my partial views with each page load? I know I can just add the code to the top of the partial view itself, but this doesn't really follow the MVC pattern.
Is there a function that is always called that I can hook into in my base controller?
You can create a base viewmodel for the repeated code and make other viewmodels inherit from it.
...such as looking up menu items, looking up the users details etc
You're a bit unclear about the type of information you want to load: in case the info is a view-component then indeed you should create a base-view and inherit from it or include it (composition) in any other view.
But, in case it is "user-information" - the data should live in a model-component that again, may live as "base-model" object that is included in other model components.
I have a blade template called 'main' and I wonder how I can render a sub template by calling a controllers method in my main template. Lets say I have a Controller WidgetsController with a method getSubView. The method returns a specific view with some data from (for instance) a database.
I already tried to #include a template but this will not call the controller which sets some necessary data to the view.
Thanks.
views don't call services, they only take variables and put them on screen for presentation.
you're on the good side with #include(). you only need to gather the infos for that sub view beforehand in the controller, and pass it to the View::make('main')->with($vars).
you may also consider using another <?= View::make('subview')->with($vars->sub);?> within the the view. or just use the #extend functionality.
I don't entirely understand your question.
I think what you are looking for is a View Composer
It allows you to get data for a subview without having to create the data in every controller.
I have an action that renders search view to do search as the above search bar in this website, so its should be shown in every view.
I don't know what is the mechanism to do it. for example if I make the search action as a widget this will not be fine, because the results of search will be shown in the same position of the search widget ( at the top of website).
so, how I can make a search action that should be shown in every view in the website?
In order to resue same search function in everywhere, you need to create a widget.
I have explained briefly in How a widget works, then you can attach it in every view that you want.
If you don't have any idea to begin, check this out: Yii ESearch
Here are some references that would be useful:
how-to-use-a-widget-as-an-action-provider
actions-code-reuse-with-caction/
Yii Widget
If you want to add something to every view then you should add it to the layout. By the sounds of it you don't need to use a widget at all, although it would probably help with code maintainability.
You never mentioned a requirement for ajax so keep it simple and don't use it. When someone enters a search and clicks submit (or presses return) then the form submits to the SearchController. This way there is no need to have a search action in each controller.
If you particularly want the same action in every controller then create a Controller base class with that function in it and inherit from it to create all your other controllers.
I have a line of code I wish to run after a view script has rendered, but I want this code to be placed in the action.
This is action specific and only updates a flag in the database, so it seems like overkill to utilise a plugin just for this.
What other options do I have? Could I register an abstract plugin that I can extend and hook into dispatchLoopShutdown() or routeShutdown() from the specific action I am in?
Thanks
By default 'View' is the last thing that the pointer will pass through.
you can:
$this->_helper->removeHelper('viewRenderer');
to globally remove views and then do the rest of action.
Actually, in many of my codes, I load views in chunks and have not faced any issues whilst rendering them before. Example, if I do:
$this->view->shouldGoinHeader;
// some actions here
$this->view->someHere;
// some more action thing
It works! So you can update a flag on your database using try catch if you are checking if view has been rendered.
If something is getting executed, you might want to post your codes?