View Single Post
Old 03-17-2009, 06:57 PM   #1 (permalink)
triumvirat
The Wanderer
 
triumvirat's Avatar
 
Join Date: Dec 2008
Location: Russia, Moscow
Posts: 14
Thanks: 0
triumvirat is on a distinguished road
Default Help with Controllers

Hi.

I don't understand, how true make controllers in MVC model.

My current realization (reduced):

PHP Code:
// User Controller - delete, save and edit user data
// Each method performs certain actions and generates a variable to "View", 
// which are used in the templates of specific pages.
class User_Controller
{
    
// main method. 
    // this method execute all others methods of this controller
    
public function run($method)
    {
        
// if in request exists key id_user, 
        // get user data from user_mapper class
        
if ($id $this->request->get('id_user''int'))
        {
            
$this->user $user_mapper->findById($id);
        }
        
        
$this->$method();
    }
    
    
// edit and add user in database 
    
public function edit()
    {
        
// if POST method (save or edit user data)
        
if (POST)
        {
            
$this->user $user_mapper->createObjectByArray($this->request->getData('post'));
            
            
// check errors .... 
            
$errors;
            
            if (!
$errors)
            {
                
$user_mapper->save($this->user);
                
header('Location: ....');
                exit;
            }
            
            
// set view error data as array-vars 
            
$this->view->errors $errors;
        }
        
        
// set view user data as array-vars 
        
$this->view->user $this->user->getDataAsArray();
    }
    
    
// view user data 
    
public function view()
    {
        
$this->view->user $this->user->getDataAsArray();

        
// important! here other vars go to "View"-component 
        
$this->view->other_vars $this->other_method(); // this method run mysql or other operations with systems resourse
    
}
    
    
// delete user from database
    
public function delete()
    {
        
$user_mapper->delete($this->user);
    }
    
    
// and etc...

Front Controller activate this class:

PHP Code:
$c = new User_Controller();
$c->run($method_name); 
Problem.
In method User_Controller::view() we are see this code:

PHP Code:
// important! here other vars go to "View"-component 
$this->view->other_vars $this->other_method(); // this method run mysql or other operations with systems resourse 
this code make operations, specific for page /user/view/id125.html

If i have use class and method User_Controller::view() in other Controller, then I have to running and code

PHP Code:
// important! here other vars go to "View"-component 
$this->view->other_vars $this->other_method(); // this method run mysql or other operations with systems resourse 
but this code is run I do not need.

I don `t know, how to make
triumvirat is offline  
Reply With Quote