IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Introducing codeIgniter

2 posters

Go down

Introducing codeIgniter  Empty Introducing codeIgniter

Post by BIT0112-Rokon Sat Jul 10, 2010 6:47 pm

You may have offline version of this tutorial. CILICK HERE

Introduction

I think we have some basic idea about php. It is server side scripting language commonly used now a days. If you dont have any idea about php, I would like to recommend you to read some books on php first to learn some basic syntax. There are lots of php books is available in Amazon.com. Personally I first I learn php syntax from w3schools. It may be better place for you if you are beginner. But I strongly recommend you to read a book on php first. Anther best place to learn php from php manual.

Why php anyway?

Anyway, firstly I want to tell something about php why we are going to learn it. In a web page, we can have lots of static element like image, text etc. But if we want something dynamic, we need some dynamic programming. For dynamic web programming we need know some programming language. Java EE is the best for dynamic web programming as far as I know. But now a days php is highly used as a dynamic web programming language. I've worked with Java EE, but it's development complexity is so high, so that, now I’m getting interest to learn php. Php has low development complexity. But its maintenance complexity is high and not so robust as Java EE.

By the way, in this text I would like to introduce you a php framework, so that, your life can easy to be develop a web based application.

Why framework ?


Sometime it not possible to learn a complex technology within a short time. To make a technology easier and flexible enough, the term framework comes out. To know more about framework, I would like to
redirect you this page.
In this text, I would like to introduce you CodeIgniter. It is a php framework. Very small and really light weight framework. There is lots of framework on php. Click here to get introduced with php framework. You may ask which one is the best. Actually its depends on perspective. You can choose CackPHP or Zend Framework or Symfony or any. Its up to you. Every framework has some limitation. You can read php framework review from here.

Prerequisites:

To work with codeIgniter , you need some tools.

1. PHP 5 or above
2. mysql
3. apache server
4. codeIgniter framework

What you need to download:


  1. eclipse pdt- click here to download
  2. xampp- click here to download
  3. codeIgniter -click here to download

If you have already installed those, then go ahead to configure codeIgniter. One warning for skype user, before starting wampp or xampp, please shutdown your skype application. There may happen port conflict. After starting wampp or xampp, you can start your skype again.

Lets start

Start your xampp or wampp. Im using xampp. So in this text I use xampp only. I hope wampp will work similarly.

For linux user open your terminal.

Then write
~$ sudo /opt/lampp/lampp start

It will start xampp. Windows user can use easily from all programs.

Now open your xampp directory. For linux user, go

File System/opt/lampp/htdocs

and windows user find it from C

now open terminal. Write

~$ cd /opt/lampp

~$ sudo chmod -R 777 htdocs

now its ready to work.

So first make a test. Open your htdocs folder and open a new file with extension .php.

For me it is hello.php

now write there some php code like

Code:

echo ‘Hello world’;

?>

now open your favorite browser like mozila firefox.

Now write on address bar . http://localhost/hello

what happen ? Can you hello world printed in your browser? If, everything is okay. Now we can start.

Now open your eclipse pdt and make its default workspace, opt/lampp/htdocs.

For windows user, it should be like c:/xampp/htdocs

Now open a new php project like the screen-shot given below –


Look carefully on directory. If it is /opt/lampp/htdocs/CodeIgniter_first then okay. Otherwise change it. Now click on finish button.

Now go to your code-igniter directory where you downloaded it. If it is zipped, unzip it and copy the system folder and index.php .

Now go to your eclipse’s php explorer and paste it in your project. It will like-

now it is time to change some default setting on the code-igniter. Go to CodeIgniter_first/system/application/config. And open the config.php file.

Find the line at line 14

$config['base_url'] = “http://example.com/“;

change it to

$config['base_url'] = “http://localhost/CodeIgniter_first/“;

now save it and open your browser and write there, http://localhost/CodeIgniter_first/index.php

now you’ll get a page like


if you get the page, then it is okay.. your code-igniter is working fine.

Now you need to have some knowledge about mvc.

MVC

MVC stands for model view and controller. What is it. Okay stay calm, Im here will try to explain it shortly.

MVC is nothing but a design pattern. Go to wiki to learn about it though here, Im trying to give a short explanation. We know, now a days, our application is being very complex and it contains lots of mixture like business logic, data access code, presentation code. So its sometime very difficult to maintain without any design pattern. MVC patter makes it easy to maintain. It separate all those things so that development can be easy and maintainable.

So now come to the point what is model view and controller.

More generally the duties of model view and controller is given below-

Model- encapsulates core application data and functionality. Typically it contains some functions that help you retrieve, insert, and update information in your database.

