How to add a user bar to a Laravel layout template? - php

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.

Related

Adding a user defined header in Laravel Template

Perhaps it was my wording when searching because I new to Laravel but I am trying to figure out the most feasible way to create a user defined header script variable. My thought behind this is to be able to have an auth section of the website - /header and on that route I would be able to manage a section of the header where I can add Analytics script or any 3rd party script tag.
Thought is to include #yield('userHeader') in my master template.
Then in each page it should be included in just add a section
#section('userHeader')
{!! $header->script !!}
#endsection
So I would essentially have to create a model for Header and a new DB table to house the entirety of the script tag.
Seems like it would work but I'm sure someone has done this before. Is there a more efficient way of doing this?
If I do decide to do it like stated above I would essentially then have to:
use App\Header;
in each controller I would want
$header = Header::all();
Then use the following in each page. There should only be a single record in this table because there will ony be one user defined header so I am not sure that a DB table is necessary. Is this the best way to include a user defined area in the header? Will it cause any issues with security or site stability if this will include special characters?
#section('userHeader')
#if(count($header) > 0)
{!! $header->script !!}
#endif
#endsection
You can do it with View Composers
Or you can set the data in the Main Controller and access it from every controller which extends the main one. I think it will be better with View Composers

Where to place common code in Phalcon-based site that is called each page load

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.

how to use a controller's action in all controllers

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.

Symfony 2: Global Database Query

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

ACL and appearance manipulation of links, forms and DOM elements

I would like to find out which strategy is the best for links, forms and DOM elements appearance manipulation (show/hide) on top level of multi modular application (admin, default, etc) ??
It should be done through ACL and it's usage I know well, permission are stored in DB.
If someone tries to access certain page (module/controller/action) that is not allowed for him, an info page is passed which says that he is not allowed. In that case all elements are visible.
I have a few strategies for elements appearance manipulation on my mind:
To set in controller
$this->view->allow_delete_link = $acl->isAllowed($role, 'delete_post'); // boolean
and to ask in view file if it is true or false and show/hide
<?php if ($this->allow_delete_link): ?>[link html] <?php endif; ?>
The drawback here is if i have 50 links on page I will need to have 50 lines of code in my controller where I am doing this and I don't like that very much.
Similar to the first except ACL is static class so view file asks if:
<?php if(My_Custom_Acl::getIsAllowed('some_resource', 'delete_post_action'){ echo 'link'; } ?>
To make one view helper which I will call upon creating every link in which I would ask if user that is logged in has access, if yes return the whole link, if not, return "" or false.
View helpers are very slow so that's my last resort.
To make separate view.phtml file for every group of users, then in controller, in which user is logged, show appropriate view.
$this->render('xx_view');
This violates DRYS, so I think this method is not good.
Is there some other strategy for something like this, because I already see that I will have headache if I choose any of these 4.
Maybe some existing plugin/class for that would be the best?
thanks in advance !
I forgot to mention, that I am using Zend framework and Zend_Acl.
3 is the best solution, because you often need to pass the database row to the isAllowed call (for example, if you need to test the owner of the post to choose if you can show the delete link).
You can't do this in the controller like you suggest in 1, because you will need one line for each row.
2 is ugly.
You can speed up resolution of view helpers by extending Zend_View: http://framework.zend.com/manual/fr/performance.view.html.

Categories