• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question

#1
[eluser]CheekyGeek[/eluser]
Greetings all.
CI newbie, just hatched. Working my way through the User Guide and following along with the two CI video tutorials. I'm having some trouble translating my vision into the CI/MVC way.

For example, lets take a simple "users" database table that will be under an Admin's control. We will want to be able to:
-list all users
-add a user
-update a user record (change the contents of some field)
-delete a user

So am I going to want to create an Admin controller that contains all of the following?
-function list_all_users
-function add_user
-function update_user
-function delete_user

OR

am I going to want separate controllers for each of those functions?

Or should I think not in terms of access (like Admin) but in terms of units (like "user"). If I had a controller named "user" then I could have functions:
-list
-new
-update
-delete
???
I guess I'll get to how to restrict access to those functions/forms as I go along here.

And if a function/controller "user/new and load a "new_user" (view) do I have the form action submit back to the user/new function/controller or use a different one, like user/add?

Just putting these questions down on paper has helped me a little, but if anyone can tell me if I'm on the right track (or off in the weeds already) would be greatly appreciated. Thanks in advance!

#2
[eluser]gon[/eluser]
Hi,

You should create a controller and a model for Users. Note that the model will probably be used in all the app, while the controller will be part of the admin section.

In the Users controller you would put a function for every operation:

- index (for a list of users)
- edit
- add
- delete

and so on.

The controller will validate the params, execute the corresponding model method, and show the result to the user.

Of course, some of the controller functions must take parameters from the url, while others not. For example you would take the user id when editing:

/users/edit/14

This way you can check if there is a user with ID=14 at the DB. If not, you could send a 404 or redirect to index.


To restrict access to controllers, I do this:

Make a base_controller. All restricted controllers will extend this one.

At the constructor, one of the things I do is to check if the user is logged, by loading an Auth class that when loaded checks the session cookie. It also loads the user data and checks what permissions or roles the user has.

At the base_controller, I write a _remap function, which, as the docs explain, is called before the controller action.
In this function I check what controller and action is being called by looking at the URI segments.
Then you check if the user has permissions for executing the action on the controller. In my case I have written a ACL library for doing this.

This is the _remap function that I use:

Code:
class Base_clovercms_controller extends Controller {
        
        // here goes var initializations...

        function __construct() {
            
             ......
             $this->load->library("auth");
              ......
        }

        function _remap($action) {
        
        if (!method_exists($this, $action))
            show_404();
        
        $segments = $this->uri->segment_array();
        if (count($segments)<2)
            show_404();
        
        $controller = $segments[2];
            
        $args = array_slice($segments, 3);
        
        $this->current_controller = $controller;
        
        // these can be overridden by some controllers
        $this->current_menu_section = $controller;
        $this->current_menu_action = $action;
        $this->current_menu_arg = isset($args[0]) ? $args[0]:"";
        
        $this->current_action = $action;
        $this->args = $args;
        
        $authorization = $this->cl_acl->getAuthorization($controller, $action);                
        if (!$authorization) {            
            show_404();
        }
        
        if (method_exists($this, $controller."_init")) {
            call_user_func_array(array($this, $controller."_init"), array($action, $args));
        }
                
        call_user_func_array(array($this, $action), $args);
        
        
    }
}


You can see how the _remap function checks the user permissions by calling $this->cl_acl->getAuthorization($controller, $action);. You don't need to pass the user because cl_acl library will get it from Auth library.

If the user has permission, the action is executed.


Hope this helps!!!

#3
[eluser]CheekyGeek[/eluser]
That helps a GREAT DEAL. Thank you. I was up late last night because I finally started making some progress. I have a MySQL "users" table populated with three records to work with. I had come to the conclusion that I needed a User controller and successfully used scaffolding with it and loaded two helpers (url, form). I also created the index function with uses a query and calls a view that lists all users in a table. The last table cell contains a link that calls the individual user's info by passing the user id as the third URI parameter. I'm working on that function now, which I call user/detail.

My detail_view will be a form with the values prepopulated from the database. Both the "all_view" (called by user/index) and the detail_view (called by user/detail) will also contain a link (or button) to call user/new, which will use the same form as the detail_view, except with no prepopulated values. So I'm at the point of figuring out how to get those two views written now. (Probably more questions on that to follow, if I get stuck!)

I also see that I need to make my life a little easier and figure out the model part of the equation here. However, I think that from learning-concepts standpoint, it will be beneficial to get it working the hard way with just a controller and a view and then create a user2 controller that makes use of a model instead. That way I will (hopefully) better understand the concepts.

I'm pretty new to this whole development thing. While I had basic programming concepts classes years ago, my main experience has been modifying a pretty big PHP program that was given to us. Some of the main PHP pages would print out to 54 pages long. (Not exactly using OOP). But I learned a lot. Enough to recognize the benefits of learning a system like CodeIgniter!

#4
[eluser]gon[/eluser]
Glad I could help.

Please do use a model since the beginning. I think models are covered in the video that explains how to make a blog. You should check it out if you didn't already. This is quite basic and simple and will get you in the right direction.

I agree on that 50-page long script didn't help you in learning to organize your code.

You should read about MVC to learn why things are done the way they are. Codeigniter is an implementation of this paradigm.

Good luck


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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