Jump to content

Simple MVC tutorial


VelvetMirror

Recommended Posts

So I've been reading and reading MVC examples for the last few days and I still can't get a grip on how to do it right (without using frameworks).This is the best tutorial I found so far: http://www.phpro.org/tutorials/Model-View-...roller-MVC.html (it also includes a zip with the code)But I'm unnable to try the blog section that includes, if I type localhost/mvc it shows the expected index, but if I type localhost/blog or localhost/index.php?rt=blog it doesn't find the page. So if I can't success in running the sample section (blog), how can I write my own and expect to make it work?Hopefully someone can give it a try and tell me what's wrong, I would truly appreciate it.Also if someone has a recommendation of a good tutorial or book for MVC in PHP, I'm all ears.

Link to comment
Share on other sites

  • 4 weeks later...
Just add more methods in the controller class and remember to create views associated with each method.
The problem is that when you create a new controller in the tinyMvc.php file:
<?phprequire 'load.php';require 'model.php';require 'controller.php';new Controller();

The __construct() method is called in the controller.php file:

class Controller {	public $load;	public $model;	function __construct() {		$this->load = new Load();		$this->model = new Model();		// Set what page you're on		$this->home();	}	function home() {		$data = $this->model->get_index();		$this->load->view('index.php', $data);	}}

In which you have to set the webpage you're on in order to load the view.So if you create another page which also loads TinyMvc.php, it would still load the index.php view.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...