View- obtains data from the model and presents it to the user. It is basically web page, that is presented to users through web browsers.

Controller- receives input and initiates a response by making calls on model objects and return a view.


Recommended reading for model and view and controller:

http://java.sun.com/blueprints/patterns/MVC.html

http://java.sun.com/blueprints/patterns/MVC-detailed.html

http://msdn.microsoft.com/en-us/library/ff649643.aspx

http://www.tonymarston.net/php-mysql/model-view-controller.html#introduction

http://www.phpwact.org/pattern/model_view_controller


Now we better start our coding.

Open a new php file under CodeIgniter_first/system/application/controllers

name it hello.php and write the code given below there

Code:

class Hello extends Controller{

function Hello(){

parent::Controller();

}

function index(){

$data['bazlur'] = ‘Bazlur Rahman’;

$this->load->view(‘hello’, $data);

}

}

look carefully-

The class must extend Controller.

The class name must be capitalized.

The file name must be lowercase.

Recommended for reading : http://codeigniter.com/user_guide/general/controllers.html

This is our controller. Now we need to write a view. Open a new php file under CodeIgniter_first/system/application/views

and name it hello.php and write the code there given below-

Code:


echo ‘Hello, ‘ .$bazlur;

?>
Recommended reading : http://codeigniter.com/user_guide/general/views.html

now open your browser and hit it with http://localhost/CodeIgniter_first/index.php/hello/index

what is there? Hopefully it will print

Hello, Bazlur Rahman

Now we will write more complex application which will able to store data in data base and collect data from database.

Now lets make a application that can make login system. We know most web application requires a user account and so that we need to register first and then need to login.

To do this, we need to have a plug-in for code-igniter that is Doctrine.

Download the stable version of Doctrine
from here.


Now you may have a question what is doctrine.

Doctrine is a Object relational mapper (ORM) for PHP that sits on top of a powerful database abstraction layer (DBAL). One of its key features is the option to write database queries in a proprietary object oriented SQL dialect called Doctrine Query Language (DQL), inspired by Hibernates HQL.

In Java, hibernate is something alternative to SQL. Java programmer don’t have the headache of data, they always work with object. So working with data in database is problem for Java programmer. Here hibernate solves the problems of Java programmers.

To know more about doctrine, go to doctrine projects’ homepage.

Now create a folder under system/application/ and name it plugins/doctrine. Extract the downloaded doctrine file, find lib folder, copy it and paste it in doctrine folder.

Now we need to create a plugin file under the system/application/plugins name it doctrine_pi.php and write the code there given below-

Code:


// load Doctrine library

require_once APPPATH.‘/plugins/doctrine/lib/Doctrine.php’;

// load database configuration from CodeIgniter

require_once APPPATH.‘/config/database.php’;

// this will allow Doctrine to load Model classes automatically

spl_autoload_register(array(‘Doctrine’, ‘autoload’));

// we load our database connections into Doctrine_Manager

// this loop allows us to use multiple connections later on

foreach ($db as $connection_name => $db_values) {

// first we must convert to dsn format

$dsn = $db[$connection_name]['dbdriver'] .

‘://’ . $db[$connection_name]['username'] .

‘:’ . $db[$connection_name]['password'].

‘@’ . $db[$connection_name]['hostname'] .

‘/’ . $db[$connection_name]['database'];

Doctrine_Manager::connection($dsn,$connection_name);

}

// CodeIgniter’s Model class needs to be loaded

require_once BASEPATH.‘/libraries/Model.php’;

// telling Doctrine where our models are located

Doctrine::loadModels(APPPATH.‘/models’);

// (OPTIONAL) CONFIGURATION BELOW

// this will allow us to use “mutators”

Doctrine_Manager::getInstance()->setAttribute(

Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);

// this sets all table columns to notnull and unsigned (for ints) by default

Doctrine_Manager::getInstance()->setAttribute(

Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,

array(‘notnull’ => true, ‘unsigned’ => true));

// set the default primary key to be named ‘id’, integer, 4 bytes

Doctrine_Manager::getInstance()->setAttribute(

Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,

array(‘name’ => ‘id’, ‘type’ => ‘integer’, ‘length’ => 4));

?>
Now we need make a database. Go to your web browser and write there http://localhost/phpmyadmin/

and make a database. For me it codeigniter

now goto system/application/config/database.php

open database.php and make changes like from the line 40

Code:

$db['default']['hostname'] = “localhost”;

$db['default']['username'] = “root”;

$db['default']['password'] = “”;

$db['default']['database'] = “codeigniter”;

