Welcome Guest, Not a member yet? Register   Sign In
Controller / View Relationship
#6

(This post was last modified: 04-29-2017, 03:59 AM by PaulD. Edit Reason: tidied it up a bit )

I would avoid though the big fat controller if I were you.

For instance suppose your page has the following options and content:

Code:
Manager Home page
Users
    List users
    Add user
    Edit user
Pages
    List pages
    Add page
    Edit page
Accounts
    List accounts
    Create account
    Edit account
Products
    Add product
    Edit product
    Manage images
Categories
    List categories
    Add category
    Edit category
Orders
    List orders
    Add orders
    Edit orders
Customers
    List customers
    Add customer
    Edit customer

With the page content for manager home page something like
Code:
Graphs
Latest activity
Overdue orders
New orders
Messages
Todo list

I would have the following controllers

Code:
manager (folder)
    Manager_home.php
    Users.php
    Pages.php
    Accounts.php
    Categories.php
    Orders.php
    Customers.php

And view structure

Code:
manager (folder)
    home_view.php
    users (folder)
       list_users_view.php
       add_user_view.php
       edit_user_view.php
    pages (folder)
       list_pages_view.php
       add_page_view.php
       edit_page_view.php
etc

So now, when you need to change the categories page in the manager, you would just be dealing with the categories.php controller, the categories models and libraries, and the page views in the manager/categories folder.

I also (these days) try to keep my controllers as thin as possible so the manager_home.php would look something like:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Test extends CI_Controller {

    public function 
index() {
       
// set variables
       
$page_data = array();
 
       
// load resources
       
$this->load->library('manager_library');
       
$this->load->library('permissions_library');
 
       
// check permissions
       
$this->permissions_library->get_permissions('manager_home');
 
       
// get data
       
$page_data['graph_data'] = $this->manager_library->get_graph_data();
       
$page_data['latest_activity'] = $this->manager_library->get_latest_activity();
       
$page_data['overdue_orders'] = $this->manager_library->get_overdue_orders();
       
$page_data['new_orders'] = $this->manager_library->get_new_orders();
       
$page_data['messages'] = $this->manager_library->get_messages();
       
$page_data['todo_list'] = $this->manager_library->get_todo_list();
 
       
// load views
       
$this->load->view('common/admin_header_view');
       
$this->load->view('manager/home_view');
       
$this->load->view('common/admin_footer_view');
    }


Now all the work and logic is being done in your libraries. Your libraries call models to query the database to get the data, so your libraries can be slim too. You can unit test your libraries quite easily.

Not saying this is the only or best way but is what I would do and it does keep everything very easy to maintain whilst avoiding big fat controllers. So not 1 to 1, nor 1 to many, but 1 to 'some' related methods. So I would have edit_user and add_user in the users.php controller, but keep them very, very thin and do all the actual work in libraries and models.
Reply


Messages In This Thread
Controller / View Relationship - by simon - 04-28-2017, 03:59 AM
RE: Controller / View Relationship - by simon - 04-28-2017, 01:55 PM
RE: Controller / View Relationship - by InsiteFX - 04-28-2017, 07:56 AM
RE: Controller / View Relationship - by simon - 04-28-2017, 02:08 PM
RE: Controller / View Relationship - by PaulD - 04-29-2017, 03:29 AM
RE: Controller / View Relationship - by InsiteFX - 04-29-2017, 04:28 AM
RE: Controller / View Relationship - by simon - 04-29-2017, 02:19 PM
RE: Controller / View Relationship - by lillian - 05-11-2017, 01:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB