Welcome Guest, Not a member yet? Register   Sign In
where to put $title creation function / call
#1

[eluser]jedd[/eluser]
Howdi,

Potentially very dumb question. I want to have my views get an array called $title, that's generated in the controller with:
Code:
$data['title'] = $this->uri->rsegment_array();

However, I don't want to have to do this in every function within every controller class. I think I read somewhere that you can't do any kind of variable assignment within the class but outside a function/method (can you tell I'm not hugely hip with the whole OO thing?). I've tried inserting this call in various places, but get a range of nasty errors on the browser. It works just fine, of course, within my index() or other functions within the controller.

So I'm assuming there's a proper place for this to go, and I'm guessing it should be part of a helper or similar - that I still have to load as part of each controller class's constructor. I'm just starting out with this mini-project, and I'm sure other similar things will need to be gen'd in the same way. So I'm pretty much looking for some style advice here.
#2

[eluser]pistolPete[/eluser]
Extend the controller class:
Code:
class MY_Controller extends Controller {
   var $data = array();
   function MY_Controller()
   {
      $this->data['title'] = $this->uri->rsegment_array();
   }
}
Code:
class Some_controller extends MY_Controller {
   function index()
   {
      // load view
      $this->load->view('some_view', $this->data);
   }

   function some_other_function()
   {
      // load view
      $this->load->view('some_other_view', $this->data);
   }
}
#3

[eluser]jedd[/eluser]
Pete - thanks for this. I've RTM'd the link you gave, and now get what I should be doing.

Reading through the user guide, it suggests that the parent class that we're extending should be prefixed with CI_ .. but doing so caused failures. A problem with the user guide, or am I missing something?

I had to insert a line into the code you provided, in the libraries/MY_Controller.php code, specifically:

Code:
class MY_Controller extends Controller {
   var $data = array();
   function MY_Controller()
   {
      parent::Controller();         // ***** This line 'ere.
      $this->data['title'] = $this->uri->rsegment_array();
   }
}

But yes, now things are working as desired. Thank you.
#4

[eluser]jedd[/eluser]
Actually, a 'best practice' question to follow up on this one.

Does it make sense to create these intercepting MY_ classes for pretty much all the core libraries up front, keep them empty, but then make all my M & C code extend MY_* classes? The expectation being that if I extend any other given core class later, I don't need to go through and modify any of my M/C code?

I'd *expect* that it's relatively inexpensive to have this interception occurring. But on the other hand, if it was that cheap I reason it'd be part of the vanilla CI structure.
#5

[eluser]pistolPete[/eluser]
[quote author="jedd" date="1235454718"]Reading through the user guide, it suggests that the parent class that we're extending should be prefixed with CI_ .. but doing so caused failures. A problem with the user guide, or am I missing something?
[/quote]

The user guide is correct because the controller class' name is really Controller:
Code:
class Controller extends CI_Base {
(...)
/* Location: ./system/libraries/Controller.php */

[quote author="jedd" date="1235454718"]I had to insert a line into the code you provided, in the libraries/MY_Controller.php code...[/quote]
Oh yes, I forgot calling the parent constructor...

[quote author="jedd" date="1235454985"]Does it make sense to create these intercepting MY_ classes for pretty much all the core libraries up front, keep them empty, but then make all my M & C code extend MY_* classes? [/quote]

No, I wouldn't do that.
In my application I have extended 3 core classes:
1) Controller
2) Form Validation
3) Session

Only when I extended the controller library I had to modify my code ( "extends MY_Controller" instead of "extends Controller" in my controllers), the other two extension are pretty much a "drop-in" solution with no modification of my existing code, because you normally only subclass Controllers and Models.




Theme © iAndrew 2016 - Forum software by © MyBB