Welcome Guest, Not a member yet? Register   Sign In
hmvc and helpers
#1

[eluser]boytun[/eluser]
Hello
In my application I use the Hmvc structure.
the main structure of my application is like this
Code:
+application
  +controllers
     +page(Class page extends Frontend_Controller)-->load the main.php's view.
  +helpers
     +cms_helpers.php
  +librairies
     +Frontend_Controller.php(Class Frontend_Controller extend MX_Controller)
  +view
     +main.php
I use the meta_titles function to build the title of my page(ex: site-contact)
but when I try to excute the page,everything work fun, only the title of my page is not showed perfectly, and I get this error:

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined property: CI::$data

Filename: helpers/cms_helper.php

Line Number: 6

this is my cms_helpers's fils:
Code:
<?php

function meta_titles ($string)
{
$CI =& get_instance();
$CI->data['meta_title']=$string.' - '.$CI->data['meta_title'];
/*echo "aaaa";
echo($CI->data['meta_title']);*/
}

and this my controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends Frontend_Controller {

public function __construct()
{
  parent::__construct();


}

public function index()
{
  $this->load->model('page_m');
  $this->load->model('widget_m');
  $this->data['page']=$this->page_m->get_by(array('slug'=> (string)$this->uri->segment(1)),TRUE);
  $this->data['widgets']=$this->widget_m->get();
  //echo "<pre>";var_dump($this->data['page']);echo "</pre>";
  $methode="_".$this->data['page']->template;
  $this->$methode();
  $this->data['subview']=$this->data['page']->template;
                // her, I call the meta_titles function, located in the cms_helper.php
  meta_titles($this->data['page']->title);
  $this->load->view('main', $this->data);

}

Thanks for any hints.

Regards
boytun
#2

[eluser]wiredesignz[/eluser]
Your Page controller is descended from the MX_Controller class and any variables it has defined are local to it only, and are not available to the CI super object. Thus the $data variable is local only to the Page controller, hence the error: undefined property CI::$data.

Using get_instance() in your helper will not give you access to any module controller content.

Edit:
If you insist on using a helper to set a class variable of a controller (bad practice IMO) then you should pass the controller instance to the helper also.
#3

[eluser]boytun[/eluser]
Thanks wiredesignz. Smile

I just want to tell you that I'm beginner in CI, but about php5 I have the basics , so that why I ask many questions in the forum in order to learn and explore more the framework.
Smile

About my question,when I applay your notes, the error is disappeared, but I did not get the desired result concerned the title tag.
this is the modified code:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends Frontend_Controller {

public function __construct()
{
  parent::__construct();


}

public function index()
{
  $this->load->model('page_m');
  $this->load->model('widget_m');
  $this->data['page']=$this->page_m->get_by(array('slug'=> (string)$this->uri->segment(1)),TRUE);
  $this->data['widgets']=$this->widget_m->get();
  //echo "<pre>";var_dump($this->data['page']);echo "</pre>";
  $methode="_".$this->data['page']->template;
  $this->$methode();
  $this->data['subview']=$this->data['page']->template;
  $ins= new Page();
  meta_titles($this->data['page']->title,$ins);
  $this->load->view('layout_main', $this->data);

}}

the helper:
Code:
&lt;?php

function meta_titles ($string,$ins)
{
//$CI =& get_instance();
$ins->data['meta_title']=$string.' - '.$ins->data['meta_title'];
/*echo "aaaa";
echo($CI->data['meta_title']);*/
}




Theme © iAndrew 2016 - Forum software by © MyBB