Page 1 of 1

My MVC

Posted: Mon Apr 25, 2011 11:50 am
by ChrisBull
O.K So I got part of the way through developing a site/application that has lots of classes and pages when i decided it was too complicated as it is and have decided to start again from scratch and create a mvc framework for it to run with. My problem is that there are very few tutorials out there for php that explain how to use it.
I have created a small mvc framework from a tutorial on net.tutsplus - this one http://net.tutsplus.com/tutorials/php/c ... -with-php/

BUT its as it says 'tiny' it only has one model and controller and view and doesn't really do anything. Nevertheless it gave me a solid introduction to how basic mvc works.
I just need some clarification at the moment on to where to go now.
What i will end up having is a index page with basic html on and then a user login page which will have a login form on, and after the user has logged in, they will still be on the same login page but, it will have the user login information and details extracted from a database in view.
Now, Should i be creating a .htaccess file that redirects ALL requests to the index page to be sorted? If so what happens then?

Also one thing i don't quite get - When using a MVC framework are ALL of the pages displayed in index.php but it shows different 'views' depending on what the url request was?

Many Thanks for all of your time.
Chris

Re: My MVC

Posted: Mon Apr 25, 2011 12:18 pm
by superdezign
If you think there are very few tutorials, then you didn't look hard enough. The MVC architecture is flexible and widespread. In it's most basic terms, it's "input -> logic -> output" or "request -> process -> response" where the controller takes input/request, the model processes logic, and the view is the output/response.

The controller takes the input in the form of a URL, a query string, POST data etc. It then uses that input to determine which model(s) to call to get the information that it needs. Models generally get this information from your database, but the sources of information can also include your session, GET/POST data, files, etc. After the controller has the information, it can do some last minute processing (which you should avoid, but depending on the way your views are set up you may not have a choice) and then give the data to the view which will format it and display it in a (hopefully) user-friendly manner. This will be the final HTML, XML, JavaScript, image data, etc.

The use of .htaccess is to centralize the requests without using a query string. It just turns example.com/index.php?request=path/to/page to example.com/path/to/page. And yes, generally, you send all requests to a central file (index.php) to be processed. Again, MVC is widely implemented and you can find many resources on it. Don't limit yourself to PHP only resources either. There are many design patterns out there that can be useful such as composite views.

Re: My MVC

Posted: Tue Apr 26, 2011 6:12 am
by ChrisBull
Thanks for your reply.
I have started creating it now and wondered if someone could help with this...
I have created the .htaccess file to redirect all requests to index.php where it is going to get the url and determine which controller to use - So at the moment the possibilities are '/' for index, '/login' for login and '/control' for control. I will then take of the '/' and load the corresponding controller to whatever is left in the variable.

Is this what I should be doing for an MVC framework?

Thanks again for your time
Chris

Re: My MVC

Posted: Tue Apr 26, 2011 6:31 am
by superdezign
Again, there are many different ways. In terms of handling requests, I've personally seen it done 3 ways:
  1. The request defines the controller, the method, and the parameters. So, when you see "example.com/members/view/superdezign", the application calls Controller_Members::view('superdezign').
  2. The controller is determined by all pieces except for the last one, which defines the function to use. So, when you see "example.com/members/view/superdezign", the application calls Controller_Members_View::superdezign(). Of course, the links would be more like "example.com/members/view?user=superdezign" which would be Controller_Members::view() and 'superdezign' would be accessible through the GET data.
  3. The way I used to do it was manually forward requests from one controller to the next. I did this so that every section of the site would share a template which is defined in the controllers before the final controllers. So, "example.com/members/view/superdezign" would go to Controller_Members::view() which would set up the template and count the amount of slash-delimited sections of the request that were left. Then, the function would determine whether it wanted to use the data or forward it using the controller's forward() method. In this example, I'd set up the view() function to use the next part of the request for the username if any existed. If there were no more parts of the request, it'd either load the currently logged in user or redirect to the login page if no user was logged in.
    The style of this code was to set up the handling of requests like train tracks with stations. The trains had to stop at every station to know which way to go from there or whether to stop. This allowed the trains to have the properties of every stop applied to them and avoided writing the same code at multiple stops.
The only requirement of MVC is that the M, V, and C are all separate. Everything else is up for grabs.

Re: My MVC

Posted: Tue Apr 26, 2011 7:13 am
by ChrisBull
Ok - Thanks for your feeback, i think i move on now

Re: My MVC

Posted: Tue Apr 26, 2011 7:33 pm
by josh
ChrisBull wrote:Thanks for your reply.
I have started creating it now and wondered if someone could help with this...
I have created the .htaccess file to redirect all requests to index.php where it is going to get the url and determine which controller to use - So at the moment the possibilities are '/' for index, '/login' for login and '/control' for control. I will then take of the '/' and load the corresponding controller to whatever is left in the variable.

Is this what I should be doing for an MVC framework?

Thanks again for your time
Chris
This is the front controller pattern (using an index.php). MVC is more about having a template file (to be re-used on multiple pages), in other words your view. The separation between model & controller is less important. Start with separating out your display logic from your business logic, the front controller pattern is good too. They are two patterns that go hand in hand, they complement each other and both should be used, they are so commonly used together maybe a new pattern should be created to simplify these discussions.

Re: My MVC

Posted: Wed Apr 27, 2011 1:32 am
by Christopher
josh wrote:MVC is more about having a template file (to be re-used on multiple pages), in other words your view. The separation between model & controller is less important. Start with separating out your display logic from your business logic,
I think you meant to say having a Model class to be re-used on multiple pages. Templates are usually very page specific.

Start out by separating your business logic from I/O logic. Put the business logic in the Model classes, the input logic in the Controller classes and the output logic in the View classes or templates.

And yes, running everything through index.php is implementing a Front Controller.