Welcome Guest, Not a member yet? Register   Sign In
Post your controller!
#21

[eluser]richzilla[/eluser]
is 106 lines too big? oh well..

my very first bit of proper codeigniter stuff:

Code:
<?php
    class Site extends Controller {
    
        function index() {
            $this->load->view('site_view');
        }
        
        function login() {
            $this->form_validation->set_rules('username','User Name', 'required|xss_clean');
            $this->form_validation->set_rules('password','Password','required|xss_clean');
            
            $this->form_validation->set_error_delimiters('<div class="error">','</div>');
            
            $data = array (
            'userName' => $this->input->post('username'),
            'userPass' => $this->input->post('password')
            );
            
            if ($this->form_validation->run() == FALSE) {
                $this->index();
            }
            else {
                
                $query = $this->user_model->checklog($data);
                
                if ($query->num_rows() > 0) {
                    $row = $query->row();
                    $this->session->set_userdata(array('loggedIn' => TRUE, 'userId' => $row->id, 'userName' => $row->userName, 'userGroup' => $row->uGroup));
                    $this->_secure();
                    
                }
                else {
                    $this->session->set_flashdata('status','The credentials you entered are incorrect, please try again.');
                    redirect('site');
                }
            }
        
        }
        
        function _secure() {
            if ($this->session->userdata('loggedIn') == TRUE) {
                $group = $this->session->userdata('userGroup');    
                switch ($group) {
                    case 'STA':
                        $this->staff();
                        break;
                    case 'ADM':
                        $this->secure();
                        break;
                    case 'CLI':
                        $this->client();
                        break;
                    default:
                        $this->_noview();
                        break;
                }
                
            }
            else {
                $this->_noview();
            }
        }
        
        function secure() {
            if ($this->session->userdata('userGroup') != 'ADM') {
                $this->_noview();
            }
            else {
                $this->load->view('secure_page');
            }
        
        }
        
        function _noview() {
            $this->session->set_flashdata('status','You are not authorised to view this page');
            redirect('site');
        }
        
        function staff() {
            if ($this->session->userdata('userGroup') === 'ADM') {
                $this->load->view('staff_view');
            }
            elseif ($this->session->userdata('userGroup') === 'STA') {
                $this->load->view('staff_view');
            }
            else {
                $this->_noview();
            }
        
        }
        
        function logout() {
            $this->session->sess_destroy();
            $this->load->view('site_view');
        }
        
        function client() {
            if ($this->session->userdata('loggedIn') == TRUE) {
                $this->load->view('client_view');
            }
            else {
                $this->_noview();
            }
        }
    
    }
#22

[eluser]Bui Duc Long[/eluser]
[quote author="J-Slim" date="1251925998"]Here is a function that is part of one of my internet portals that let's me check the position a web site is in Google results.
[/quote]

Hi J-Slim,

Could you share me the dom_helper.php Big Grin is it PHP Simple HTML DOM Parser?
#23

[eluser]stuffradio[/eluser]
Here is a function for a controller that handles all the pages:

Code:
function index()
    {
        $this->load->model('adminmessage');
        if ($this->uri->segment(3) == "userexist"):
        $data['error'] = "Username or password doesn't match or username doesn't exist.";
        elseif($this->uri->segment(3) == "verified"):
        $data['success'] = "You have successfully verified your account! You may now login.";
        $data['error'] = '';
        elseif($this->uri->segment(3) == "notverified"):
        $data['error'] = "Your account has not been verified!";
        $data['success'] = "";        
        elseif ($this->uri->segment(3) == "registered"):
        $data['success'] = "You have successfully registered an account!";
        $data['error'] = "";        
        else:
        $data['error'] = '';
        $data['success'] = '';
        endif;
        $this->load->model('campaign');
        $data['themessage'] = $this->adminmessage->getmessage();
        $data['campaigns'] = $this->campaign->listCampaigns();
        $data['getu'] = $this->users->getPermission();        
        
        $this->load->view('header', $data);
        if ($this->session->userdata('log_in') !== "Guest" && $this->session->userdata('log_in') !== ""):
        $this->load->view('index', $data);
        elseif ($this->session->userdata('log_in') == "Guest" || $this->session->userdata('log_in') == ""):
        $this->load->view('login', $data);
        endif;
        $this->load->view('footer');
    }
#24

[eluser]DogWin[/eluser]
[quote author="Boris Strahija" date="1251656397"]I think it would be better if you would put your SQL statements into a model. There's a reason why CI is MVC Smile[/quote]I agree with this point
#25

[eluser]Nick Husher[/eluser]
@stuffradio:

Suggestion: rather than have a lot of if-else blocks and stringy content data in your controller, you could push the content into a config file and access them using the third url segment as an index. For example:

Code:
// controller.php

// get the segment string once, so we don't need to retrieve it many times
$action = $this->uri->segment(3);

