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)
Related
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
}
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.
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.
it may seem like a dumb question because I feel like it is when asking it but I can't find anything about it in the doc.
I need to put a form in the header of my website that appears in all pages.
I need that form to be able to be submitted in every page (no surprise here).
I can display my form via the layout function of Zend Framework, but I can't find the equivalent for the back-end of the website to be able to treat it at only one place, site-wide.
I thought it would be possible to put the form validation in the bootstrap but it doesn't seem like it.
Did I miss something or do I have to, at least, put the form validation in all init() function of all my controllers ?
take a look at the plugin documentation
You can write a plugin that handles your request. If you need to have your form processed before any other actions take place, I suggest you use the dispatchLoopStartup() hook.
You should avoid predispatch and postdispatch in this case, because those hooks are called multiple times if you proces multiple actions (by using _forward for instance)
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.