Codeigniter multiple views in one view - php

I am working on a web application. This might be a silly question, but I want to know whether I am taking the good approach to this or not.
I want to have multiple views on one single view/page.
The Codeigniter documentation says that "A view is simply a web page, or a page fragment, like a header, footer, sidebar, ...".
I want to have a header, some quick search view, some other view and a footer for a example. Should I implement a controller to every view (header, quick search, footer, ...), or is it better to implement every view functions in a single controller? For instance, if I know that the header, footer, quick search views are going to always be there (even if their content might change) should I put functions for all those views in one controller?
Please, help.

one approach is to have a template view that has the elements you require.
see this pseudo-code example...
so, template_view.php has:
$this->load->view('header',$header);
$this->load->view('quicksearch',$quickssearch);
$this->load->view('body',$body);
$this->load->view('footer',$footer);
your single controller then calls the template with the parameters for each view.
$data = new stdClass();
$data->header = ....
$data->quickssearch = ....
$data->body = .....
$data->footer = .....
$this->load->view('template_view',$data);

Related

How do I supply the view with all necessary query data in Symfony / MVC frameworks?

Bit of a beginner question about Symfony2/3 here.
Say I'm building a page to display an article. I create a route like "/articles/{id}", link that to a controller which queries the database for the relevant article dataset and pass that on to a Twig view template to display everything. So far, so simple.
But what if I wanted to also include a "Related posts" section at the bottom of the page, which presents other articles with similar topics that might also be of interest. Or a sidebar with numerous other widgets that all require their own logic and database queries.
So, how do I go about supplying every element of the final page with the required data? Do I anticipate every single element of the view and query the data that will be needed beforehand in the controller, to hand it over in bulk? (But if so, what if I modified my sidebar template and switched the widgets around. Would I then have to update the logic in every controller on my site that leads to a page with the sidebar?) Or do I just somehow call back additional data from inside the view when needed?
Coming from simpler stuff like Wordpress where the logic is freely mixed within the view and PHP functions can be called whenever, the seemingly strict and/or desired separation between the view and the logic is introducing a new data flow I'm still trying to wrap my head around. Thanks!
I Hope You can achieve this by embedding Controllers in a Template:
<div id="sidebar">
{{ render(controller(
'AppBundle:Article:recentArticles',
{ 'max': 3 }
)) }}
</div>
Refer : https://symfony.com/doc/current/templating/embedding_controllers.html

multiple controllers for one view php mvc design

I am very new to MVC.
I just finished reading a book and am trying to implement what I have learned, but I am stuck. In the book and some other explanations I have read online, it's always one controller for one view, like navigation view being controlled by its controller, login form controlled by its own controller.
But I have a header with couple of navigation links, and a search form. Do I separate navigation from searching or assume searching is part of navigation and just control them all in one controller?
First of all, you seem to have the impression that "template" and "view" is the same thing. It's wrong. A properly done view will juggle multiple templates and choose which combination to use based on the current state of the model layer.
As for your navigation & search thing ... well ... it's confusing. Each link in navigation would point to either a different controller entirely or a different methods of controller. And search query would definitely be submitted to a separate controller/view pair.
The navigation+search is just a template, that is used in multiple views as part of the complete response.

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 PHP MVC should look like with jQuery/javascript code?

Well, I've read this tutorial if I could say: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code
And, actually, I write my code very similiary. But my question, where should I insert my jQuery code? I am sure it should be in the part of the View, but are there any good examples on how should I combine it to make "live" websites?
Edit: By saying live, I mean, for example, send POST request through Ajax and get information and similar. So, no refreshes for people.
Thank you.
jQuery as a part of javascript resources should be included in html.head or in-place, depending on what should jquery do and if the code is reusable for more views.
So it has to be part of View and you're choice is to set it up in layout or action view itself
If you need the javascript on every page then add it to your master view if not then just include it to the particular view files.
In context to codeigniter:
I extend the controller with MY_Controller and initialize a property as array which would hold the scripts that are added dynamically to the view.
eg.
var $templateData['scripts'] = array();
The controllers then pass $this->templateData to the views
And the views load the extra scripts( available as $scripts) as directed by the controllers in the head tag
This way you can even avoid loading the main jquery file when not needed.
Loading jquery.js only for the controller that need it.

Simple Code Igniter sidebar implementation? How?

I posted a thread a few days back but it didn’t take the direction I was looking for, so I will try again and fingers cross explain a little better.
I have a list of controllers.. Home, League, Forum as so on. Once the controller has its data it calls down to a view named template which in turn calls the default header, footer, content and sidebar views. (The content variable is set by the controller).
So far so good, this works great until I realise the need the sidebar data to be dynamic instead of its current static state.. Now as the site grows larger I would prefer not to have to specify the data that gets called for the sidebar each time.
I would prefer to set a variable in the controller that loads “widgets” possibly, I see this being done in the template view, however I know controllers can not be called in view, so I am stuck.
$sidebar = array(‘latest_news’,‘latest_forum’);
Currently I am emulating this affect with Ajax… I specify a list of variables in the controller. The template loops the variables and does an Ajax request for each required widget, pushing them into a div..
There must be a better way…
Example: http://dev.banelingnest.com/
Now i have looked at HMVC and i cant wrap my head around it. It rather seems bloated for my limited requirements. Does anyone have a nice and simple way of achieving this?
Thanks in advance..
Korhal
As it's most simple, set a variable to do whatever in the controller, then just send that as one of the variables to the view...
Controller:
$data['widget'] = $this->show_widget('time'); // widget output
$this->load->view('sidebar', $data);
Sidebar view:
<div class="sidebar">
<?php if (isset($widget)) echo $widget; ?>
</div>

Categories