• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Same Class, different controllers?

#1
[eluser]kb1ibh[/eluser]
i'm working under a modified version of codeigniter which allows me to use multiple different controllers for different purposes... but for the purposes of this question, assume i'm only using CI_Controller and MY_Controller...

is it possible for "/ClassA/Function1" to use MY_Controller, and for "/ClassA/Function2" to use CI_Controller?

#2
[eluser]GrahamDj28[/eluser]
When reading this al I can think of is that this is not the way MVC must be used.

Controllers call functions in libraries, helpers or models.
Helpers, models or libraries don't call functions in controllers.

#3
[eluser]Aken[/eluser]
Can you do that from the same PHP file? No.

Can you set up routes to direct your URI to the appropriate file? Yes, quite easily.

#4
[eluser]kb1ibh[/eluser]
[quote author="GrahamDj28" date="1336082991"]When reading this al I can think of is that this is not the way MVC must be used.

Controllers call functions in libraries, helpers or models.
Helpers, models or libraries don't call functions in controllers.[/quote]

Everything i do is still MVC, its a method that i use to make view management much more modular and simple... i'll create a very very simple template in "/views/controller_templates"

nav363.php:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
?>
<!DOCTYPE html>
&lt;html lang="en"&gt;

&lt;head&gt;
&lt;?=$head?&gt;
&lt;/head&gt;

&lt;body&gt;
<div class="navbar navbar-fixed-top">
  &lt;?=$navbar ?&gt;
</div>
<div class="container">
  <div class="row">
   <div class="span3">
    &lt;?=$leftnav ?&gt;
   </div>
   <div class="span6">
    &lt;?=$content ?&gt;
   </div>
   <div class="span3">
    &lt;?=$rightnav ?&gt;
   </div>
  </div>
  <hr>
  <footer>
   &lt;?=$footer ?&gt;
  </footer>
</div>
&lt;/body&gt;

Then create a controller for that specific layout that assigns default views to those page sections

NAV363_Controller
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class  NAV363_Controller  extends  MY_Controller  {

    function __construct()  
{
        parent::__construct();
  
$this->data['head'] = &$this->head;
$this->data['navbar'] = &$this->navbar;
        $this->data['leftnav'] = &$this->leftnav;
        $this->data['content'] = &$this->content;
        $this->data['rightnav'] = &$this->rightnav;
        $this->data['footer'] = &$this->footer;
  
        $this->head = $this->load->view('Defaults/head', '', true);
        $this->navbar = $this->load->view('Defaults/navbar', $this->auth, true);
        $this->leftnav = $this->load->view('Defaults/leftnav', '', true);
        $this->content = $this->load->view('Defaults/content', '', true);
$this->rightnav = $this->load->view('Defaults/rightnav', '', true);
        $this->footer = $this->load->view('Defaults/footer', '', true);
}

function output()
{
  $this->load->view('Controller_Templates/nav363', $this->data);
}
}

all of the default views are empty except for Navbar and Footer, because those never change from page to page, and the Head view contains the global CSS and script files...

When i need to add things to a page, i only have to fill in the leftnav, rightnav, and content areas, which is as easy as

Code:
$this->rightnav = $this->load->view('ads/wide_skyscraper', NULL, TRUE);

adding multiple views, or adding to (as opposed to overwriting) the default, you just concatinate

like this:
Code:
$this->head .= $this->load->view('scripts/script_a', '',TRUE);
$this->head .= $this->load->view('scripts/script_b', '',TRUE);
$this->head .= $this->load->view('scripts/script_c', '',TRUE);


then output using $this->output(); and you're done.

makes it easy to divide your site into tiny sections while still making it easy to inject them later on, and you arent wasting space calling the same default views in every controller function, the only problem is that codeigniter by default only allows one custom controller (My_Controller), so you have to make a slight modification by adding the following to the end of config.php:

Code:
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
}

#5
[eluser]GrahamDj28[/eluser]
I must be missing something, don't see how your example explains your original question.

Your question was can
ClassA / function1 use MY_Controller

And can
ClassA / function2 use CI_Controller

When you say USE, I assume you have a class with 2 functions and that
Function1 calls a function IN MY_Controller and
Function2 calls a function IN CI_Controller

But if you mean that classA is a Controller and it has 2 functions that
Function1 calls a function IN MY_Controller and
Function2 calls a function IN CI_Controller

Then yes, you can because, MY_Controller extends CI_Controller and Class A extends MY_Controller. So all functions in MY_Controller and CI_controller are available in Class A. If they are public Smile

#6
[eluser]CroNiX[/eluser]
Yes, using the scope resolution operator. parent::method() (to access CI_Controller) and self:Sadmethod) (to access MY_Controller), assuming ClassA extends MY_Controller.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.