CodeIgniter Cart class product name restrictions

codeigniter logo

The CodeIgniter cart class has a non immediately apparent feature regarding special characters in the Product Name. Within the Cart library the line
var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods will strip out any non defined special characters. While you could hack this line, the correct way to do this is to extend the library and create an over-ride.

Create a new file application/libraries/MY_Cart.php

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

class MY_Cart extends CI_Cart {

function __construct() {
parent::__construct();

// Remove limitations in product names
$this->product_name_rules = '\d\D';
}
}

Note this will allow all characters, you may want to limit to specified special characters.

CodeIgniter routes and .htaccess

CodeIgniter has a robust routing module as part of its MVC architecture. A trap for the inexperienced arises when the default controller works (e.g. site.com) but anything else such as mysite.com/dashboard goes to a server based 404, not the CodeIgniter one.

Solution: in your config.php file ensure this line is present
$config['index_page'] = '';
and place the following code in an .htacess file
Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

# activate URL rewriting
RewriteEngine on

# do not rewrite links to the documentation, assets and public files (your folders here)
RewriteCond $1 !^(images|assets|uploads|js)

# do not rewrite for php files in the document root, robots.txt or the maintenance page etc
RewriteCond $1 !^([^\..]+\.php|robots\.txt|maintenance\.html)

# rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>

# If we don't have mod_rewrite installed, all 404's can be sent to index.php, and everything works as normal.

ErrorDocument 404 index.php

</IfModule>