What is the best way to switch to MVC coding?

About 5 months have passed since I took the PHP book and started coding in PHP. First, I created all my sites without any organizational plan or MVC. I soon learned that it was a pain. Then I started reading on stackoverflow on how to separate php and html and what I have been doing since.

Ex: profile.php <--this file is HTML,css. I just echo the functions here. profile_functions.php <--this file is mostly PHP. has the functions. 

This is how I still separated all my encodings, and now I feel like I have to move on and run MVC. But the problem is that I have never used classes and suck with them before. And since MVC (such as cakephp and codeigniter) are all classes, this may not be good.

My question is: Are there any good books / sites / articles that tell you how to write code in MVC? I'm looking for beginners of beginner books :) I just started reading the codeigniter manuel , and I think I'm going to use this.

EDIT: Is it possible to have an MVC organization structure for your encoding without using cake, coderigrin, etc.? Basically just split the profile.php file into 3 different files (view, controller, model)

+6
php frameworks model-view-controller
source share
5 answers

to answer your question

Is it possible to have an MVC organizational structure for your coding without using cake, codeigniter, etc.? Basically just separate say profile.php in 3 different files (view, controller, model)

absolutely...

first profile.php file (view that gets to the browser)

 <?php include( 'controllers/UsersController.php' ); $controller = new UsersController(); $controller->profile(); $pageData = $controller->data; ?> 

controller

 <?php include 'models/UsersModel.php'; class UsersController{ public $data; public $model; public function __construct(){ $this->model = new UserModel(); } public function profile(){ $this->data = $this->model->findUser(); } } 

model

 <?php class UsersModel{ public function __constuct(){ // connect to your db or whatever you need to do } public function findUser(){ return mysql_query( "SELECT * FROM users WHERE users.id = 2 LIMIT 1" ); } } 
+1
source share

MVC is just a design pattern. This is not exactly what you can โ€œencodeโ€.

If you like the code in PHP, here is an article on MVC in PHP. It provides an overview explaining the design pattern, and then gives an example.

+1
source share

As I learned after completing this lesson:
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/

The focus is on learning about the Symfony Framework, but by default you'll be familiar with the good principles of MVC.

+1
source share

This is not PHP, but see if you can get a copy of the Tate Bitter Java. He will discuss the organizational side of things (how and why the organizational code improves the material).

I hesitate a bit to recommend one of the great Java books for PHP programming, but this book is one of the few that starts with code written without an organizational plan and improves it into the MVC structure without using third-party libraries. Thus, he teaches you what organization is from a practical point of view. Hopefully when you understand the template, it will not be too difficult to translate ideas into PHP.

Another alternative is to grab one of the dozens of PHP frameworks and transcode into the framework. This will greatly speed up your results, but the disadvantage is that you are likely to understand these results in more detail, and there is a small chance that your code will not behave as soon as you rewrite it from scratch. We all like to think that the new material will do everything that the old material did, but often we forget something (or it behaves differently).

+1
source share

MVC is a โ€œgenericโ€ design pattern that does not apply to any language. More coding philosophy. At the most basic level, it simply separates data from business logic from presentation. The following is a simple example of a "template" system using MVC. You could swap any part without breaking anything and the data is not tied to formatting / display. This is sample code, inefficient.

Model get data:

 function getName($id) { $name = array('_first_'=>'Joe', '_last_'=>'Smith', '_mi_'=>'C'); return $name } 

The controller processes it:

 $name = getName(1); $name['_fullname_'] = $name['_first_'].' '.$name['_mi_'].'. '.$name['_last_']; outputView($name); 

View, display content:

 // Example html file: <b>Hello _fullname_, how are you</b> function outputView($view, $data) { switch ($view) { case 'xml': $out = file_get_contents('view.xml'); case 'html': $out = file_get_contents('view.html'); case 'json': $out = file_get_contents('view.json'); } $search_for = array_keys($data); $replace_with = $data; echo str_replace($search_for, $replace_with, $out); } 
+1
source share

All Articles