Welcome Guest, Not a member yet? Register   Sign In
Include controller within controller
#1

[eluser]variouspixels[/eluser]
I have a fairly simple website setup at present. My contacts page is more complicated than my other static pages and I have created a seperate form controller for this which works. How do I include my form controller into my contacts page? I know I can paste the code in from my other controller but I want to keep them separate.

Also, /form has a page of its own I do not want this form controller should only churn out a contacts_view.php include.

Noob here, I guess this is a simple question. Hope the question makes sense.

Code:
class Site extends Controller {

    function index() {
    $data = array();
        $data['title'] = 'Home';

        $this->load->view('header_view', $data);

        $data = array();
        $this->load->view('index_view', $data);

        $data = array();
        $this->load->view('footer_view', $data);
    }

    function about( // like above )...
    function events( // like above )...
    function services( // like above )...
    function contacts( // alot of code to paste in from my working controller. messy I want to keep separate controller )

}
#2

[eluser]antonagestam[/eluser]
You should check out modular extensions: http://codeigniter.com/wiki/Category:Library::HMVC/

net.tutsplus.com posted a nice article on the topic a while a go: http://net.tutsplus.com/tutorials/php/hv...plication/
#3

[eluser]variouspixels[/eluser]
Quick scan through that article and I get the impression CI does not support HMVC and its better for larger scale apps.

I have a small site with 4 static(ish) pages and 1 contact form. Have I started off wrongly making this more complicated than it needs to be? Surely I dont need to create a new page for every new controller/new dynamic element. I thought it would be best practice to keep my site and form controller separate.
#4

[eluser]Flemming[/eluser]
the way you've structured your site means that you will need to go to yoursite.com/site/about to get to the about page?

I'm not sure if that's what you had intended, or learned from the user guide / forums but it seems a little odd to me!

I would have thought you'd be better off creating separate controllers for each of your pages:

application/controllers/about
application/controllers/events
etc. etc.

that way, when you come to 'contact' you have a dedicated contact controller that can handle your form.

hope that helps?
#5

[eluser]variouspixels[/eluser]
@flemming No www.mysite.com/about works fine as I have done this to routes.php:
Code:
$route['about'] = "site/about";

So is it the standard to use a new controller for each page even if there is only a couple of variables different on each page?
#6

[eluser]mddd[/eluser]
If pages do different things, it is normal to have different controllers for them.

Including controllers inside controllers will not work, because the controller is meant to be the 'first item in the chain': depending on the uri, a certain controller is started and that controllers manages wat has to happen. Loading two controllers would be like two captains on a ship. But of course, you could put some code -for instance to handle a form- in a model, and call that model from the controller?

For sites that have a lot of similar pages (like you say in your example: index, about, events, they are all the same except for the 'main view' that is loaded between the header and footer) I use this solution (I have adapted it to look and act the same as your example):

Code:
// controller for 'standard text pages'
class Page extends Controller()
{
  function __construct()
  {
    parent::construct();
  }

  function _remap($page='home')
  {
    // set some variables
    $data = array();
    $data['title'] = 'Home';
    $this->load->vars($data);

    // load header and footer, and specific view in between
    $this->load->view('header');
    $this->load->view($page);
    $this->load->view('footer');
  }
}

This way, if you call /page it will show the 'home' view (because that is the default, if nothing is set after /page).
If you call /page/some_view it will show the 'some_view' view.. etc.
So, if they are all the same except for the main view, this is an easy way to deal with them all.

Of course, you can still use routes to make sure that /somepage is routed to /page/somepage.. and the 'somepage' view is shown.
#7

[eluser]danmontgomery[/eluser]
Code:
parent::__construct();

Fixed Wink
#8

[eluser]mddd[/eluser]
Thanks. You are right of course.
#9

[eluser]variouspixels[/eluser]
Okay, I will try rework my code your way. A controller per page looks simpler.
#10

[eluser]mddd[/eluser]
Do it the way you feel comfortable with! That's the main thing.




Theme © iAndrew 2016 - Forum software by © MyBB