Thread: More on mvc

    #1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    More on mvc


    Hello;

    Nothing urgent. I used to ask a lot on mvc. I learned quite few things here in devshed. Just running somre more code to get tips so I can improve.

    If anyone has got a few minutes to give me some tips I appreciate it.
    PHP Code:
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class 
    Celebrities extends CI_Controller {

        public function 
    __construct()
            {
                
    parent::__construct();
                
    $data['header_title'] = "Sincity - Control Panel";
                if(
    $this->session->userdata('login')!=true)
                    {
                        
    redirect(base_url().'home''refresh');
                    }
                
                
    $this->load->model('celebrities_model');
                
    $results $this->celebrities_model->list_celebrities();
                
    $data['records'] = $results->result_object;
                
    $this->load->vars($data);
            }
        
        public function 
    index()
            {
                
    $this->view_things();
            }
        public function 
    add()
            {
                
    $this->load->library('validations/add_celebrity','','add_celebrity');
                
    $this->add_celebrity->validate();
                
                
    $this->load->view('header_view');
                 
    $this->load->view('add_celebrity_view');
                 
    $this->load->view('footer_view');
            }
        public function 
    delete($id)
            {
                if(!
    is_numeric($id))
                    {
                        
    redirect(base_url().'celebrities/''refresh');
                    }
                
    $this->load->model('celebrities_model');
                
    $this->celebrities_model->delete_celebrity($id);
                
    redirect(base_url().'celebrities/''refresh');
            }
            
        public function 
    edit($id)
            { 
                
    $this->load->model('celebrities_model');
                
    $results $this->celebrities_model->celebrity_profile($id);
                if(
    $results)
                    {
                        foreach(
    $results as $val=>$row)
                            {
                                
    $data['records'][$val] = $row;
                            }
                        
    $this->load->vars($data);
                        
    $this->load->library('validations/edit_celebrity','','edit_celebrity');
                        
    $this->edit_celebrity->validate($data['records']['photo']);
                        
                        
    $this->load->view('header_view');
                        
    $this->load->view('edit_celebrity_view');
                        
    $this->load->view('footer_view');     
                    }
                else
                    {
                        
    redirect(base_url().'not_found''location'301);
                    }
            }
            
        public function 
    view_things()
             {
                 
    $this->load->view('header_view');
                 
    $this->load->view('celebrities_view');
                 
    $this->load->view('footer_view');
             }
    }
    Thank you
  2. #2
  3. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    What would happen if someone did access the script directly?

    Nothing

    So why bother with that first line? - it only adds time to development and time to execute.

    If you're really worried about direct access (in case you accidentially tell your server to parse .php as plain text) then keep these files outside the web root.

    Then, I'd move this code from your constructor into an Execute method:

    PHP Code:
                $this->load->model('celebrities_model'); 
                
    $results $this->celebrities_model->list_celebrities(); 
                
    $data['records'] = $results->result_object
                
    $this->load->vars($data); 
    So the calling code would do

    PHP Code:
    $c = new Celebrities;

    $c->Execute();

    $c->add(); //$c->delete, ect  etc 
    Why?

    constructors can't really be used to return values, but methods can.

    Lets say you returned false from your execute method if the requested model didn't exist, you could wrap the call to Execute in an if statement,

    PHP Code:

    $c 
    = new Celebrities;

    if(
    $c->Execute()) {

         
    $c->add(); //$c->delete, ect  etc

    I suppose it depends on where and how you want to have your error reporting.....

    it may not always be for error reporting either, you may have some other logic to perform before Executing that code.

    In general, I try to make my constructors do the ultimate bare minimum....because I can't always guarantee that I'll need everything it's doing
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo