« Bob Parsons on Domain Kiting | ^ Main | Vista Beta 2: Software Install Review...coming soon »


Code Igniter ROCKS!

I FINALLY got around to using CI on a real project and it met my expectations and then some and then some more and the a tid bit more than that. I didn’t get to toy with the database interactions, since the project used a CMS we built a while back for a client and it posseses a data access API, but everything else…pretty nice.

I did have problems with the URL’s. My main problem was not fully understanding .htaccess (IIS man here). It is Saturday night so I couldn’t get with our hardcore PHP guys to get help so I went to the net. CI has great documentation which had a .htaccess example which I tweaked a bit to fit our needs. Let’s geek on some code real quick.

Here are the index.php changes I made. Everything else is the same as normal CI stuff.

//custom definitions
define('SITEROOT', realpath(dirname(__FILE__))."/");
define('SITEURL', "...");
define('CLASSPATH', SITEROOT."_classes/");
define("INCLUDEPATH", SITEROOT."_inc/");

include(INCLUDEPATH.'common.inc.php');

define('ADMINCLASSPATH', "../admin/assets/classes/");
include(ADMINCLASSPATH.'data.php');

define('IMAGEPATH', SITEURL."_img/");
define('CSSPATH', SITEURL."_css/");
define('JSPATH', SITEURL."_js/");

define('SITETITLE', "...");

require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;

Nothing special there. Just normal includes, etc, etc, etc. One thing I ended up doing after building all of the controllers was build a general controller class that held two functions duplicated across the other views. To get the class init’ed “globally” I made a change to /CI/application/codeigniter/CodeIgniter.php. Here’s the change:

require(APPPATH.'controllers/GeneralController'.EXT);

This line just includes the class so I can easily extend it in other classes. This was a great improvement which I’ll show why now.

<?

class GeneralController extends Controller{
 	function GeneralController(){
  		parent::Controller();
  	}
 
 	function index()
 		{
  			$params = array("Data"=>new data());
  			$params['headerImage'] = IMAGEPATH.$this->headerImage;
  			$params['headerImageAlt'] = $this->headerImageAlt;
  			$params['headerTitle'] = $this->headerTitle;
  			$params['rightHeaderImage'] = IMAGEPATH.$this->rightHeaderImage;
  			$params['logoClass'] = $this->logoClass;
  			$params['showTestimonialBrief'] = true;
  			$params['SectionLinksID'] = $this->sectionLinksID;
  			
  			$params['RightColumnInsert'] = INCLUDEPATH."elements/randomTestimony.inc.php";
  			$params['content'] = $params['Data']->getContentAndFiles(array("type"=>"parentID", "data"=>$this->contentID));		
  			
  			$params['sectionHeaderTitle'] = $params['content'][0]['data'][1]['value'];
  			
  			$this->load->view('generalContent.php', $params);
  		}
 		
 		function section()
 		{
  			if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
   				$this->index();
   				return;
   			}
  			
  			$params = array("Data"=>new data());
  			$params['headerImage'] = IMAGEPATH.$this->headerImage;
  			$params['headerImageAlt'] = $this->headerImageAlt;
  			$params['headerTitle'] = $this->headerTitle;
  			$params['rightHeaderImage'] = IMAGEPATH.$this->rightHeaderImage;
  			$params['logoClass'] = $this->logoClass;
  			$params['showTestimonialBrief'] = true;
  			$params['SectionLinksID'] = $this->sectionLinksID;
  			$params['RightColumnInsert'] = INCLUDEPATH."elements/randomTestimony.inc.php";
  			
  			$params['content'] = $params['Data']->getContentAndFiles(array("type"=>"parentID", "data"=>$_GET['id']));
  			
  			if(count($params['content']) == 0){
   				$this->index();
   				return;
   			}
  			
  			$params['sectionHeaderTitle'] = $params['content'][0]['data'][1]['value'];
  			
  			$this->load->view('generalContent.php', $params);
  		}
}
?>

Here’s what all of the controllers look like:

class Company extends GeneralController {
 	var $contentID = 2;
 	var $sectionLinksID = 23;
 	var $logoClass = "logo-interior";
 	var $headerImage = "irrigation_systems.jpg";
 	var $headerImageAlt = "Galvanizing";
 	var $headerTitle = "Galvanizing";
 	var $rightHeaderImage = "irrigation_systems_right.jpg";
}

And here is the main view that handles all but 2 sections:

<? include(INCLUDEPATH."head.inc.php"); ?>
<body>
<div id="wrapper">
<div id="bg">
	<? include(INCLUDEPATH."leftColumn.inc.php"); ?>
	
	<div id="content">
		<? include(INCLUDEPATH."elements/sectionHeader.inc.php"); ?>
		<?	
			$len=count($content);
			for($i=0; $i<$len; $i++){
 				$sectionHeaderTitle = $content[$i]['data'][1]['value'];
 				include(INCLUDEPATH."elements/h2Header.inc.php");
 				if(count($content[$i]['files']) > 0){ ?>
  					<div id="photo-block">
  						<div id="photo-stroke">
  							<img src="<?="/assets/uploads/".$content[$i]['files'][0]['filename']?>" alt="" />
  						</div>
  						<p><?=$content[$i]['data'][2]['value'];?></p>
  					</div>
  		<? 		} ?>
 			<p><?=format("paragraph", $content[$i]['data'][3]['value']);?></p>
 		<? } ?>
	</div>
	<? include(INCLUDEPATH."rightColumn.inc.php"); ?>
</div>
<? include(INCLUDEPATH."footer.inc.php"); ?>
<div id="wrapper-btm"></div>
</div>
</body>
</html>

So, if I want a new page all I do is copy and paste one of the controllers (as seen above), change the “instance” variables, and upload. That’s it. Done! I did have 2 other controllers. 1 was a general CI index() method and the other was a customized method for handling different content sections.

So, for a site with a navigation of 19 (dynamic so more can be added later) there are 4 views, 8 controllers (including GeneralController; 3 are unique…rest copies), 1 index page, and 1 .htaccess. How sweet is that?

I think the best part was after I figured out the .htaccess portion it took me 1 hour to complete this entire site, essentially from ground up. I have spent more time prior to converting to CI so it won’t take me 1 hour to build full sites with CI but I would say that may be pretty close to accurate.

CI + the clients CMS = super fast dev time

CI + Custom DB = don’t know..hadn’t done it yet. :-)

I have to admit…I do not like PHP for anything other than very small projects, I never use it for KM or personal sites, and I do not like frameworks in any language. With Code Igniter…PHP is fun and it seriously makes me want to build more things in PHP. KM won’t use PHP for a core language…I guarantee that (CI isn’t that good!) but CI def makes client work much easier. ;-)

For more info on Code Igniter go here. Watch the hello world and blog building video.

Posted by John C. Bland II on June 25, 2006 1:38 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/173

Comments


wvoizra pemqthlv xkli lpseuki itgrcnkxy hjlcnde iezab

Posted by: bpsyo ogvyfpsun | August 23, 2007 2:51 PM


wvoizra pemqthlv xkli lpseuki itgrcnkxy hjlcnde iezab

Posted by: bpsyo ogvyfpsun | August 23, 2007 2:55 PM


wvoizra pemqthlv xkli lpseuki itgrcnkxy hjlcnde iezab

Posted by: bpsyo ogvyfpsun | August 23, 2007 2:56 PM

Post a comment