PHP with OOP and MVC - php

Using PHP 5.x
Question, lets say I want to display results from a database. Lets say the last 30 stories from the database. Using OOP and MVC would the ideal setup be you have a class that connects to the db, a class that queries the database to get the information then a class that handles the display of the results and then a page that puts it all together?
So basically I would pass the db connection object to the class that gets me the story results from the database Then pass the story class object to the display class to build the view then pass that back to the view itself and echo out the value? Hopefully this makes sense, just trying to see if Im understanding this:
dbconnect_class.php
storyresults_class.php
storydisplay_class.php
Include all 3 into a page such as display_stories.php, which I believe is the controller in mvc, then run the code and display it in the view which would be in its basic form an include to a template file.

MVC meaning Model -> View -> Controller. In most of the frameworks the request comes to the Controller, the Controller asks the Model to retrieve the database results then the same Controller sends the results to the view in order to display them so that's the basic breakdown of a MVC application.

Don't pass the results object around. That will mean the display class needs to know the implementation within that class.
What you can do is let the controller get the results then pass the results to the display class.
You can make a data class that keeps the data in a suitable object which you can then pass to the display class.
However, this is just some general oop advice but for better mvc, you need to read on that. Some links have been provided by others.

Unless your using a template engine it would be very difficult to not write php code in a view file. At the company I work for, we have developed a set of UI components that allow us to bind data to a component in the controller and have that component render on the view, similary to how asp.net controls work.

Related

Codeigniter writing a menu constructor

I've only just started learning CodeIgniter, so please excuse any misunderstandings I have.
I understand that the controller calls models and views. Views contain the HTML, models contain the database functionality etc.
I don't want to have to write code out to create a header menu, footer menu, side menu etc, in each of the controllers that import the menu structure into the view, so after some googling, it would look like it would be best to write a library in which my controllers can call to retrieve the type of menu they want.
My question is:
Would I place my database queries for retrieving the data to build each menu in the library itself, or would I delegate it to a model and call the model from the library? For the menu construction, I don't need to write to the database, just read the data it contains.
Many thanks
I never put any query directly in the library, as I believe that goes against the MVC that CodeIgniter intended.
I would aim to always make functions for simple database queries in the model, so that you may re-use them in various other portions. This allows you to return the data from what you need via the database when you require it. Which in turn allows you, in the library, to add additional caching or manipulate the data as it comes from the model.
I based that from 3 years of working in CI, and this little picture on this page: http://ellislab.com/codeigniter/user-guide/overview/appflow.html

Code Igniter: returning data from DB, where to put this parsing/output logic

I'm returning a list of people from MySQL
So I have a model, a controller and a view.
As I'm outputting the list of people on a page, I need to check the length of the person's name and dynamically add a class to the that it's displayed in.
Before this was CI, I created a simple function called 'name_class' and put that in a common.php include. On the page, as I was outputting people's names, I'd pass the name to that and it would echo a class on the if the name was over a certain length.
In code igniter, where is the best place to put this function? I thought perhaps as a private function on the controller but then, can I access that from the view?
It seems more logical to me that this belongs as a function within your view. If what your doing has something to do with how you are displaying the data, then it should be in the view layer of your code.
Though CodeIgniter is geared towards web applications, you should always consider the fact that the MVC design pattern is not specific to web pages/sites. The view layer of MVC is used to change how the data provided by the model is displayed or provided to the UI or client. You should attempt to put any client or UI based information or logic into the view and keep it hidden from the Model and Controller layers.
For example, it looks like your view is outputting the HTML directly which is consumed by the client side browser to display. Here is makes a lot of sense to output HTML, and provide the CSS classes needed based on the data provided by the Model. Now, consider what would need to change within your application if you decided to use a client library that accepts your data in JSON format and displays it. If your application's architecture is correct, you should only need to change the View to provide the information in JSON format and you shouldn't be leaving view specific code (i.e. your class logic) in another layer.
Not as a private function on the controller, but as a public function on the controller. If you created a function like this:
public function _test(){
echo "HERE";
}
then you could access it in the view with get_instance()->_test();. Make sure you begin the function name with an underscore to make sure it is not executable from the browser.
EDIT: you could also use a helper as documented at http://codeigniter.com/user_guide/general/helpers.html.
It definitely goes in the view, or if you can make the function useful enough to be used for more than just this one instance, put it in a helper - but still only call it in the view.
The view is your output. HTML belongs in the view whenever possible. class is HTML, so naturally you want to restrict it to your view.
Think of it like this (though this is a broad generalization):
Controller = Input
Model = Processing
View = Output
There is also no good way to access Controller functions directly from the view, you'd have to run it in your controller and pass it as data to the view.

Beginner CodeIgniter concepts - Reusable view code, where to go? (Helper?)

