‘Hello World’ with CodeIgniter

Posted on December 1, 2008. Filed under: CodeIgniter | Tags: |

Hi everybody! This an english version of my other blog. Well, I hope you will understand if I have some mistakes redacting this post and the rest but My native language is Spanish and I just speak a little English.

Well, here you have my first post where I’ll show you how to build a simple example, the most used… yes… the famous ‘Hello World’ with CodeIgniter.

So, let’s start!

First of all, we need to download the framework called CodeIgniter. Actually there are several frameworks on the Web, but this one works very well. It’s very light and powerful.

Well, it’s not necessary configure any files from this framework, at least not yet. You have to download this framework and then extract it into your folder ‘www’. ( You know… the folder where you save all your PHP aplications).

If we already have all the framework in our folder, so we can run the aplication. Go to your browser and write ‘http://localhost/’ + the name of the folder where you have the framework. In my case, I have extracted it in ‘www\codeigniter\’ so I will write in my url: ‘http://localhost/codeigniter’

If you followed me so you are seeing this in your browser:

It means that it works perfect. But we see that if we write: ‘http://localhost/codeigniter/index.php’, we will get the same. It is because CodeIgniter works with this file and loads by default working as a router intercepting the request we did. And now we wonder, why the welcome page works automatically. Well, the index.php loads a controller which we write in a url like this: ‘http://localhost/codeigniter/index.php/welcome’. In this case, that word: ‘welcome’ is the name of the file where is all what we see.

CodeIgniter’s url works like this: next to ‘index.php’ goes ‘controller/function/parameter’, and if we write ‘http://localhost/codeigniter/index.php’ so it loads ‘welcome.php‘ because CodeIgniter configures this controller by default, and it’s something that we can modify, but we see it in other post of this blog.

Well, let’s continue with our example: ‘hello world’.

Create a new file, in my case I will call it: ’saludo.php’ and I will save it in ‘www\codeigniter\system\application\controllers\’

Well, in that file we will write this:

< ?PHP
class saludo extends Controller
{
	function index()
	{
		echo 'Hola mundo!';
	}
}
?>

Done! Now let’s go to our browser and write the url, in my case: ‘http://localhost/codeigniter/index.php/saludo’

It’s time to explain you the url.

We see before to ‘index.php’ this ‘codeigniter’. Well, as I said before, that’s the name of the folder where I extract the framework and obviously our aplication too.

About ‘index.php’, it was explained in the first lines. Finally, we see ’saludo’. Well, it refers to my controller ’saludo’, where there is a class called: ‘Saludo’. All of the controllers that we create have to contain a class with the same name but the first letter in capital letter.

Anyway, the result is:

With this, I am trying to help you to learn using CodeIgniter. any suggestions, corrections, etc. so leave me a comment.

That’s all for this post!

Thanks!  =)


Read Full Post | Make a Comment ( None so far )

Recently on Shared Code...

Hello world!

Posted on December 1, 2008. Filed under: Uncategorized |

Liked it here?
Why not try sites on the blogroll...