How to avoid query duplication when using view composers? - php

I'm using Laravel 5.1.
I have many views which somehow included in each other. For example, I have the main layout, some modal windows, some custom pages. So basically I have N views.
Let me show you a concrete example. The layout.blade.php is basic layout. The companies/index.blade.php is company list. The modals/companies/create.blade.php is a modal template which is included in layout.
Imagine I have a list of company industries. I need to provide the list to the modals/companies/create.blade.php and to the companies/index.blade.php. The list shouldn't be passed from the controller, because it's kind of code duplication and the list is not related to any conditions, so I don't want to pollute controllers with the same lines of code.
So I have a view composer service provider where I link view composers to views. I may have two approaches:
Each view composer represents small part of data, like "IndustriesViewComposer" which serves industry list
Each view composer represents all data necessary for some view, like "CreateCompanyModalViewComposer"
When I tried the first way my service provide file looked like:
'App\ViewComposers\IndustriesViewComposer' => [
'modals.companies.create',
'modals.companies.edit'
... some other views ...
],
In this case if some of the views are included in the same view the query which retrieves industry list will be duplicated. So if I have many small view composers and corresponding views, then the queries are duplicated in case I include those views together. Since I have many modals which are included together I get the duplication.
In the second way I have exactly one view composer for one view. Same situation happens here. For example, I know that I need to provide industry list for company creation modal, and the same list I need to provide to company edition modal.
View sharing for composers won't work since I need to provide the data only for specific pages for users who is logged in.
So basically the question is: how to execute one query for multiple views in case the views are rendering in the current request and avoid duplication.
Imagine I have a single company page.
1) In every page of my system I have company creation modal which needs industries list
2) In single company page I include company edition modal which needs industries list
3) Single page also need industries list
How to retrieve the list only once and provide it to single view, new company modal and edit company modal views?

You can use a middleware. In the middleware you make a query and save the model (or collection) with a bind to IoC Container.
So, in your middleware you can do (for example):
$my_data = Data::where('some_field','some_value')->first();
App::instance('my_data_saved', $my_data);
do not forget to include the use App;
After that you can access the data where you want (controllers, views, etc.) with IoC Container, for example:
<title>{{ App::make('my_data_saved')->titleĀ }}</title>
Then you only need include the middleware in your routes or controllers where need the data for your views.

Related

Application design for different user types

I'm trying to figure out the best way to build an application with multiple user types (i.e. Admin, Vendor and Customer).
I'm using Symfony2 to develop the application. I have two options in mind:
Have one controller and check for the user role and render the corresponding view for that role. (One controller, Multiple views)
Have one view and check for the user role to render the corresponding piece. (One controller, one view)
The difference between the above two methods, that is think the first one will have views to be cleaner, without too much logic in it. But will lead to too much logic inside the controller.
If there any better way to think this out ?
The way I do it is similar to your option 2 I am not sure how it works with symphony since I have my own custom framework but its similar in a way:
Controller handles the logic and passes in the parameter to view about user, whether user is admin or has a specific permission
View then renders specific parts of the code depending on this permission/role
Example Controller:
$view = new View('PATH_TO_VIEW');
$view->isAdmin = true;
$view->canDropDatabase = true;
Example View:
<div>
<?php if ($this->isAdmin) : ?>
*** Show some Admin Stuff ***
<?php else : ?>
*** Show Non-Admin Stuff ***
<?php endif; ?>
<?php if ($this->canDropDatabase) : ?>
*** Well I hope you dont do this ***
<?php endif; ?>
</div>
The reason I like this approach is because if I need to update something I only deal with one file versus each view file for each role. Plus for the future if you add roles you need to add a new file while you can just add an extra elsif condition.
I developed a site with Symfony2 and user roles. I didn't change entire pages depending on the role, but there were significant chunks that would change based on role. I went with option 2 and used the logic within the views, and that seemed to work well for me. Cheers
Personally I would go with option 1.
You can minimize the logic in controller by just passing the role of the user as a string/array to your twig template and use something like:
{% include role ~ 'header.html' %}
or in case of an array on which you need to do some checks/apply logic you can use a custom twig extension instead of logic in controllers
{% include roles|check_roles ~ 'header.html' %}
Alternatively you can create a service class and inject it in twig so you can use it's methods inside the view so that you keep your controllers clean and the concern of selecting view/partial for each role to be in one place only.

Is it possible (and how to?) divide application to backend (admin part) and frontend (user part) parts?

