Welcome Guest, Not a member yet? Register   Sign In
__construct() and uri params?
#1

[eluser]mhulse[/eluser]
Hi,

Is it possible to access uri params in the controller constructor?

Thanks,
M
#2

[eluser]mhulse[/eluser]
Hmm, I probably should go back to the drawing board on this one. Big Grin

I was thinking I could instantiate some user-defined variables upon controller initialization.

I am trying to avoid generating a multi-dimensional array twice, from one controller, for two different views (one view that generates CSS, and another that generates HTML -- both based on what the user passes to a single controller.)

site.com/folder/controller/html_output/param1/param2/

site.com/folder/controller/css_output/param1/param2/

I would use the above uris for ajax calls.

I suppose I could put the css and html into the same view, and use jquery to grab one or the other chunk of code.

Anyway, sorry to bug ya'll.

M
#3

[eluser]Kamape[/eluser]
[quote author="mhulse" date="1261816576"]Hmm, I probably should go back to the drawing board on this one. Big Grin

I was thinking I could instantiate some user-defined variables upon controller initialization.

I am trying to avoid generating a multi-dimensional array twice, from one controller, for two different views (one view that generates CSS, and another that generates HTML -- both based on what the user passes to a single controller.)

site.com/folder/controller/html_output/param1/param2/

site.com/folder/controller/css_output/param1/param2/

I would use the above uris for ajax calls.

I suppose I could put the css and html into the same view, and use jquery to grab one or the other chunk of code.

Anyway, sorry to bug ya'll.

M[/quote]

There's also always the HOOK for "post_controller_constructor" where you can load up the view with the array by using $CI->load->vars(). I'm using the post_controller_constructor to specifiy some controller dependent lang_files to load. This so that I can use the lang() helper inside controller methods instead of loading the lang-file in every method.
#4

[eluser]mhulse[/eluser]
[quote author="Kamape" date="1261840599"]There's also always the HOOK for "post_controller_constructor" where you can load up the view with the array by using $CI->load->vars(). I'm using the post_controller_constructor to specifiy some controller dependent lang_files to load. This so that I can use the lang() helper inside controller methods instead of loading the lang-file in every method.[/quote]

Oooh, interesting! Thanks for the tip! I will investigate the bits of code you mention... Sounds like what I need. Smile

I may be back with more questions.

Thanks Kamape!

Have a great day/night.

Cheers,
Micky
#5

[eluser]mhulse[/eluser]
[quote author="mhulse" date="1261904093"]Oooh, interesting! Thanks for the tip! I will investigate the bits of code you mention... Sounds like what I need. Smile[/quote]

Unfortunately, my brain is more patient than my actual patience (i.e. ability) to learn new stuff... I opted to use a switch statement as a temporary solution:

Code:
<?php

class System extends Controller {
    
    // Properties here...
    
    private $data;
    
    function __construct()
    {
        
        parent::Controller();
            
    }
    
    public function index()
    {
        
        // ...
        
    }
    
    // ...
    public function construct($kind = 'html', .... )
    {
        
        // ... init stuff here ...
        
        $this->data = array(
            'blueprints' => $this->blueprints();
            // More here ...
        );
        
        # Determine view:
        switch ($kind) {
            
            case 'html':
                $this->load->view('folder/html', $this->data);
                break;
            case 'css':
                $this->output->set_header('Content-type: text/css');
                $this->load->view('folder/css', $this->data);
                break;
            default:
                echo 'First parameter is wrong.'; // Temp for now.
        }
        
    }
    
    // ...
    private function blueprints()
    {
        
        // This method generates and returns a
        // decently-sized multi-dimensional array.
        
    }
    
}

The "blueprints" method outputs the same multi-dimensional array for both the "css" and "html" view. The view files do different things with the $data array.

As soon as I learn more about PHP5 OOP and CI, I will probably re-factor my code.

But, for now, I am open to suggestions. Unfortunately, I was not able to figure out a way to accomplish the same thing using post_controller_constructor.

Thanks for reading!

Cheers,
Micky




Theme © iAndrew 2016 - Forum software by © MyBB