I am a beginner with CodeIgniter still struggling to get a complete grasp on how to use the MVC ideology most cleanly.
I am writing a basic CMS system with the ability to vote on entries and follow people etc, consequently, I have found myself using the same or similar pieces of code across multiple views here and there consisting of various pieces of html and logic such as:
Voting panel
Follow/Unfollow panel
Login/Logout panel
Code to check if a user is logged in etc...
I am wondering where to put this code so it can be unified? I am thinking a helper is the way to go? If I declare the helper in the controller, it can be called from the corresponding view right?
Some of the elements are dynamic - such as a follow/unfollow button - It would need to check if you are already following the user or not and display the appropriate button, which would require a model to check. What I have now is that all the logic is in the controller and it returns an appropriate button, but it seems weird to be returning formed html code in a controller return as well. Should it be more like:
controller checks if you are following someone
the controller passes a boolean to the view
the view calls the helper with this value to draw the appropriate button
Also, as a secondary question, I have been doing a fair bit of looping through mysql arrays in foreach loops to process mysql results returned from the view. It seems like my views are getting somewhat complicated, but I can't think of another way to do it, although perhaps this should be done in another helper as well?
Apologies if this is a naive or repetitive question, there is indeed a lot of discussion surrounding this subject but it is not always easily relatable to another project.
Helpers are certainly one way to modularize anything that isn't DRY. Another is to use Partial Views. CodeIgniter looks like it supports partial views. Here's a good breakdown - not PHP specific but the discussion should be agnostic.
As far as handling user logins is concerned, you will probably want to use a static class and the singleton design pattern, which will allow you to check to see if a particular user is logged in or not anywhere in your application. There is a good tutorial here
http://www.phpandstuff.com/articles/codeigniter-doctrine-scratch-day-4-user-login
Loading the helper, I don't believe loading it in your controller will automatically load it in your view. I think you have to re load the helper in your view file, or you have to autoload the helper. (cant remember off top of head but Im pretty sure).
Regarding looping through the mysql results, you should be using a model for this, always. Any functions which are grabbing or sorting information from your applicaiton, should be done within the model. Then, in your view file you loop through the results and format the data how you choose to.
When developing http://newspapair.com which has the vote functionality you mentioned I used helpers and custom classes to spread the functionality across multiple views.
Helper - has functions without a class. So a standalone function or group of functions can be placed in a file and saved as a helper.
For instance I used a helper with generic form processing functions for NewsPapair, instead of a static class. But this is not the "best practices" thing to do. I did it this way because I already had the functions from a previous project.
As far a looping through MySQL results, try to write a query that allows the DB Server to do the heavy lifting. This will make your code more efficient. Perhaps ask a question about a specific query with example code. Plus do all of the data gathering in your Model.

model access from layout file in CakePHP

I am working on a CMS using CakePHP and I want to create a dynamic menu which is the same on all the pages the user can access. So I figured out to create in the layout (since it shared among so many pages and view) but I don't seem to know how to acces the model and get the data from the database to construct the menu. any help is appreciated.
That's because for proper MVC separation* in Cake you're not supposed to access the Model from the View. The only part with access to data should be the Controller (via the Model), which hands it down to the View, which simply displays the data.
As such, using a beforeFilter callback in your global AppController to set() the data is probably the best choice.
In emergencies you can always access anything from anywhere by loading an instance of the needed Class using ClassRegistry::init, but you really shouldn't.
* Actually, in "proper MVC" there's no problem with the View getting data directly from the model. You shouldn't do that in templates necessarily, but View related code can well get data from the model to visualise the model state. It just doesn't really work that way in Cake, because Cake is not proper MVC and default Cake views are only templates.
An alternative could be requestAction, it allows you to call controller actions from views/layouts, and in those actions you can then access the desired model(s).

Zend Framework: right way to retrieve data from database

I am working on a project with zend framework and i need your advise on the right way to fetch data from the database.
Am making use of Zend_Layout to load my template. The appropriate view is then loaded into the template.
On the template, there is supposed to be a section that displays data from the database (e.g Categories). Since i am using one template, the data will be displayed on every page requested irrespective of the controller or action called.
I know its not a good practise to fetch the data from the template and it wouldn't be a good idea to fetch the data from each action executed. I dont know if the right thing to do is to use helpers to fetch the data from the database, but wouldn't that go against the whole idea of MVC.
You haven't mentioned the option of using a Model class to fetch the data. That's the "M" in MVC. :-)
A Model class is one that has an interface the View can use to request specific fragments of data. Inside the Model class, it may use a mix of using Zend_Db_Table methods, and also custom SQL queries (executed directly through Zend_Db_Adapter's query() method). Whatever works to get the data.
The point is that a Model encapsulates all the logic needed to supply data in a format the View can use.
See also
"An Introduction to Domain-Driven Design" at Microsoft.
"Domain-Driven Design" by Eric Evans.

Categories