Working With Codeigniter Views
What is view in codeigniter?
In codeigniter a view is the most important aspect from user point of view. A view is the actual HTML displayed on user’s browser after all processing is complete. Think about a website which is unorganized, elements are not properly working and lot of errors, will search engines rank it? Or will you visit such websites? The answer will be a big “NO” that is why people invest huge amount of money in designing part of any website to make it SEO and user friendly.
A good developer codes a website or web application which is user friendly first, because a website is driven by users, if users don’t like the application then the application becomes worthless. So let us learn now how we can add a view to codeigniter.
Adding View in codeigniter:
Create a html file with .php extension and add some elements into it for testing purposes add few div element and some text to it or you can download the sample file I have added here in the post to see an example how the tags look like. For now we are not breaking views into parts that we will do later on, for start let us try with a single view template or file.
Once you are done creating the file copy that file and open your application/views directory inside your codeigniter installation and paste it there now you are ready to load the view and display your html contents.
Loading the view
Now we will load the view file we just created and display it in the user screen, for this go in to the controller directory and open Home.php in your text editor. Remove everything inside the index function and add this line.to index function.
Note: view(‘home’) here home is the view file inside the views folder, if you have used a different name then replace home with name of your view file.
This function tells codeigniter to load view home so when the Home controller is loaded then it by default starts loading the home.php view file as it is the index function of that controller. For help please view the snap of my home controller below.
Now open up your browser and load the home controller ie. http://localhost/codeigniter/index.php/home
If you have done everything properly then you will see your html printed on your browser screen congratulations you have added your first view successfully. Else recheck everything and make sure you do not miss any thing. If you have any questions comment here or email me on my address.