Jump to content

Question regrading mvc, model and controller!


darksniperx

Recommended Posts

I have done mvc few years back, but forgot most of it.

 

I am trying out mvc which i have found on the following link: http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

 

when i need to access functions of model from controller class. How would i do that?

 

I have home controller and home model.

 

model has a function doThis(){...};

 

How do i execute that function from controller class?

 

 

Link to comment
Share on other sites

I didn't look at the tutorial but you would simply make sure that the Model class is available (included) and then instantiate it as per usual. You might even make a helper method within your base controller that handles this for you.

Link to comment
Share on other sites

here is what i did:

 

<?php
include('home.php');
class HomeController extends Controller {
$model = new Home();

function index($id = null,$name = null) {

	$this->set('title',$name.' - First Test page');
	$model->printText('123');
}
}

 

I get the following:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /usr/local/www/vhosts/htdocs/application/controllers/homecontroller.php on line 4
Link to comment
Share on other sites

Here is what i did:

 

<?php
class HomeController extends Controller {

function __construct() { 
	$model = new Home();
} 

function index($id = null,$name = null) {

	$this->set('title',$name.' - First Test page');
	//$this->set('todo',$this->Item->select($id));
	$model->printText('123');
}
}

 

Here is what it tells me only when i add __construct:

Fatal error: Call to a member function set() on a non-object in /usr/local/www/vhosts/htdocs/library/controller.class.php on line 21

 

controller.class.php

<?php
class Controller {

protected $_model;
protected $_controller;
protected $_action;
protected $_template;

function __construct($model, $controller, $action) {

	$this->_controller = $controller;
	$this->_action = $action;
	$this->_model = $model;

	$this->$model =& new $model;
	$this->_template =& new Template($controller,$action);

}

function set($name,$value) {
#this line#		$this->_template->set($name,$value);
}

function __destruct() {
		$this->_template->render();
}

}

Link to comment
Share on other sites

You might have read up on some basic OOP before simply copying and pasting code from the tutorial you've linked to. See oop5.

 

In fact, even some simple tutorials on functions and variable scope would come in handy for you. Variables defined within a function are not available outside of that function.

 

The base controller you just posted requires that the model, controller and action are passed into the __construct as params. This is a floored design IMO.

Link to comment
Share on other sites

$this refers to the current object.

 

$Home is an instance variable in that object, which is also in this case an object.

 

printText() is a method defined in the class of the object $Home contains.

 

I'm not sure why you arrived at this code or where you are using it -- but there are a often a number of ways to accomplish the same things in Oop.  Judging from some of your prior code snippets, I guess that Home is an object of class Home() whatever that is.

 

 

Link to comment
Share on other sites

Yes, Home is object of class home. The MVC that i am using, there isnt much documentation explaining every little detail.

 

I searched though codes from various tutorials from the same author of MVC until l i came upon that line. And when i modified to work with my model and function it work.

 

And it also did not required for me to do any includes.

 

The main issues as i have stated above, i haven't touched php for a couple of years. Thats why it is a little slow for me at this time.

Link to comment
Share on other sites

The main issues as i have stated above, i haven't touched php for a couple of years. Thats why it is a little slow for me at this time.

 

This is indeed why I suggested reading up on some of the basics first.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.