• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Application design

#1
[eluser]VirtualDevel[/eluser]
First off, thanks for the time out to take a look at this. I'm working on a project that will have some basic objects, users, groups, etc. My thoughts are to create class or libraries for each of these. I have begun with a User class for basic user related tasks, login, logout, check if exists, update and such. I have an admin controller which loads the user class and through this handles such requests. I am about to begin one for the groups. Now, this is where I'm a bit confused, is this an incorrect way of going about such things? Should I instead create a new controller for each item? I feel as if I am in a way breaking the use of the framework as I am writing seperate classes and using one controller for most of the tasks. Thanks.

#2
[eluser]VirtualDevel[/eluser]
bump..anyone?

#3
[eluser]stuffradio[/eluser]
You waited an hour and bumped your post? Tsk tsk Smile

You should try and separate things as much as possible. If you want to handle sessions like login/logout, maybe have a controller that's called sessions. If you want to have a registration page maybe do a controller called register or registration.

Don't stick all things in one controller as it's less efficient I think. Don't have one model for everything. Maybe make a model for each table, or do a model for different components of your site.

Models: Register -> checkUser()//This would check if users exists
-> checkInput()//This could make sure all input from the form is valid
Login -> checkSession()//Checks if the user is logged in already

Those are examples of how you could organize your application.

#4
[eluser]VirtualDevel[/eluser]
Right, but, in my case, I am using classes to handle much of the data manipulation not controllers.

Admin (controller)
User (class - library)
Group (class - library)

So, in the admin controller it looks like:

Code:
class Admin extends Controller {

    $this->load->library('user');
    $this->load->library('group');

    // do something
    $this->user->login($username, $password);

    $this->group->addUser($user_id, $group_id);

}

I am not using different controllers, but classes, is this bad practice?

#5
[eluser]VirtualDevel[/eluser]
Anyone? Im sorry to keep bumping but I am on a tight deadline, as I am sure most of u r Wink Thanks again...

#6
[eluser]Michael Wales[/eluser]
Models are really where data manipulation should be taking place.

Also, quit bumping your posts - it's annoying and not an acceptable practice on these forums.

#7
[eluser]matthewr[/eluser]
Hi VirtualDevel, creating classes is good practice if you're not using a framework. Frameworks exist to solve these issues because creating these classes are time-consuming.

You need to read page one of the codeigniter manual, which explains MVC. There you would know where to put what.

Controllers - Business Logic
Models - Data Manipulation and DB access
Views - Data display.

Speeding through the manual might seem a better idea when you lack time, but more often than not you just end up wasting more time because you find yourself having to redo everything you did wrong.

While waiting for forum replies read the manual! The answer to your question is in the manual. If there something in the manual you don't understand, feel free to post.

#8
[eluser]VirtualDevel[/eluser]
So, then, if lets say I wanted to call updatePassword which is defined in the model. I would then need to pass it a param, for example, user_id for the user I wanted to update the password.
Code:
$this->user_model->updatePassword($user_id);

What I want to do, and this is why I was thinking classes, is as follows. Anything wrong with this.. Sorry a bit new to the CodeIgniter world.
Code:
// call setter
$this->user_model->setUserID($user_id)
$this->user_model->updatePassword();

#9
[eluser]Michael Wales[/eluser]
No - you can do exactly what you wanted to do. The only thing you have to watch out for is when calling $this->load->model it instantiates the object as $this->model - so you are limited to only one instance of that class using the CI super object.

Of course, you can always create another instance after the model has been loaded though:

Code:
$this->load->model('user');
$this->user->setUserID($user_id);
$this->user->setPassword($new_password);
if ($this->user->save()) {
  echo 'Be awesome.';
}


$u = new User();
$u->setUserID($user_id);
$u->setPassword($new_password);
if ($u->save()) {
  echo 'Be awesome';
}

#10
[eluser]VirtualDevel[/eluser]
Thanks for the quick response! Appreciated Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.