I just started to use codeigniter. I need to know the explanation of
Views are never called directly.
Is it mean that i can not use $this->load->view('My_view') into another view?
I created a project in core php and decided to convert into codeigniter. In my project a page has different section so i created a main file recipes.php in views. I also created a folder where i put different section files to include in recipes.php. In my controller i loaded recipe view, it showed the recipe page then inside recipes view i used $this->load->('categorymenu'). It worked fine. I didnt pass any data since this section contains simple html. But im confused that it is not a right way of loading views.
So please can someone explain in detail. Am i doing the right thing or is there another way of doing it.
I also loaded the view in controller and passed to main view in controller which also worked perfect. But as i mentioned im not sure which is the right approach. My apology if this is a stupid question.
As you are naive to Codeigniter no question asked by you is stupid.
Now the answer of your question is that,
You can load a view inside another view and it is absolutely acceptable as there will be some cases when you have to put header in all pages of the web site, that time you have to load the view in another view.
Second is that the correct way to load the view is in the controller. As the controller is the mediator between the model and view. if you want some data of database to be displayed in your page you have to get that data through the function created in the model and then after you can load the view in controller and can pass the data in that.
This is code of controller
public function temp()
{
$data['recs']=$this->my_model->my_function(); //getting data to pass in view
$this->load->view('my_view',$data); //load the view
}
Related
I'm trying to implement mvc design pattern to my existing dating-web-app project. I believe I understood the concepts and created a login model and controller without any problems. Basically, I have a base controller which initiates a view object on construct. Than I use that object to send information to the view from within the controllers as method parameters. However, I couldn't decide how to send variables to the top menu view. Since this menu displays dynamic information (request count, notification count,etc) and included in every page of the application, only two options come to my mind:
Send information to the view from every controller, which seemed like reuse of code.
Put a seperate action in the base controller and make it called automatically when other controllers extend base controller.
Which one should I choose? Or, is there a better way to do this?
I can't figure out how to solve this problem i'm facing. Alright, so basically i have a default view/site/index.php view file which is generating the home page. Now i have built two separated Models and their associated Controllers and views. I want to include those view of the controllers in the default home page view i.e. views/site/index.php. Does it make any sense ?
So my home page is divided into several sections. The header and footer parts are the same throughout the whole site, which is why i've written them in the /themes/brushed/layouts folder under separate files. Those are being loaded fine.
The two controller i was talking about are named as FrontPageCategoriesController.php and TopProductsController.php. They are generating their separate view files i.e. index.php in their respective view folders.
How will i include those view files (index.php) into the main site view file (views/site/index.php). I just can't include those files using include_once() because the data is being generated yet by its controller. So whats the way to achieve this ?
Can anyone please help me out with this. Thanks in advance.
First you should move data generation from controller to model. Controller should only wire your input parameters to model and pass it to view. The reason is that you want to reuse models, not controllers. Remember that controller is bound to page request, while model is not.
Then just use your model to fetch the data and renderPartial to render the data. For example:
$categories = FrontPageCategories::model()->findAll();
$this->renderPartial('/frontPageCategories/index', array(
'categories' => $categories,
));
In fact it is possible to fetch categories from model directly in view, if they are not dependant on some parameters in the current page.
Actually, you should use renderPartial for your header and footer too.
I have in my index a place where I want to include a search form. Thing is this 'search form' needs to be in different sections of the site, so what I did was to create a controller and model for search form view. Now my problem is I don't know how to include it in my index (or in other parts of the site).
I already know it goes against the MVC pattern to load a controller from a view, so I stopped looking for that. I've done some research and the most common answer seems to be to 'reuse the model' but I'm not sure what that means. Should I copy the functions in the controller of the search form and include them in the index controller?
First of all, I think you should take a look at the template method, if you're not already using it.
http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html
This enables you to make custom templates and you can decide in your controller whether you want the search form in it.
If you'd rather do this in the view itself, you could try using this in your view
include(searchform.php)
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.
I have an application that when accessed its the main.php controller, which spits out the main_view.php view. Now I was wondering what is the right way to go from this view to another controller to generate the next view. The CI tutorials just talk about going from controller to view not the other way around. I have a form that I need to submit on main_view.php and then generate the next view from a database based on that info.
thnks!
You target controllers and functions in the url like this yoursite.com/controller_name/function_name. so the form on main_view.php can link to any controller/function you want, ie yoursite.com/form_controller/form_processing_function
Use site_url to ensure portability of your application:
echo site_url("controller/method");
See http://ellislab.com/codeigniter/user-guide/general/helpers.html for more info.
Read this chapter in the user guide:
http://codeigniter.com/user_guide/libraries/form_validation.html