![]() |
New with Code Igniter - Application structure - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: New with Code Igniter - Application structure (/showthread.php?tid=16137) |
New with Code Igniter - Application structure - El Forum - 03-03-2009 [eluser]bisol[/eluser] I've beginning my website. Tell me if I'm wrong. Edit I think I'm wrong because on each page, I load the column2.php and column3.php (view including view) but I need to write the SQL into each part of the main controller. example. function : Home - Load top10 web site - load home function : Contact - Load top10 web site - load contact .... End edit Controller Code: class Accueil extends Controller { View (accueil) Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); View (header) Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?> New with Code Igniter - Application structure - El Forum - 03-03-2009 [eluser]Fero[/eluser] $data['test'] = $this->db->version(); what the heck is this??? New with Code Igniter - Application structure - El Forum - 03-03-2009 [eluser]bisol[/eluser] This is only an example to describe my problem. This code is a code needed in each part of the main page (top10, home, ....). Now, I will create a model file and called it like this : $data[‘top_10’] = $this->MainModel->get_top_10(); New with Code Igniter - Application structure - El Forum - 03-05-2009 [eluser]ayukawaa[/eluser] This is well documented in the forums and it has been answered many times I think. For me, instead of creating many controllers that extend the 'Controller' (repeating code in all the controllers), I create one MY_Controller library which extends Controller and I put in there all the common code. Then, all the controllers must extend My_Controller, NOT Controller For example, create your controllers like Code: class ControllerExample extends MY_Controller { Code: class MY_Controller extends Controller{ And that's all. ^_^ New with Code Igniter - Application structure - El Forum - 04-03-2009 [eluser]taro uma[/eluser] does anyone have an example of how you pass variables from MY_Controller to a controller that extends it? In this last example, I kinda see how this is loading a view that has the variables in it, but what if you just want to pass the variables without having a view? I just want to pass the $data array along and access it in the controller that extends MY_Controller, and i don't exactly understand how this is done. New with Code Igniter - Application structure - El Forum - 04-03-2009 [eluser]jedd[/eluser] [quote author="taro uma" date="1238813037"]does anyone have an example of how you pass variables from MY_Controller to a controller that extends it?[/quote] How about [url="http://ellislab.com/forums/viewreply/553141/"]this one[/url]? New with Code Igniter - Application structure - El Forum - 04-03-2009 [eluser]taro uma[/eluser] I've seen that and it helped some, I was able to create the MY_Controller based on that info. But I still can't see how to get the variables from MY_Controller to the controller that is extending it. Here is what I have so far, I just can't get the $data from MY_Controller to pass along. Code: class MY_Controller extends Controller { but it's not giving me those variables in a control that extends it: Code: class FrontPage extends MY_Controller { This passes $data along to my view, which just echos out the $data variables (right now). I am expecting $sitewide to be an array that exists with my site wide stuff in it. But it doesn't, so I know I'm doing it wrong. the load->view is passing $data along, because in the view I have the $frontpagedata, which is an array of that builds the content part of the page. But trying to use $sitewide , like echo $sitewide['mainNav') gives me as error "Message: Undefined variable: sitewide" So I see where I'm doing it differently as far as not loading a view in MY_Controller, from the example you show. But I can't see why the variables aren't in the $data array. In my view, I have this: Code: <?php echo $sitewide['mainNav']; ?> and it returns this error message: A PHP Error was encountered Severity: Notice Message: Undefined variable: sitewide Filename: views/frontpage.php Line Number: 34 New with Code Igniter - Application structure - El Forum - 04-03-2009 [eluser]jedd[/eluser] In your (normal) controller, change this line: Code: $this->load->view('frontpage',$data); to this: Code: $this->load->view('frontpage',$this->data); ... and give it another try. New with Code Igniter - Application structure - El Forum - 04-03-2009 [eluser]taro uma[/eluser] Ah. That works. So I can see now that $this->data and $data are not the same. But I don't understand why in MY_Controller, I can't just say Code: $data['sitewide']['mainNav'] = $this->getMainNav(); instead of Code: $this->data['sitewide']['mainNav'] = $this->getMainNav(); what is the $this-> part doing to make the variables available to the normal controller? New with Code Igniter - Application structure - El Forum - 04-04-2009 [eluser]TheFuzzy0ne[/eluser] It's a question of scope. $data is available within the confines of the function it's declared in, if you declare $data as a class variable, it's available to the whole class, and accessed via $this->data. $this->something means you want to access a variable that's in the global scope of the current object. $this won't work outside of an object. Code: class Example { I hope this helps without causing more confusion. |