Welcome Guest, Not a member yet? Register   Sign In
Another newbie question - putting functions in a parent controller when vars are in that controller?
#1

[eluser]Bleeding Edge Productions[/eluser]
Hi guys,

First off, thanks to those who have taken the time to respond to my questions so far. Apologies for another newbie problem here...

I have a controller "Homepage" which extends "MY_Controller" (which extends "Controller").

In homepage I have several page-specific variables defined:

Code:
protected $page_id                = 'home'; // Page reference ID
protected $page_title            = 'Home'; // Page title for <title> tag
protected $parent_id            = ''; // Parent page ID if child (null if page is parent)
protected $h1_text                = "Hi! I'm Martin"; // Text used for <h1> with CSS turned off
protected $page_color            = '#0d507a'; // Page colour scheme
protected $bg_position            = '560px 0px'; // Background image CSS position - left, top
protected $h1_margins            = '30px 0px 40px 0px'; // <h1> image CSS position - top, right, bottom, left
protected $meta_description        = 'Description'; // Meta description
protected $meta_keywords        = 'Keywords'; // Meta keywords
protected $meta_robots            = 'INDEX,FOLLOW'; // Meta robots

As part of the index() method in the Homepage class, I have a couple of routines to do things like get the heading 1 image size:

Code:
/**
* Get the heading 1 image CSS size attributes
* using the img_css_size() helper
*/
$h1_fullpath = $this->base . $this->assets . "img/pages/" . $this->page_id . '-h1.png';
$h1_css_size = img_css_size($h1_fullpath);

and generate the page title:

Code:
/**
* Page &lt;title&gt; tag
*
* If there is a parent, include that in the &lt;title&gt;
*/
if (empty($this->parent_id)) {
    $full_page_title = $this->site_name . ' | ' . $this->page_title;
}
else {
    $full_page_title = $this->site_name . ' | ' . $this->parent_title . ' | ' . $this->page_title;
}

etc.

The problem is, I am going to need these routines in ALL my controllers (Homepage, About, Work, Biog, etc etc.). I'd really like to put them in MY_Controller (the parent controller), but most of the variables are page specific (this->page_id etc.), and only set in the controller which EXTENDS MY_Controller.

Is there a way to do this?

Many thanks,

Martin
#2

[eluser]Twisted1919[/eluser]
You can define the vars in MY_Controller and give them values in your extended classes, that way, at least you don't have to declare the variables again and again in your controllers.
But that's everything you can do Smile
#3

[eluser]Bleeding Edge Productions[/eluser]
I've done it! All the routines are in MY_Controller (including setting a group of variables set in Homepage as an array in MY_Controller).

I'm very impressed Big Grin , but now need to pick apart the code and work out exactly WHY it works now. Essentially it seems that changing most of the variable references from $variable to $this->variable, but at the moment that's as far as my understanding goes!

M
#4

[eluser]InsiteFX[/eluser]
Use $this to refer to the current object.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB