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.
Related
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
As I understand it the "MVC" came before the web and is often used in Desktop software for example.
If I understand correctly, the Controller runs at the time that the user will click a button and this will trigger an action.
But if we talk about web pages, the scenario is a little different, assuming the user clicks a link then it triggers an action.
But then, came me the doubt, is the home page part of a Controller?
What I mean is, the homepage is not usually performed by a user action within the website is just the home, but noticed that many PHP frameworks use a Controller for the home, is that correct?
Another doubt is in my "home" I have several items, for example:
banners
featured posts
recent posts
Each of these items would have a different Model, can I call more than one Model and a View into the Controller?
Would these be the correct steps? Or most of php frameworks are not strict?
You are completely right and I understand the confusion.
In a pure MVC approach, the Controller only listens to user actions and updates the Model accordingly. The Model then notifies the View (through the Observer design pattern) and the View updates itself, accessing the data it needs from the Model.
This is how it was done before the web, in desktop application, where the Model and the View are running concurrently, and where the Model can notify the View. The Controller doesn't set up the View.
In this pure MVC model, the example you mentioned about the homepage doesn't need a Controller indeed. The View would just render itself, accessing the data it needs through the Model. Controllers are very thin and only listen to user actions and update the Model. That's all they do.
In the web, this model is not possible, and thus there are alternative approaches. The Model can't notify the View as in the pure model, and thus what popular frameworks require you to do, is to set up the View in your Controller. That's why for your example of the homepage you need a Controller, as you will set up your view there with all the data it requires.
However, there is another approach I have been personally writing about, where Controllers don't set up the View which allow you to keep tiny Controllers. You can read about it here: A better OOP approach to building MVC applications
Thus I believe that the homepage shouldn't need a controller action at all. The View should just render itself and access the data it needs to render. The Controller's role is to handle user actions and update the model accordingly, as you said. If it does more than that, it probably violates SRP. The Controller shouldn't set up the View, instead, the View should get its own data from the Model.
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.
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)
I'm a total beginner to CakePHP, and I'm trying to build a pretty basic website here that has a menu + some UI stuff like a footer that will persist through all the pages.
The trouble that I'm having right now is that in my controller, I have several different functions corresponding to the website's menu options that grabs data from database, but obviously I wouldn't want to create the same number of views since every page has the exact same setup.
So my question is what's the standard or best practice to achieve this? I read up on elements and am still kinda confused as to how this would be done; how would I keep track of which page is the user currently browsing? And if they click on a menu option, how would it be coded so it takes them from "blah.com/home" to "blah.com/contact"?
I know my question is kinda long and noobish but I'd really appreciate it if I could get some help in beginning CakePHP.
For functions corresponding to the website's menu options that grabs data from database, put these in beforefilter() of the App Controller and use $this->set to set variables and make element for menu which you can call in your layout. You can set the layout in your controllers like var $layout=''.