A weblog about coding, interface design, productivity and the art of making people love the web.

Posts tagged: code


Taking care of the www issue in CodeIgniter

One of the problems that came up with the rising dependency on Google and SEO in general is the use of www. On the one hand there is no reason to use www anymore. 100% of modern browsers use the HTTP protocol by default so you don't need to specify that you need to access the web part of a domain. It makes your site's address larger and harder to remember, more ugly, less likely to fit alongside a logo. Also many suspect that it has an effect on your keyword density, therefore harming your search engine rank just a little bit.

 

On the other hand most of the users still keep the old habit of typing www.site.com in the address bar. I try to redirect to the "clean" version of the domain on all my sites. Even if the user uses the www he's not harmed at all.

 

Still not made up your mind about the three annoying letters that take forever to say ? Read a great post by sitepoint or check out what Matt Mullenweg of Wordpress has done about it.

 

In fact most of the times nobody even notices. One way or another there is a solution to this. Whether you want to get rid of that 90s annoyance or you want to keep your domain a classic www there is an easy way to do this in codeigniter.

 

 

First create a hook in /application/config/hooks.php

$hook['post_controller_constructor'][] = array(
 'class'    => 'Utils_hook',
 'function' => 'redirect_to_base',
 'filename' => 'utils_hook.php',
 'filepath' => 'hooks'
);

 

This tells CodeIgniter to execute the "redirect_to_base" method before the controller starts executing it's own methods. Then you need to create the file /application/hooks/utils_hook.php and place the following code inside

Class Utils_hook {
 function redirect_to_base(){
  $ci = & get_instance();
  $uri = $ci->uri->uri_string();
  // recreates the URL
  $location = substr(base_url(),0,strlen(base_url())-1).$uri;
  
  $active_base_url = 'http://'.$_SERVER['SERVER_NAME'].'/';
  
  if (($active_base_url != base_url()) && ($_SERVER['SERVER_ADDR'] != '127.0.0.1')){
   Header( "HTTP/1.1 301 Moved Permanently" );
   Header( "Location: $location" );
  }
 } 
}

 

This way the visitor is always redirected to the URL version you want even if he entered through an old bookmark such as http://www.sitename.com/action/example/

Friday, July 24, 2009, 21:11. Tagged: codeigniter code

Mainframe CSS framework is born

I just completed an early version of a CSS framework which I think can ease a lot of the pain in modern web design. It's name is Mainframe.

 

The key features are

 

  • CSS reset: all things are reset to what should be the "defaults". This includes fonts, borders, margin, padding and others.
  • Some Internet Explorer specific CSS bugs and annoyances are fixed
  • Mainframe uses "spacers". Clear classes with a bottom margin. Very convenient
  • Some classes you can use to display errors, warnings and information boxes in a stylish way. I call these "AJAX helpers" because that's what I use them for.
  • 3 predefined containers for your content. 12 columns at 960px, 16 columns at 960px and a fluid one at 100% of the page width. PSD files for graphic designers are under development.
  • The 960 grid system.
  • It is free to use and modify under the terms of the Creative Commons Attribution-Share Alike 3.0 Unported License.
Friday, July 24, 2009, 21:03. Tagged: downloads code mainframe css

Templates in CodeIgniter

One of the most annoying stuff for CodeIgniter developers is having to include a header/footer/etc part in each and every view. CodeIgniter out of the box does not include template support (in the sence of a Joomla template or Wordpress theme). I have created a small library to assist you in using "templates" in CodeIgniter.


How to use

1) First copy the file MY_loader.php to your application/libraries folder.

2) Check the example folder to see how a template should look like.

3) Simply do:

$this->load->template('template_name', $data); 
$this->load->view('view_name', $data);


The template loading is done in pretty much the same way as the views loading. However please read the comments inside the MY_loader.php file. It takes one minute and you'll get the whole picture about how you can pass $data arrays containing variables.

Saturday, July 18, 2009, 07:00. Tagged: downloads code codeigniter