Blog Details

Controllers


                                
                                    <h3>* What is a Controller?</h3>
&nbsp;
<ul>
	<li>A Controller is simply a class file that is named in a way that can be associated with a URI.</li>
</ul>
&nbsp;
<h4>Consider this URI:</h4>
&nbsp;
<ul>
	<li><strong>localhost/codeigniter/welcome/index.php/<var>blog</var>/</strong></li>
	<li>In the above example, CodeIgniter would attempt to find a controller named <dfn>blog.php</dfn> and load it.</li>
	<li>When a controller's name matches the first segment of a URI, it will be loaded.</li>
</ul>
&nbsp;
<h4>Example:</h4>
&nbsp;
<h5>Step 1 :</h5>
&nbsp;
<ul>
	<li>Let's create a simple controller so you can see it in action. Using your text editor, create a file called <strong><dfn>blog.php</dfn></strong>, and put the following code in it:</li>
</ul>
&nbsp;

&lt;?php
class Blog extends CI_Controller {

public function <strong>index</strong>()
{
echo 'Hello World!';
}
}
?&gt;

&nbsp;
<ul>
	<li>Then save the file blog.php to your <strong><dfn>application/controllers/</dfn></strong> folder.</li>
	<li>Now visit the your site using a URL similar to this:</li>
	<li><strong>localhost/codeigniter/welcome/index.php/<var>blog</var>/</strong></li>
</ul>
&nbsp;
<h4>Output:</h4>
&nbsp;

<samp>Hello World!</samp><samp></samp><samp></samp><samp></samp><samp></samp>.<strong>Note:</strong>
<ul>
	<li>Class names <strong>must start</strong> with an <strong>uppercase letter</strong>. In other words, this is valid:</li>
</ul>
&nbsp;
<h3>* How to Create Functions In CodeIgniter :-</h3>
&nbsp;
<h4>Consider this URI:</h4>
&nbsp;
<ul>
	<li><strong><strong><strong><strong>localhost/codeigniter/welcome/index.php/<var>blog</var>/
<samp>index.php/comments/</samp></strong></strong></strong></strong></li>
	<li>In the below example the function name is <strong><strong><strong><strong>
<samp>comments<strong><dfn>()</dfn></strong>.</samp></strong></strong></strong></strong><strong><dfn>
</dfn></strong></li>
</ul>
&nbsp;

&lt;?php
class Blog extends CI_Controller {

public function index()
{
echo 'Hello World!';
}

public function <strong>comments</strong>()
{
echo 'Look at this!';
}
}
?&gt;

&nbsp;
<ul>
	<li>Then save the file blog.php to your <strong><dfn>application/controllers/</dfn></strong> folder.</li>
	<li>Now visit the your site using a URL similar to this:</li>
	<li><strong><strong><strong><strong>localhost/codeigniter/welcome/index.php/<var>blog</var>/<strong>
<samp>index.php/comments</samp></strong></strong></strong></strong></strong></li>
</ul>
&nbsp;
<h4>Output:</h4>
&nbsp;

Look at this!
<h3></h3>
                                    


Categories

CodeIgnitor

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote