Welcome Guest, Not a member yet? Register   Sign In
help me to organize my code without to break mvc pattern rules. code work, help is needed to organize it.
#1

[eluser]zoreli[/eluser]
I am relativly new in mvc pattern, but not that new in php programming. I start to rewrite my own cms with codeigniter and I have some doubts about how to organize my code in proper way, so I need help:

Code works fine, my problem is how to organize it.

Question 1:

In the back office administration system I have different modules like faq, news, products, projects, services, articles etc..

Should I have it all in one controller? I start to put everything in one, but it starts to become huge, and I am thinking now to make separate controller for faq, another for the news module etc.

One more thing that lead me to this decision is that I will not use all modules(FAQ, products, projects...) in every projects, so it sound logically to have one controller for each module than one huge controller for whole back office.

Question is am I breaking the mvc logic with creating more than one controller? What is your opinion?

Question 2:

I have the following model:

Code:
class DeleteRecords extends CI_Model
    {
        
        public function deleteIt($deleteWhat = array())
        {

            /**
            *
            * @author: Zoran Makrevski [ [email protected] ]
            *
            * @rid type int
            * @tname type string
            * @fname type string
            *
            * @rid is actual record id, which must be provided, so the function will
            * know which record to delete
            *
            * @tname is table name
            *
            * @fname is the fieldname, which is used in where comparision
            * example, if the query is delete * from users where id....
            * fname should be equal to id
            *
            **/  
            
            $rid = $deleteWhat['rid'];
            $tname = $deleteWhat['tname'];
            $fname = $deleteWhat['fname'];
            $this->db->where($fname, $rid);
            $this->db->delete($tname);
        } // end of function deleteIt      
    
    }
    /* End of file deleteRecord.php */
    /* Location: application/models/deleteRecord.php */

Here is my controller:

Code:
public function deleteUser()
        {
            $deleteWhat = array(
                'dpage' => $this->input->post('dpage'),
                'tname' => $this->input->post('tname'),
                'fname' => $this->input->post('fname'),
                'rid'   => $this->input->post('rid')
            );
            $this->load->model('deleteRecords');
            $this->deleteRecords->deleteIt($deleteWhat);
            //$this->users();
            redirect($deleteWhat['dpage']);
        } // end of function deleteUser

And here is second controller:

Code:
public function deleteFaqCategory()
        {
            $deleteWhat = array(
                'dpage' => $this->input->post('dpage'),
                'tname' => $this->input->post('tname'),
                'fname' => $this->input->post('fname'),
                'rid'   => $this->input->post('rid')
            );
            $this->load->model('deleteRecords');
            $this->deleteRecords->deleteIt($deleteWhat);
            //$this->users();
            redirect($deleteWhat['dpage']);
        }


Now, the question here is: Am I repeating my self here?

I can use one controller to delete any records from any table and after deleting the record, I can redirect him anywhere I want.

Any advice will be deeply appreciated.

Regards, Zoreli
#2

[eluser]Mauricio de Abreu Antunes[/eluser]
Can you group your all DML statements in your MY_Model? :-)
#3

[eluser]zoreli[/eluser]
I am afraid that my knowledge is not on that level at this time.

I will certainly try that after some months, but for the moment, trying to have something to work with for my customers.

I am following closely the videos on codeigniter.tv and those people are doing great job, but I am not on their level.

My code must be reusable as much as possible, since that will allow me to work faster, therefore offer lowest possible prices.

Sure enough, I am trying to not breach any mvc pattern rules.

Regards, Zoreli

#4

[eluser]Mauricio de Abreu Antunes[/eluser]
https://github.com/jamierumbelow/codeign.../README.md

Read this.
This code is reusable. :-)
And it's coded by:
•Phil Sturgeon
•Dan Horrigan
•Adam Jackett

Take a look at this, please.
I'm using this in my APP and works fine.

*

Think simple.

Code:
class MY_Model extends CI_Model {

//Generic methods for insert
//Generic methods for update
//etc...

}

I guess in your model your codes are not reusable.
Like:

Code:
class User_model extends CI_Model {
  //Constructor and other methods
  //...


  //Methor for insert user in the database
  public function insertUser ($data) {
     return $this->db->save('users', $data);  }
}

And you have other classes doing the same stuff again, again and over and over again... :-)
#5

[eluser]zoreli[/eluser]
Hi Mauricio

I have seen the videos on Codeigniter.tv about my model, but I am afraid I am not on that level yet. Seem hard for me to work with something on daily basis that I do not understand.

Can you please try to answer my 2 questions?

Regards, Zoreli
#6

[eluser]Mauricio de Abreu Antunes[/eluser]
Answer for question 1:

Yes, you need one controller for each page, my friend.

faq.php (controller for Faq_model.php)
about.php (controller for About_model.php)
users.php (controller for Users_model.php)


Answer for question 2:
You, you are repeting yourself. Sad
#7

[eluser]zoreli[/eluser]
Hi to everyone

Thanks for the responses & advices. So I guess my only way is to go with MY_Model solution huh? Gulp... :ohh:
#8

[eluser]Mauricio de Abreu Antunes[/eluser]
I can help you with your MY_Model. Send me a private message. :-)




Theme © iAndrew 2016 - Forum software by © MyBB