Press "Enter" to skip to content

CodeIgniter: How to Create pages using Functions

Creating pages using Functions in CodeIgniter

OK.. Let’s start the coding…

Before starting the coding, you must read the basics of the CodeIgniter frameworks those written in our previous articles.

Creating Controller

  • Now just open your Controllers folder.
CodeIgniter Controllers folder
  • There you can see two files names as ‘index.html’ and ‘welcome.php’. These are two default file to load. Just delete these files.
  • Now let us create a new PHP file. You must aware of basic concepts of plane PHP such as creating PHP files, basic PHP coding, etc.
  • Right click here and from context menu, select New-> Text document. And save the file with extension of ‘.php’.
  • Here created a PHP file with the name ‘user.php’.

Now let us open the file. Here Codelopster is used to open and edit PHP files. You can use any text editor even notepad, but Codelopster, sublime or Atom are free and recommended PHP text editors.

Inside the file, you have to create a Controller class like this:

<?php
class User extends CI_Controller
 {

 }
  • Class should be written after a PHP opening tag (<?php)
  • Class name should be equal to the file name. Here name of both class name and file name are ‘user’. But you need to capitalize the first letter of the class name (User).
  • You have to inherit the parent class CI_Controller by using keyword ‘extends’.
  • You don’t need to write the php closing tag (?>).
Done. Now you have completed the first process of creating a controller class.

Creating PHP pages

How do we create PHP pages in plane php? There we have to create separate php files for each pages on the project. But here, you don’t need to create any pages.
In CodeIgniter, every functions on the controller class act as separate and independent pages.
So if we want to create a PHP page, then just write a function with the name of the page.

Let us create a page with the name ‘index’.

  • First of all create a function with the name ‘index’ and you have to specify the type of the function, here which is ‘public’.
  • Function should ends with a parenthesis ().
  • The function should contain a body section with brace brackets {}.
  • The function should be inside the class (inside class brace brackets).
  • As a testing, let’s print ‘hello’ using echo.

 

<?php
class User extends CI_Controller
 {
  public function index()
   {
    echo "hello";
   }
 }

 

Creating a CodeIgniter Controller class file

Let us run the project…

We have learnt already in our first lesson about running a CodeIgniter project.
So we need to specify the name of controller and the name of the page (function) that we want to run.

CodeIgniter first output page

Like this, you can create many functions according the number of pages.
Here let us create two another pages named as ‘register’ and ‘login’.

<?php
class User extends CI_Controller
 {
  public function index()
  {
  echo "hello";
 }

public function login()
{
echo “Login here”;
}

public function register()
{
echo “New? Register”;
}
}

If you want to open your ‘register’ page, then browser url should be like this:

http://localhost/myproject/index.php/user/register

Running a function in codeigniter

Note:

I think you have already aware about the name ‘index’. The page (function) with the name ‘index’ will automatically run if we do not specify any page (function) name.

That is, if we run this URL, http://localhost/myproject/index.php/user(here any name of page or function is not specified), then PHP automatically run the page (function) named index.

Then the URL
http://localhost/myproject/index.php/user/index will work automatically.

Now we just learnt how to create pages in CodeIgniter. Yeah, It’s very easy…

Here we wrote all the texts (‘hello’, ‘login here’, ‘New? Register’) inside the controllers’ pages. But we never write any text or HTML codes inside the controller page. Controller page is only to write PHP codes. In previous lesson we leant that the ‘View’ section is used to write these type of text and HTML tags.

So let us learn those ideas in our next lesson..

Be First to Comment

Leave a Reply