// the segment function returns false if the segment doesn't exist
// set a default value in that case.
if($action === false) { $action = 'default'; }

// load the messages config file into the messages namespace in our controller
$this->config->load('messages', true);
$data['success'] = $this->config->item($action + '_success', 'messages');
$data['error'] = $this->config->item($action + '_error', 'messages');


// messages.php
$config['default_success'] = '';
$config['default_error'] = '';

$config['userexist_success'] = '';
$config['userexist_error'] = 'Username or password doesn&apos;t match or username doesn&apos;t exist.'

$config['verified_success'] = 'You have successfully verified your account! You may now login.';
$config['verified_error'] = '';

It's generally considered good style to minimize extensive if-else blocks and case statements if you can. Long if-else blocks can make it more difficult to read your program, and it also affects extensibility: if ever you wanted to add more messages based on that URL fragment, you'd have to add another block to the end. Your controller could quickly become huge and unwieldy, when the above method will always have the same number of lines in your controller. At the very least, bind the segment string to a variable at the top of your code (i.e. $action = $this->uri->segment(3)), so if you ever need to change which segment it is, you only have to change it in one place.
#26

[eluser]Bui Duc Long[/eluser]
This is my root controller, handle every requests.

Check out the real site here: http://daodanang.com

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Main extends Controller {



    var $html_title;

    var $html_description;

    var $cur_lang;

    

    function Main()

    {

        parent::Controller();

        $this->lang->load('common', $this->config->item('language'));

        

        $this->load->model('hierarchial_model', 'HModel');

        $this->load->model('pages_model');

        $this->load->model('products_model');

        

        $this->cur_lang = 'Vn';

        

        $this->html_title         = $this->config->item('default_title_'.$this->cur_lang);

        $this->html_description = $this->config->item('default_description_'.$this->cur_lang);

    }

    

    function _remap($method)

    {        

        $layout         = 'default';

        $template         = 'main';

        $data             = array();

        $cur_segment     = $this->uri->segment(2);

        

        switch ($method)

        {        

            case 'thong-tin' :

                $data = modules::run('default/pages/index', $cur_segment);



                $this->html_title     = $data['page']['Name'].' | '.$this->html_title;

                $template             = 'page';

                break;

            

            case 'danh-muc' :

                $data = modules::run('default/products/cat', $cur_segment);



                $this->html_title     = $data['cat_name'].' | '.$this->html_title;

                $template             = 'category';

                break;

            

            case 'san-pham' :

                $data = modules::run('default/products/index', $cur_segment);



                $this->html_title     = $data['product']['Name'].' | '.$this->html_title;

                $template             = 'product';

                break;

            

            case 'lien-he' :

                $data     = modules::run('default/pages/contact');

                

                $this->html_title     = $this->lang->line('contact').' | '.$this->html_title;

                $template             = $data['layout'];

                

                break;

            

            case 'tim-kiem' :

                

                $this->html_title     = $this->lang->line('search').' | '.$this->html_title;

                $template             = 'search';

                

                break;

            

            case 'sitemap' :

                modules::run('default/sitemap/index');

                

                break;

                

            default :    

                $data['hot_products']     = $this->products_model->get_hot_products();

                $data['new_products']     = $this->products_model->get_new_products();

                $this->html_title     = $this->lang->line('home').' | '.$this->html_title;                    

                break;

        }

        

        $this->_render($data, $template, $layout);

    }

    

    function _render($data, $template, $layout)

    {

        $_data = array();

        

        if ($layout=='default')

        {

            $_data['header']     = $this->load->view('default/header', $this->header(), true);

            $_data['top']         = $this->load->view('default/top', array('pages'=>$this->pages_model->get_pages()), true);

            $_data['left']         = $this->load->view('default/left', $this->left(), true);

            $_data['main']         = $this->load->view('default/'.$template, $data, true);

        }

        else

        {

            $_data = $data;

        }

        

        $this->load->view($layout, $_data);

    }

    

    function header()

    {

        $data['title']         = $this->html_title;

        $data['description']= $this->html_description;

        $data['keywords']     = $this->config->item('default_keywords_'.$this->cur_lang);

                

        return $data;

    }

    

    function left()

    {

        $this->HModel->table = "categories";

        $this->HModel->PK_field = "id";

        $this->HModel->name_field = "Name";

        $this->HModel->parent_field = "ParentID";

        $this->HModel->order_field = "Priority";

        $this->HModel->condition_field = "Enable";

        

        return array('menu'=>$this->HModel->get_items(), 'banners'=>$this->pages_model->get_banners());

    }

}



/* End of file main.php */
#27

[eluser]Jay Logan[/eluser]
I got the DOM helper from here:

http://www.weblee.co.uk/2009/06/18/simpl...deigniter/




Theme © iAndrew 2016 - Forum software by © MyBB