<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>thinkAPI&#187; Codeigniter</title>
	<atom:link href="http://thinkapi.com/blog/category/programming/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkapi.com/blog</link>
	<description>An interface to tech thoughts</description>
	<lastBuildDate>Fri, 06 Apr 2012 09:23:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Making Codeigniter clean URLs cleaner</title>
		<link>http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/</link>
		<comments>http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/#comments</comments>
		<pubDate>Mon, 10 May 2010 10:32:49 +0000</pubDate>
		<dc:creator>Sukumar</dc:creator>
				<category><![CDATA[Codeigniter]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clean url]]></category>
		<category><![CDATA[codeigniter seo]]></category>
		<category><![CDATA[url rewrite]]></category>

		<guid isPermaLink="false">http://thinkapi.com/blog/?p=192</guid>
		<description><![CDATA[Usually when working with codeigniter you come across the use case where you need to remove the extra &#8220;index&#8221; appearing in the URL for purposes ranging from shorter URLs to SEO. For example, As you can see, the difference in &#8230; <a href="http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/' rel='bookmark' title='Codeigniter: Separating reads and writes for scaling MySQL'>Codeigniter: Separating reads and writes for scaling MySQL</a> <small>Generally websites average a ratio of 9:1 or more for...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Usually when working with codeigniter you come across the use case where you need to remove the extra &#8220;index&#8221; appearing in the URL for purposes ranging from shorter URLs to SEO.</p>
<p>For example,</p>
<pre class="brush: plain; title: ; notranslate">
Current URL style: http://example.com/controller/method/category1/category2/page-name
Target URL style: http://example.com/category1/category2/page-name.html
</pre>
<p><span id="more-192"></span><br />
As you can see, the difference in the target URL is the absence of the controller name and the method name and the presence of the extension &#8220;.html&#8221;. Now, lets see how to achieve this and why the extra extension is required.</p>
<p>Below is my version of .htaccess.</p>
<pre class="brush: plain; title: ; notranslate">
RewriteEngine on

RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</pre>
<p>The line that is extra is 3 which redirects all the requests for &#8220;.html&#8221; files to the controller named &#8220;page&#8221;.</p>
<p>Now, lets look at the page controller which will handle these requests.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page extends Controller
    {

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

    function _remap()
    {
        $segment_count = $this-&gt;uri-&gt;total_segments();
        $segments = $this-&gt;uri-&gt;segment_array();

        if($segment_count == 0)
        {
            $this-&gt;data['title'] = 'UB Group Wines Division';
            $this-&gt;load-&gt;view('home', $this-&gt;data);
        }
        elseif($segment_count == 2) // Single page
        {
            $page_url = $segments[2];
            // Handle the page request
        }
        elseif($segment_count == 3) // Page with category
        {
            $category = $segments[2];
            $page_url = $segments[3];
            // Handle the category + page request
        }
    }
}
</pre>
<p>In this way you can have pages with smaller and cleaner URLs.</p>
<p><b>Tips:</b><br />
1. You can use any extension you like. e.g; in case you have static HTML files to server, you can use the extension &#8220;.html&#8221; for static and &#8220;.htm&#8221; for non-static files.<br />
2. You can have any number of URL stubs before the &#8220;.html&#8221; extension and you can access all these stubs in the controller.</p>
<p><b>Cons:</b><br />
1. The URL needs an extra extension.<br />
2. Only one controller per extension.</p>
<p>Do let me know what you think about this or if there is any way to improve on this.</p>
<p>Related posts:<ol>
<li><a href='http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/' rel='bookmark' title='Codeigniter: Separating reads and writes for scaling MySQL'>Codeigniter: Separating reads and writes for scaling MySQL</a> <small>Generally websites average a ratio of 9:1 or more for...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Codeigniter: Separating reads and writes for scaling MySQL</title>
		<link>http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/</link>
		<comments>http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 09:41:12 +0000</pubDate>
		<dc:creator>Sukumar</dc:creator>
				<category><![CDATA[Codeigniter]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[database replication]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysql replication]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://thinkapi.com/blog/?p=173</guid>
		<description><![CDATA[Generally websites average a ratio of 9:1 or more for reads:writes for their applications which makes MySQL replication as one of the ways to scale you web application. The simplest configuration is to separate reads and writes with all the &#8230; <a href="http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/' rel='bookmark' title='Making Codeigniter clean URLs cleaner'>Making Codeigniter clean URLs cleaner</a> <small>Usually when working with codeigniter you come across the use...</small></li>
<li><a href='http://thinkapi.com/blog/how-to-skip-mysql-replication-counter/' rel='bookmark' title='How to skip MySQL replication counter'>How to skip MySQL replication counter</a> <small>There are such times when MySQL replication stops when you...</small></li>
<li><a href='http://thinkapi.com/blog/upgrading-to-mysql-51-ga-better-wait/' rel='bookmark' title='Upgrading to MySQL 5.1 GA? Better Wait'>Upgrading to MySQL 5.1 GA? Better Wait</a> <small>MySQL 5.1 General Availability has been released to the public....</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Generally websites average a ratio of 9:1 or more for reads:writes for their applications which makes MySQL replication as one of the ways to scale you web application. The simplest configuration is to separate reads and writes with all the reads coming from the slave servers.<br />
<span id="more-173"></span><br />
<div id="attachment_174" class="wp-caption aligncenter" style="width: 440px"><a href="http://www.teonator.net/2008/10/23/mysql-replication/"><img class="size-full wp-image-174" title="MySQL Database replication" src="http://thinkapi.com/blog/wp-content/uploads/2009/08/db_replication.jpg" alt="MySQL Database replication" width="430" height="450" /></a><p class="wp-caption-text">MySQL Database replication</p></div></p>
<p>We can implement this in <a href="http://codeigniter.com/" target="_blank">codeigniter</a> using database groups.<br />
Below is a sample execution.</p>
<p>Create two active groups &#8211; one for read and one for write.</p>
<p>File: system/application/config/database.php</p>
<pre class="brush: php; title: ; notranslate">
$active_group = &quot;write&quot;;
$active_record = TRUE;

$db['read']['hostname'] = &quot;localhost&quot;;
$db['read']['username'] = &quot;root&quot;;
$db['read']['password'] = &quot;&quot;;
$db['read']['database'] = &quot;read_database_name&quot;;
$db['read']['dbdriver'] = &quot;mysql&quot;;
$db['read']['dbprefix'] = &quot;&quot;;
$db['read']['pconnect'] = TRUE;
$db['read']['db_debug'] = FALSE;
$db['read']['cache_on'] = FALSE;
$db['read']['cachedir'] = &quot;&quot;;
$db['read']['char_set'] = &quot;utf8&quot;;
$db['read']['dbcollat'] = &quot;utf8_general_ci&quot;;

$db['write']['hostname'] = &quot;localhost&quot;;
$db['write']['username'] = &quot;root&quot;;
$db['write']['password'] = &quot;&quot;;
$db['write']['database'] = &quot;write_database_name&quot;;
$db['write']['dbdriver'] = &quot;mysql&quot;;
$db['write']['dbprefix'] = &quot;&quot;;
$db['write']['pconnect'] = TRUE;
$db['write']['db_debug'] = FALSE;
$db['write']['cache_on'] = FALSE;
$db['write']['cachedir'] = &quot;&quot;;
$db['write']['char_set'] = &quot;utf8&quot;;
$db['write']['dbcollat'] = &quot;utf8_general_ci&quot;;
</pre>
<p>Create separate database connections to access read and write databases separately where needed (constructor is generally a good place for this).</p>
<pre class="brush: php; title: ; notranslate">
$read_db = $this-&gt;load-&gt;database('read', TRUE);
$write_db = $this-&gt;load-&gt;database('write', TRUE);
</pre>
<p>You can now go about running queries in the usual way.</p>
<pre class="brush: php; title: ; notranslate">
/* For reads */
$query = $read_db-&gt;get('table_name');

foreach ($query-&gt;result() as $row)
{
    echo $row-&gt;title;
}

/* For writes */
$data = array(
               'title' =&gt; $title,
               'name' =&gt; $name,
               'date' =&gt; $date
            );

$write_db-&gt;insert('mytable', $data);
</pre>
<p>In case you want to try this on an application you are already running, you can leave the &#8220;default&#8221; connection group intact and create only the &#8220;read&#8221; connection group. Use it where you think you are running read heavy queries.</p>
<p>Related posts:<ol>
<li><a href='http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/' rel='bookmark' title='Making Codeigniter clean URLs cleaner'>Making Codeigniter clean URLs cleaner</a> <small>Usually when working with codeigniter you come across the use...</small></li>
<li><a href='http://thinkapi.com/blog/how-to-skip-mysql-replication-counter/' rel='bookmark' title='How to skip MySQL replication counter'>How to skip MySQL replication counter</a> <small>There are such times when MySQL replication stops when you...</small></li>
<li><a href='http://thinkapi.com/blog/upgrading-to-mysql-51-ga-better-wait/' rel='bookmark' title='Upgrading to MySQL 5.1 GA? Better Wait'>Upgrading to MySQL 5.1 GA? Better Wait</a> <small>MySQL 5.1 General Availability has been released to the public....</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