I am developing web application, using zend framework 2. In my app, I want some modules. News, for example, users etc.
Is it possible, to divide whole application to two big parts: front-end (for users) and back-end (for admins).
In front-end there will be part of News module, which render news list, full news item etc.
In back-end there will be part of News module, which will render list of all news, tools for editing, deleting etc.
At the moment, I have simple structure:
I have MyZfcAdmin module, with routes configurated so, that when I am writing /admin/news/edit/5 it will call NewsController edit action with id 5 and render it. So now, I have all actions (for admins and for users) in one controller. Is it possible to create something like this:
module
frontend
News
SomeMoreModule
backend
News
SomeMoreModule
I found topic on google questions, in that suggested to use this CMS, but I want to try it by myself.
Different people have different idea in managing application, for me I will have news module that handle all news related calls both admin parts and user accessible parts. The idea you have described have some side effects like duplication of code between modules and crossing of borders between isolate modules like calling action in another modules(it's ok if there are related or fall in hierarchy), which may have issues in long term maintenance of the applications.
IMHO I might suggest you could create modules like News, users, etc where it can have combined functionality of both admin and user access. You can later include user authorization plug ins using bootstrap events and restrict action accessible by admin and other users
There is not concept of submodules inside any module. But you can give them proper naming conversations. If code is not formed in oops properly, then there can be code duplication into your project. As per #raj's answer, it's good to give restrictions based on user role. If any action is not intersecting admin and user role then you can separate those actions, but actions like add or edit news content are same for both role then you should point to same action by router.

Need a light understanding MVC pattern

You have the URL:
http:///www.site.com/controller/method/values/1/2/3
Do I have to always have the controller being called or can I have the view being called and instantiate the controller inside the view or in a bootstrap file referring to this view?
What I don't get it is if I need more than 1 controller on the view, how to archive that?
For example:
On my index page I want run a simple CMS, where the admin can change the text blocks and images of the site. That would be on the content management controller.
On my index page I also got the latest added products vitrine, what would be controlled by the controller products.
If I define www.site.com/contentmanagement or www.site.com to run the contentmanagement controller, how the product controller would be called?
Also, another example. On my menu I got a link to a page called aboutus, that would be a simple page and the only feature needed would be the content management controller to manage the texts blocks.
If I follow the pattern Im reading all over the place I will end with a link like:
http://www.site.com/contentmanagement/method/aboutus
?
Kinda lost here cause surely this URL will look weird. Would be much easier to have the URL calling the view http://www.site.com/aboutus and a boot file where I can tell the controller that should be loaded when the surfer is there ...
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
I appreciate any help or a light here, thanks.
by the way, Im coding in PHP.
Hm, if you want to have text blocks and images of the site (one controller), and products vitrine (second controller), then call the methods you need in one controller..
I would do it this way: when you request index page with all the elements you mentioned above, actually one controller is called, and it decides which view to show.. so in that controller, you call the all methods that you need, get the data, and pass it to the view.. if the methods are called from other controllers, just call that methods and get the data.. i often make some static methods in controllers, which I can call anywhere, so I don't have to instantiate whole object..
E.g. you call www.site.com/contentmanagement, controller is called, which will display index view, and in that controller, you call all methods you need, prepare the data, and pass that data to the final view which will be displayed..
Do I have to always have the controller being called or can I have (..blah blah..)?
It kinda depends on what you understand by "call".
Controller needs an ability to change state of both View and Model(s).
And each View need ability to request data (in Model2 MVC) from Model(s).
Thought Controller should not cause the rendering of View (which is common in all the RoR sycophants) much like View has not business in executing actions on the Controller.
What I don't get it is if I need more than 1 controller on the view, how to archive that?
Views and Controllers should have 1:1 relationship. If your view need more then one controller, you might want to look into HMVC architecture.
For example: On my index page I want run a simple CMS, (.. more blah .. ) how the product controller would be called?
CMS should be a separate application ( or at least module ) with multiple controllers.
If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?
Why not just http://site.com/cms/content/2/edit (where 2 is the ID for "about us" page). There is no law which states that URL structure for site administration must mirror the publicly available page .. hell .. it can actually cause increased vulnerability.
I am going to try to walk
What I don't get it is if I need more than 1 controller on the view,
how to archive that?
For example: On my index page I want run a simple CMS, where the admin
can change the text blocks and images of the site. That would be on
the content management controller. On my index page I also got the
latest added products vitrine, what would be controlled by the
controller products. If I define www.site.com/contentmanagement or
www.site.com to run the contentmanagement controller, how the product
controller would be called?
Having the URL to contains the controller's name is definitely nice, but it is not the requirement for MVC. So you don't have to necessary map your controller to the URL itself. A classic MVC does not tie itself to a particular naming convention, so you could call your product controller via different URL which then process the product and show the view for the product.The important for MVC is to have one controller that deals with sets of model and resulting in one view as the presentation layer. In your particular example, your entry point seems to be a single controller that interacts with both ContentManagement and Product Class/Module and the resulting interaction produce a single view.
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
Thus your original interaction above is not completely off, except that you are not really calling two controllers, but rather doing the following upon hitting index:
Load a controller, let's call this one IndexController
Load ContentManagement module to get the related content, you might want to put the info as part of your Model for the Index page
Load Product module to get related products, again put this into your Model
Pass the Model containing the info for rendering the page into the View
In your View, you render the Model that contains both Content from ContentManagement module and Product list from the Product module thereby producing a single view
If I follow the pattern Im reading all over the place I will end with
a link like: http://www.site.com/contentmanagement/method/aboutus ?
This is again not a requirement, you should follow what makes sense and easier for the users. Also, if you are required to follow the conventions, you could always make your URL pretty by using URL mapping.
What you are looking for is called HMVC and there are a couple of frameworks for that out there, like Kohana.
Also see this question:
What is the HMVC pattern?

Adding interactions to admin pages generated by the admin generator

I am using Symfony 1.2.9 (with Propel ORM) to create a website. I have started using the admin generator to implement the admin functionality.
I have come accross a slight 'problem' however. My models are related (e.g. one table may have several 1:N relations and N:N relations). I have not found a way to address this satisfactorily yet. As a tactical solution (for list views), I have decided to simply show the parent object, and then add interactions to show the related objects.
I'll use a Blog model to illustrate this.
Here are the relationships for a blog model:
N:M relationship with Blogroll (models a blog roll)
1:N relationship with Blogpost (models a post submitted to a blog)
I had originally intended on displaying the (paged) blogpost list for a blog,, when it was selected, using AJAX, but I am struggling enough with the admin generator as it is, so I have shelved that idea - unless someone is kind enough to shed some light on how to do this.
Instead, what I am now doing (as a tactical/interim soln), is I have added interactions to the list view which allow a user to:
View a list of the blog roll for the
blog on that row
View a list of the posts for the blog on that row
Add a post for the blog on tha row
In all of the above, I have written actions that will basically forward the request to the approriate action (admin generated). However, I need to pass some parameters (like the blog id etc), so that the correct blog roll or blog post list etc is returned.
I am sure there is a better way of doing what I want to do, but in case there isn't here are my questions:
How may I obtain the object that relates to a specific row (of the
clicked link) in the list view (e.g. the blog object in this example)
Once I have the object, I may choose to extract various fields: id etc.
How can I pass these arguments to the admin generated action ?
Regarding the second question, my guess is that this may be the way to do it (I may be wrong)
public function executeMyAddedBlogRollInteractionLink(sfWebRequest $request)
{
// get the object *somehow* (I'm guessing this may work)
$object = $this->getRoute()->getObject();
// retrieve the required parameters from the object, and build a query string
$query_str=$object->getId();
//forward the request to the generated code (action to display blogroll list in this case)
$this->forward('backendmodulename',"getblogrolllistaction?params=$query_string");
}
This feels like a bit of a hack, but I'm not sure how else to go about it. I'm also not to keen on sending params (which may include user_id etc via a GET, even a POST is not that much safer, since it is fairly sraightforward to see what requests a browser is making). if there is a better way than what I suggest above to implement this kind of administration that is required for objects with 1 or more M:N relationships, I will be very glad to hear the "recommended" way of going about it.
I remember reading about marking certain actions as internal. i.e. callable from only within the app. I wonder if that would be useful in this instance?
I'm assuming your application is called backend. Suppose there are two models, BlogPost and BlogPostComment. These are managed using admin generated modules called blog_post and blog_post_comment.
I believe you want a link against each BlogPost displayed on the list page at backend.php/blog_post. The links take you to backend.php/blog_post_comment, which should only show comments related to the relevant BlogPost.
Under apps/backend/blog_post/templates, create a file called _commentslink.php and put this in it:
View Comments
Then in apps/backend/blog_post/config/generator.yml, you need to include this partial in the fields for the list view:
....
param:
config:
list:
display: [ id, title, _commentslink ]
Note the _commentslink - the _ tells it to use a partial instead of looking for the field in the model. Your object is available in this partial as $<name of model> - $blog_post in this case.
Essentially, all this method does is links to the same action as the filter on the comments list normally goes to, passing the relevant condition to it to make it filter by blogpost_id.
If you've got CSRF protection enabled in the backend, you'll need to disable it, or this method won't work. This is set in apps/backend/config/settings.yml. There will be a setting in there called csrf_secret - it should be set to false to disable csrf.
You should try symfony 1.3/1.4 out if you need support for 1:N relationships in forms. 1.3 is in my experience a relatively hassle free upgrade from 1.2.x - 1.4 is the same, but with deprecated features removed.

Views: Including multiple views in one view

I developed an application with different MVCs using the Yii Framework
For example
I created a crud layer for Student Contact details and Student Courses etc.
All worked perfectly since each had its own menu, and when clicked eachs own view rendered.
Now my client wants everything on one page, and is quite persistent, we are talking about 7 MVC that need to be displayed on one page. Each MVC has it's own controller, model and view and own DB table. How can I render all of them on one page without re-writing the whole application? Is this possible.
If I understood your problem correctly, you simply want to merge all menu items and show the full navigation on each page.
By separating menus into standalone views and including each and one of them into a navigation view, you can have a well-structured non-repeating code.
$this->renderPartial('anotherView');
is pretty much everything you might need to know to get started. This is only callable in views as $this refers to the current controller object.
You can use views from other controllers:
$this->renderPartial('application.views.student_Contact.show',array('model'=>$model));
Hope this helps.

Categories