<?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>C.M. Jackson .Net &#187; CodeIgniter</title>
	<atom:link href="http://www.cmjackson.net/tag/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cmjackson.net</link>
	<description>Web Design, Programming, Tutorials</description>
	<lastBuildDate>Tue, 13 Dec 2011 17:38:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>How to setup CodeIgniter</title>
		<link>http://www.cmjackson.net/2009/06/30/how-to-setup-codeigniter/</link>
		<comments>http://www.cmjackson.net/2009/06/30/how-to-setup-codeigniter/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 01:04:40 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=243</guid>
		<description><![CDATA[Introduction This article explains the process I go through to setup the CodeIgniter framework and how to configure it so that I can start developing an application. What is CodeIgniter? If you don&#8217;t already know, CodeIgniter is an MVC framework built on PHP.  There are many features and built in functions that make building a web [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This article explains the process I go through to setup the CodeIgniter framework and how to configure it so that I can start developing an application.</p>
<h2>What is CodeIgniter?</h2>
<p>If you don&#8217;t already know, CodeIgniter is an MVC framework built on PHP.  There are many features and built in functions that make building a web application fairly easy.  You can download the current version, 1.7.1, from <a href="http://codeigniter.com/">CodeIgniter.com</a>.  Also, check out the very well documented <a href="http://codeigniter.com/user_guide/">User Guide</a>.</p>
<p>If you are interested in using the Zend library of tools with CodeIgniter, please check out <a href="http://www.cmjackson.net/2009/02/17/how-to-use-zend_search_lucene-with-the-php-framework-codeigniter/">How to use Zend_Search_Lucene with CodeIgniter</a>.</p>
<h2>Step-By-Step</h2>
<p>For this tutorial, I&#8217;m using a <a href="http://www.wampserver.com/">WAMP Server</a> running on my local Windows computer.  You could just as easily perform these same actions on a web server running PHP, although the paths may be different depending on if you are using Windows or a Linux based server.</p>
<h3>Step 1</h3>
<p>Download the latest version of <a href="http://codeigniter.com/download.php">CodeIgniter</a> (1.7.1).</p>
<p>Before extracting the files to your server, let&#8217;s talk about where to put the files.  For security purposes, it is recommended to place the CodeIgniter files outside the path of your web server so those files cannot be accessed by typing in a URL.  So, if I access my web server by typing in <a href="http://localhost">http://localhost</a> and the web server loads the website at C:\wamp\www\, then I want to place the System folder of CodeIgniter inside the C:\wamp\ folder.  Go ahead and extract the System folder from the download package to the C:\wamp\ folder.  You can place the System folder in the C:\wamp\www\ folder if you desire.  Just make sure to adjust the path name later on.</p>
<h3>Step 2</h3>
<p>Next, I have decided that I may want to use CodeIgniter for several applications that I develop.  To save on hosting space, I can setup all CodeIgniter applications to use the same core framework.  First, we need to move some files around from their default locations.</p>
<p>Open the C:\wamp\system\application\ folder.  You should see several folders listed here.  The default CodeIgniter application is setup for a single application.  We will be changing it to run multiple applications.  Create a new folder called baseApplication_1234 or something unique.  Make a copy of the index.html file and paste it inside baseApplication_1234.  Next, move all of the folders located at C:\wamp\system\application\ into C:\wamp\system\application\baseApplication_1234\.</p>
<p>We will configure this base application with all of our default settings that we want to use for all CodeIgniter applications.  Then, when you want to make a new application, you can simply copy the baseApplication_1234 and rename it.</p>
<h3>Step 3</h3>
<p>Next, we will configure the CodeIgniter application files.</p>
<p>Browse to C:\wamp\system\application\baseApplication_1234\config\ and open the following:</p>
<h4>autoload.php</h4>
<p>Modify line 42 to show:</p>
<pre class="brush: php;">$autoload['libraries'] = array('database', 'session');</pre>
<h4> config.php</h4>
<p>Modify line 14 to show (change localhost to your domain name):</p>
<pre class="brush: php;">$config['base_url'] = &quot;http://localhost/&quot;;</pre>
<p>Modify line 26 to show:</p>
<pre class="brush: php;">$config['index_page'] = &quot;index.php?&quot;;</pre>
<p>This line will be very important for the way our URLs are displayed later on.</p>
<p>Modify line 44 to show:</p>
<pre class="brush: php;">$config['uri_protocol'] = &quot;QUERY_STRING&quot;;</pre>
<p>Modify line 57 to show (this is optional if you want a .html to be shown on the end of your URLs):</p>
<pre class="brush: php;">$config['url_suffix'] = &quot;.html&quot;;</pre>
<p>Modify line 220 to show (add your own unique value within the quotes):</p>
<pre class="brush: php;">$config['encryption_key'] = &quot;12345&quot;;</pre>
<p>Modify lines 234 &#8211; 241 to show:</p>
<pre class="brush: php;">$config['sess_cookie_name']  = 'ci_session';
$config['sess_expiration']  = 7200;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name']  = 'ci_sessions';
$config['sess_match_ip']  = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;</pre>
<p>This will require that we have a database and a table within called ci_sessions.  This table will need to have certain fields that CodeIgniter will be attempting to write session data to for each users who visits your site.  I&#8217;ll explain more later on.</p>
<h4>database.php</h4>
<p>Modify lines 41 &#8211; 45 to show:</p>
<pre class="brush: php;">$db['default']['username'] = &quot;dbuser&quot;;
$db['default']['password'] = &quot;mypassword&quot;;
$db['default']['database'] = &quot;mydbname&quot;;
$db['default']['dbdriver'] = &quot;mysql&quot;;
$db['default']['dbprefix'] = &quot;dev_&quot;;</pre>
<p>You will need to enter your correct username, password, database name, and prefix if you wish to use one.  If you use a prefix, you will need to have a table called dev_ci_session instead of ci_session.</p>
<h4>routes.php</h4>
<p>For the base application, the routes.php file is probably OK.  There are two lines that need to be modified when you build an application. </p>
<pre class="brush: php;">$route['default_controller'] = &quot;welcome&quot;;
$route['scaffolding_trigger'] = &quot;12345&quot;;</pre>
<p>Again, use some unique value for the scaffolding_trigger.  If you use a value that is hackable or nothing at all, your application will have a possible security hole.  The default controller name can be changed here as well.</p>
<h3>Step 4</h3>
<p>There are two helper files that I add to my projects that help translate the URLs the way I like them (<a href="http://localhost/controller/action/id.html">http://localhost/controller/action/id.html</a>).</p>
<h4>MY_form_helper.php</h4>
<pre class="brush: php;">

&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('form_open'))
{
    function form_open($action = '', $attributes = '', $hidden = array())
    {
        $CI =&amp; get_instance();

        if ($attributes == '')
        {
            $attributes = 'method=&quot;post&quot;';
        }

        //Modify -&gt;site_url to -&gt;item('base_url').$action
        $action = ( strpos($action, '://') === FALSE) ? $CI-&gt;config-&gt;item('base_url').$action : $action;

        $form = '&lt;form action=&quot;'.$action.'&quot;';
 
        $form .= _attributes_to_string($attributes, TRUE);
 
        $form .= '&gt;';

        if (is_array($hidden) AND count($hidden) &gt; 0)
        {
            $form .= form_hidden($hidden);
        }

        return $form;
    }
}
?&gt;</pre>
<h4>MY_url_helper.php</h4>
<pre class="brush: php;">

&lt;?php
function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
    $CI =&amp; get_instance();

    switch($method)
    {
        case 'refresh' : header(&quot;Refresh:0;url=&quot;.site_url($uri));
        break;
        default   : header(&quot;Location: &quot;.$CI-&gt;config-&gt;item('base_url').$uri, TRUE, $http_response_code);
        break;
    }
    exit;
}
?&gt;</pre>
<p>Create these files and save them to C:\wamp\system\application\baseApplication_1234\helpers\.  These functions will override the original CodeIgniter functions and fix some URL rewriting issues.</p>
<h3>Step 5</h3>
<p>Enabling URL rewriting may depend if your server supports it.  Apache has a rewrite_module that must be enabled before this will work.  Most hosting providers should already have enabled URL rewriting.</p>
<p>Create a new text file called .htaccess and paste the following code into it:</p>
<pre>RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]</pre>
<p>This will redirect all requests on your domain back to the index.php file, unless the actual path does happen to exist, then the server will serve up whatever file/directory is at that location.</p>
<p>Notice the ? behind index.php.  The web server is rewriting the pretty URL into a QUERY_STRING that CodeIgniter is expecting and passes in the controller and action as variables such as index.php?var1=this&amp;var2=that. </p>
<p>The .htaccess file should be stored in the C:\wamp\www\ folder or the root of your web site.</p>
<h3>Step 6</h3>
<p>Next, the database will need to be created and the ci_sessions table created to store the user sessions to.</p>
<p>After you create the MySQL database, run the following SQL and it will create the table for you:</p>
<pre class="brush: sql;">CREATE TABLE `dev_ci_sessions` (
  `session_id` varchar(32) NOT NULL,
  `ip_address` varchar(15) NOT NULL,
  `user_agent` text NOT NULL,
  `last_activity` datetime NOT NULL,
  PRIMARY KEY  (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;</pre>
<p>Make sure to remove the &#8220;dev_&#8221; if you did not setup a dbprefix.</p>
<h3>Step 7</h3>
<p>Next, for testing purposes, we&#8217;ll setup the baseApplication_1234 web application and attempt to run it.</p>
<p>Find the index.php file that came with the CodeIgniter 1.7.1 download.  It should be located in the same folder as the system folder and user_guide folder.  Copy this file into your web site root at C:\wamp\www\.</p>
<p>Modify line 26 as shown:</p>
<pre class="brush: php;">$system_folder = &quot;c:/wamp/system&quot;;</pre>
<p>Modify line 43 as shown:</p>
<pre class="brush: php;">$application_folder = &quot;application/baseApplication_1234&quot;;</pre>
<p>This is the line that specifies which application to load.</p>
<p>This index.php file is the main entry point for the application.  It will use these modifications to find the C:\wamp\system\ folder and run the correct web application.  The only other files that need to be in the C:\wamp\www\ folder are files that HTML needs to be able to access via URLs such as images and JavaScript.</p>
<h3>Conclusion</h3>
<p>That should be everything.  You can now try to open your web application by going to the URL, <a href="http://localhost">http://localhost</a>.  The default controller is Welcome and the default action is Index, so you can also test <a href="http://localhost/welcome/index.html">http://localhost/welcome/index.html</a> to verify URL rewriting works.  Remember the &#8220;.html&#8221; can be used to hide that PHP is running your web site.  In reality, index is an action of the welcome controller and doesn&#8217;t need the &#8220;.html&#8221; to be there at all.</p>
<p>If you run into problems, you can add this line to an action or the controller&#8217;s constructor for further information:</p>
<pre class="brush: php;">$this-&gt;output-&gt;enable_profiler(TRUE);</pre>
<p>Be sure to shut this down when not needed, or it could give your users information you may not wish them to have.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2009/06/30/how-to-setup-codeigniter/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>How to use Zend_Search_Lucene with the PHP framework CodeIgniter</title>
		<link>http://www.cmjackson.net/2009/02/17/how-to-use-zend_search_lucene-with-the-php-framework-codeigniter/</link>
		<comments>http://www.cmjackson.net/2009/02/17/how-to-use-zend_search_lucene-with-the-php-framework-codeigniter/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 04:06:21 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Lucene]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[search engine]]></category>
		<category><![CDATA[Zend framework]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=99</guid>
		<description><![CDATA[If you&#8217;ve heard the buzz about Apache&#8217;s open source search engine, Lucene, then you probably already know what a great search engine tool it is.  The search engine is fast, has ports to various languages, and was written to be able to share the search index between the different Lucene ports. The PHP version of [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve heard the buzz about Apache&#8217;s open source search engine, Lucene, then you probably already know what a great search engine tool it is.  The search engine is fast, has ports to various languages, and was written to be able to share the search index between the different Lucene ports.</p>
<p>The PHP version of Lucene is packaged in the <a href="http://framework.zend.com/">Zend framework</a>and is called Zend_Search_Lucene.  When it comes to PHP frameworks, I tend to prefer using <a href="http://codeigniter.com/">CodeIgniter</a>as opposed to Zend.  So, you might ask, how can you use a favored framework such as CodeIgniter with the power of Lucene&#8217;s search capabilities?</p>
<p><strong>Install CodeIgniter 1.7.1</strong></p>
<p>I downloaded a copy of the latest version of <a href="http://codeigniter.com/downloads/">CodeIgniter 1.7.1</a> and configured it to run the default welcome action.  Next, I made a copy of the welcome controller and view to test my indexer and search actions (which we&#8217;ll get to in just a minute).</p>
<p><strong>Install Zend Framework 1.7</strong></p>
<p>Next, I downloaded the latest version of the <a href="http://www.zend.com/community/downloads">Zend Framework 1.7.5</a>.  After extracting the zip file, copy the Zend folder inside ZendFramework-1.7.5/library and paste it into the CodeIgniter framework under  System/application/libraries.</p>
<p><strong>Create A Zend Loader Class</strong><br />
The next thing that needs to be done is create a loader file to load the Zend library classes in CodeIgniter.  <a href="http://www.beyondcoding.com/2008/02/21/using-zend-framework-with-codeigniter/">This tutorial</a> also explains how to create this loader class.  This file below is named Zend.php and should be located in the System/application/libraries folder of CodeIgniter.</p>
<pre class="brush: php;"> &lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CI_Zend
{  
    function __construct($class = NULL)  
    {   
        // include path for Zend Framework   
        // alter it accordingly if you have put the 'Zend' folder elsewhere   
        ini_set('include_path',   ini_get('include_path') .
        PATH_SEPARATOR . APPPATH . 'libraries');

        if ($class)   
        {    
            require_once (string) $class . EXT;
            log_message('debug', &quot;Zend Class $class Loaded&quot;);
        }
        else
        {    
            log_message('debug', &quot;Zend Class Initialized&quot;);
        }  
    }

    function load($class)  
    {   
        require_once (string) $class . EXT;   
        log_message('debug', &quot;Zend Class $class Loaded&quot;);  
    }
}
//End of File: Zend.php</pre>
<p><strong>Creating An Indexer</strong></p>
<p>Now we will create the search index.  For demonstration purposes, I&#8217;m going to place the indexer and search functions in the same controller.  You should have your indexer in a separate controller with security that will keep everyone from being able to run it.</p>
<p>We&#8217;ll start with the copy of the welcome controller, which I named home.php.  After changing the class name and function calls to home instead of welcome, the contents of the file should look like this.  Also, add the sanitize function below.</p>
<pre class="brush: php;"> &lt;?php
class Home extends Controller
{  
    function Home()  
    {
        parent::Controller();
    }    

    function index()  
    {   
        $this-&gt;load-&gt;view('home_view');  
    }

    function sanitize($input)
    {
        return htmlentities(strip_tags($input));
    }
}
/* End of file home.php */
/* Location: ./system/application/controllers/home.php */</pre>
<p>Now, we can just replace the contents of the index() function with the following.</p>
<pre class="brush: php;">$this-&gt;load-&gt;library('zend', 'Zend/Feed');   
$this-&gt;load-&gt;library('zend', 'Zend/Search/Lucene');   
$this-&gt;load-&gt;library('zend');   
$this-&gt;zend-&gt;load('Zend/Feed');   
$this-&gt;zend-&gt;load('Zend/Search/Lucene');     

//Create index.   
$index = new Zend_Search_Lucene('c:\wamp\www\ci\tmp\feeds_index', true);      
$feeds = array(    
    'http://www.cmjackson.net/feed/rss/',    
    'http://andrewmjackson.com/feed/rss');       

//grab each feed.   
foreach($feeds as $feed)   
{    
    $channel = Zend_Feed::import($feed);    
    echo $channel-&gt;title().'&lt;br /&gt;';        

    //index each item.    
    foreach($channel-&gt;items as $item)    
    {     
        if ($item-&gt;link() &amp;&amp; $item-&gt;title() &amp;&amp; $item-&gt;description())     
        {      
            //create an index doc.      
            $doc = new Zend_Search_Lucene_Document();            
            $doc-&gt;addField(Zend_Search_Lucene_Field::Keyword(
                'link', $this-&gt;sanitize($item-&gt;link())));      
            $doc-&gt;addField(Zend_Search_Lucene_Field::Text(
                'title', $this-&gt;sanitize($item-&gt;title())));      
            $doc-&gt;addField(Zend_Search_Lucene_Field::Unstored(
                'contents', $this-&gt;sanitize($item-&gt;description())));            

            echo &quot;\tAdding: &quot;. $item-&gt;title() .'&lt;br /&gt;';      
            $index-&gt;addDocument($doc);     
        }    
    }   
}      

$index-&gt;commit();      
echo $index-&gt;count() .' Documents indexed.&lt;br /&gt;';</pre>
<p>This indexer will read in the RSS feeds from this website as well as <a href="http://www.andrewmjackson.com">my brother&#8217;s</a>website and index the contents of the feed.  When the index is created, you must specify a location to store the index.  These are binary files that Lucene creates and it does not require a database for storage.</p>
<p>In <a href="http://devzone.zend.com/node/view/id/91">this article</a>, the author further explains the fields of the index document and when each should be used.</p>
<p>Feeds are not the only resource that Lucene can index.  Web sites, databases, Microsoft Office documents, etc.  Find out more information on Zend Search Lucene in the <a href="http://framework.zend.com/manual/en/zend.search.lucene.html">Zend Framework Manual </a>.</p>
<p><strong>A Basic Search</strong></p>
<p>After running the indexer, you are ready to try searching the documents that are indexed.  For demonstration purposes, I&#8217;ve added another function to the same controller as the index called search().  This function does not get the results of a form, but instead simulates a string query as if it were from a form.</p>
<pre class="brush: php;">function search()  
{   
    $this-&gt;load-&gt;library('zend', 'Zend/Search/Lucene');   
    $this-&gt;load-&gt;library('zend');   
    $this-&gt;zend-&gt;load('Zend/Search/Lucene');      

    $index = new Zend_Search_Lucene('c:\wamp\www\ci\tmp\feeds_index');      

    $query = 'new movie';      
    $hits = $index-&gt;find($query);      

    echo 'Index contains '. $index-&gt;count() .
        ' documents.&lt;br /&gt;&lt;br /&gt;';   
    echo 'Search for &quot;'. $query .'&quot; returned '. count($hits) .
        ' hits&lt;br /&gt;&lt;br /&gt;';      

    foreach($hits as $hit)   
    {    
        echo $hit-&gt;title .'&lt;br /&gt;';    
        echo 'Score: '. sprintf('%.2f', $hit-&gt;score) .'&lt;br /&gt;';    
        echo $hit-&gt;link .'&lt;br /&gt;&lt;br /&gt;';   
    }    
}</pre>
<p>This function loads the same index that we previously created and searches for the key phrase &#8216;new movie&#8217;.  The results that are returned are sorted by their score ranking.  To make the search results look more like google, styling could be added  as well as formatting the result entries, but this gives you a good idea of the basic functions of the search engine and how it works.<code></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2009/02/17/how-to-use-zend_search_lucene-with-the-php-framework-codeigniter/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

