1

I am using Codeigniter: I have two controllers. They use different models. They have nothing in common. Controller 1 is a login controller it directs me to a secure page, if my login credentials are correct. I simply want Controller 2: to load only the middle part of this page, that's it. Why is it so damn difficult to do this with CI?

+-------------------------------------------------------+
|View "Home_Page"                                       |
+--------------------------------------------------------
| TOP:                                                  |
|Controller 1: function checklogin(){} loads this part  |
|                                                       |
--------------------------------------------------------+
|Middle:                                                |                                  
|Controller 2: function content(){ } loads this part    |
|                                                       |
--------------------------------------------------------+
|Bottom:                                                |                                  
|Controller 1: function checklogin(){} loads this part  |
|                                                       |
--------------------------------------------------------+
tereško
  • 58,060
  • 25
  • 98
  • 150
AnalogJake
  • 21
  • 3
  • Because the CodeIgniter framework does not implement MVC nor MVC-inspired design pattern. What you actually have is a framework for hast prototyping. – tereško Aug 27 '12 at 10:25

3 Answers3

2

To answer your question.

Because CI is not developed to do this kind of things?

Things that are not MVC? Controllers don't call controllers, models don't call controllers, views don't controllers, controllers call everything they wish, only thing that calls controllers are routes.

If you want similar functionality read about HMVC / Modular separation.

In any way, this is bad practice, consider restructuring your code to be MVC and stop trying make CodeIgniter do what it's not supposed to do.

Things to read about MVC and what models are for.

controller 1

public function index() {
    $this->load->view("header");
    $this->load->view("top");
    $this->load->view("middle");
    $this->load->view("bottom");
}

controller 2

public function index() {
    $this->load->view("header");
    $this->load->view("middle");
}
Community
  • 1
  • 1
Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
  • but i'm not trying to call a controller within a controller i'm trying to load one view with two controllers. – AnalogJake Aug 27 '12 at 04:40
  • Why would you want to do that? Make three views and call them as you wish, you even can (though you shouldn't) call views from the view file. If you want to call one view, but show different parts of it then pass some switch to them and do `if/else` stuff in your view. – Sergey Telshevsky Aug 27 '12 at 04:42
  • So wait it's actually better practice to only have one controller for a project with multiple functions? – AnalogJake Aug 27 '12 at 04:42
  • No, it's better to have as much controllers as you want, as well as view files. Make a separate view file for every part of your page if you want to load only parts of it in your controllers – Sergey Telshevsky Aug 27 '12 at 04:45
0

I think you need the master page ,

refer this

Hardik Raval
  • 1,948
  • 16
  • 29
0

There are several auth libraries that work just fine, and they don't operate this way.

Generally the flow in CI is:

Request -> Front Controller 
-> Route
-> Controller
[-> Auth Library 
    [-> Auth Model]
] 
-> Display View

Brackets are there to indicate optional components.

The controller will run the auth code, be it a library or another method in your controller, and then display a view or redirect the user based upon the outcome.

I personally use Tank Auth, but there are many others that will work as authentication libraries.

Brendan
  • 4,565
  • 1
  • 24
  • 39