$db['default']['dbdriver'] = “mysql”;

$db['default']['dbprefix'] = “”;

$db['default']['pconnect'] = TRUE;

$db['default']['db_debug'] = TRUE;

$db['default']['cache_on'] = FALSE;

$db['default']['cachedir'] = “”;

$db['default']['char_set'] = “utf8″;

$db['default']['dbcollat'] = “utf8_general_ci”;

open autoload.php from system/application/config/

go to line 66 and change it like

$autoload['plugin'] = array(‘doctrine’);

okay now lets go forward ..

now lets write a model under system/application/models/user.php

Code:


class User extends Doctrine_Record {

public function setTableDefinition() {

$this->hasColumn(‘username’, ‘string’, 255, array(‘unique’ => ‘true’));

$this->hasColumn(‘password’, ‘string’, 255);

$this->hasColumn(‘email’, ‘string’, 255, array(‘unique’ => ‘true’));

}

public function setUp() {

$this->setTableName(‘user’);

$this->actAs(‘Timestampable’);

}

}
and now lets write a controller under under system/application/controller/ Doctrine_Tools.php

Code:


class Doctrine_Tools extends Controller {

function create_tables() {

echo

action="" method="POST">
type="submit" name="action" value="Create
Tables">';

if ($this->input->post(‘action’)) {

Doctrine::createTablesFromModels();

echo “Done!”;

}

}

}
now go to your web browser and write there http://localhost/codeIgniter_first/index.php/doctrine_tools/create_tables

there will appear a button named Create Tables.

Click on it.

What is there!!!!
A PHP Error was encountered


Severity: Warning

Message: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect via unix://)

Filename: Doctrine/Connection.php

Line Number: 476
Fatal error: Uncaught exception ‘Doctrine_Connection_Exception’ with message ‘PDO Connection Error: SQLSTATE[HY000] [2002] Invalid argument’ in /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Connection.php:480 Stack trace: #0 /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Connection/Mysql.php(101): Doctrine_Connection->connect() #1 /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Transaction.php(186): Doctrine_Connection_Mysql->connect() #2 /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Connection.php(1384): Doctrine_Transaction->beginTransaction(NULL) #3 /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Export.php(1201): Doctrine_Connection->beginTransaction() #4 /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Export.php(1099): Doctrine_Export->exportClasses(Array) #5 /opt/lampp/htdocs/codeIgniter_first in /opt/lampp/htdocs/codeIgniter_first/system/application/plugins/doctrine/lib/Doctrine/Connection.php on line 480
its a great problem I think.. very view people faced it.. no problem, as a linux user, we have to solve it. Let see how..

goto /opt/lampp/etc and find the php.ini file. Now open the terminal and

write there

:~$ cd /opt/lampp/etc

:/opt/lampp/etc$ sudo chmod -R 777 php.ini

give your password and press enter button.

Now open your php.ini file

goto 1099 and change it like

pdo_mysql.default_socket=/opt/lampp/var/mysql/mysql.sock

save it.

and now restart your xampp.

Now refresh your web page. Hope you’ll see a message done! In your web browser. Its now okay. Goto xampp and see.. your table is created.

Credit line:

http://www.codeproject.com/KB/architecture/WhatIsAFramework.aspx

http://www.phpframeworks.com/

Stay tuned for next tutorial .. …… Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy


Last edited by bit0112-rokon on Sun Jul 11, 2010 10:10 am; edited 4 times in total
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Introducing codeIgniter  Empty Re: Introducing codeIgniter

Post by BIT0122-Amit Sat Jul 10, 2010 10:58 pm

Ah.. you are gettting Better in making tutorials Very Happy Good!

I am also expecting that the terminal commands be given the code tag, so that they can become more noticeable.

I have not read the whole tutorial, but reading half of it earned you rep++ Smile
keep it up.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Introducing codeIgniter  Empty Re: Introducing codeIgniter

Post by BIT0112-Rokon Sun Jul 11, 2010 8:56 am

I've attached offline version which is full featured with images. you may download it.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Introducing codeIgniter  Empty Re: Introducing codeIgniter

Post by BIT0112-Rokon Sun Jul 11, 2010 10:18 am

BIT0122-Amit wrote:Ah.. you are gettting Better in making tutorials Very Happy Good!

I am also expecting that the terminal commands be given the code tag, so that they can become more noticeable.

I have not read the whole tutorial, but reading half of it earned you rep++ Smile
keep it up.

read the whole tutorial, hope it will help you if you wanna get introduced with a framework.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Introducing codeIgniter  Empty Re: Introducing codeIgniter

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum