• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using a single controller?

#1
[eluser]michaelp420[/eluser]
Hi,

I've been trying and trying to get my current CI setup to use a single controller but i cannot and am now asking for your help. Quick overview...i've got a site created that currently has the following controllers:
controllers/
--about.php
--misc.php
--news.php
--living.php
--template_engine.php

The associated views for the four controllers reside in the views directory with the fiels for each in a separate subdir:
views/about/index.inc
views/about/about_us.inc
views/about/contact.inc

...etc. This same sort of structure is used for a subdirectory for each controller. I have a template class that is included in each controller (see below) that actually builds the pages based on the content chosen by the specific page requested. I'm going to abbreviate for simplicity sake but here is the about.php controller:

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

// This is important
require_once('template_engine.php');

class About extends TemplateEngine {
    
    function About()
    {
        parent::TemplateEngine();
        parent::set_section('pagetitle','| '.$this->Nav->section_title(), 'APPEND');
    }
    
    function index()
    {
        // Set page title
        parent::set_section('pagetitle', '', 'APPEND');
        
        // Set as main contents
        parent::set_section('content', $this->load->view('about/index.inc', '', true));
        // build the template
        parent::build_template();
    }
        
       function about_us()
    {
        // Set page title
        parent::set_section('pagetitle', '', 'APPEND');
        
        // Set as main contents
        parent::set_section('content', $this->load->view('about/about_us.inc', '', true));
        // build the template
        parent::build_template();
    }

        function contact()
    {
        // Set page title
        parent::set_section('pagetitle', '', 'APPEND');
        
        // Set as main contents
        parent::set_section('content', $this->load->view('about/contact.inc', '', true));
        // build the template
        parent::build_template();
    }
}


All included main content files are named the same as the function called in the controller. (index.inc , about_us.inc , contact.inc). Also, in the template_engine.php the site navigation is added (top and side) from a database.

Now for my dilemma... i've created a simple cms using ajax that allows me to update the content files (about_us.inc, contact.inc). But i also want to extend the ability to add pages using my cms system. My thoughts are to have a single controller that will allow me to build my pages based upon the link clicked (for example www.mydomain.com/about/about_us). That way i don't need a seperate about.php or misc.php controller file. I've tried hooks, routing and building MY_Controller but to no avail. Maybe i'm missing something very simple here....

There must be a way to access all my views without actually having a separate controller file for each subdirectory ?!? Any help would be greatly appreciated...

-Michael

#2
[eluser]danoph[/eluser]
I had a similar problem and solved it using routing. On my website, I use one controller and cut out the controller name and go straight to the function. For example, my controller is named 'home' and my functions are 'solutions', 'build', and 'contact'.

If you go to my domain however, the URLs are http://bizwidgets.biz/solutions, http://bizwidgets.biz/build/, etc. They are all functions within the home controller, but the URL doesn't reflect that. It appears they are separate controllers. I wrote a blog post about this called CodeIgniter Routes Trick - Removing Controller Names from the URI to Keep URLs Short.

If it helps you out, digg it!

#3
[eluser]michaelp420[/eluser]
I thought of doing something similar but doesn't that mean that all my url links will need to be modified? Taking your example, your clickable links would still need to have "home/" in them (http://bizwidget.biz/home/solutions) but would display in the browser address line without the "home" due to routing (http://bizwidget.biz/solutions). Is this correct? I'm not exactly sure though...

I was hoping to not have to add additional pieces to the links since this will be a cms system and the "users" should not have to be burdened with have to remember to add an additional section in a link for the link to work properly. Is there a way to "grab" the call right after Controller class is processed and redirect to the appropriate view?

Thanks for the reply!

#4
[eluser]danoph[/eluser]
no, your links would be directly to http://bizwidgets.biz/solutions, taking out the controller name. in the regular expression, you could still have different controllers if you like, but if they aren't specified in the regular expression, all of the URLs will use the home controller.

#5
[eluser]michaelp420[/eluser]
Ah ha! Thanks man...got it to work out. I kept running into a snag by just forwarding on any requests to a controller i named "master" (see below). I had to create at least one function within Master or it wouldn't work (placeholder):
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// This is important
require_once('template_engine.php');

class Master extends TemplateEngine {
    
    function Master()
    {
        parent::TemplateEngine();
        parent::set_section('pagetitle','| '.$this->Nav->section_title(), 'APPEND');
        
        if(count($this->uri->segment_array())<=1)
        {
            $pagefile = $this->uri->segment(1)."/index.inc";
        }
        else
        {
            $pagefile = $this->uri->segment(1)."/".$this->uri->segment(2).".inc";
        }
        
        parent::set_section('pagetitle', '| '.$this->Nav->page_title(), 'APPEND');
        
        //Set privacy view as main contents
        parent::set_section('content', $this->load->view($pagefile, '', true));
        
        //Load & pass sidebar to template engine
        $data['bottom_image'] = '/images/wm_develop.gif';
        parent::set_section('sidebar_left_bottom',$this->load->view('templates/layouts/sidebar_left_bottom_view', $data, true));
        
        //build the template
        parent::build_template();
    }
    
    function placeholder()
    {
        // this function is only here as placeholder so that
        // the redirected route has a place to go
    }
}
?&gt;

Then using my new route placed in the config/route.php file:

Code:
$route[':any'] = "master/placeholder";

All requests are rerouted to the master controller and from there i build the appropriate page.

Thanks for you help!!!

-Michael


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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