Showing posts with label ci. Show all posts
Showing posts with label ci. Show all posts

Saturday, January 27, 2018

Views in codeigniter ... MVC Pattern

As we know, codeigniter works on the MVC pattern, which is Models, Views and Controllers.
We have discussed in detail about the controllers in previous post. Now we are going to discuss about the Views.

First of all we should know that 

What are views in codeigniter ?

Views are considered to be the special files in Codeigniter that are used to store the markup outputted by the applications. Views are consist of HTML and PHP tags. As we all know that HTML is used in front-end designing of websites or any web application. We also know that some of the PHP tags are also used in designing the interfaces of any web application.

A view can be defined as a simple web page, or a page fragment, or any interface of web application with which the end-user is going to interact like header, footer, sidebar or body etc.

How views are created ?

Views can be created by creating a new file  in the views folder which resides in the application folder of codeigniter and saving the file with the .php extension. let say, you have created a file in views folder and saved it with name myView.php. Your View will be created.

The code in the myView.php file is as follow:

<html>
</body>
<h1> This is my View </h1>
<p> I am going to be loaded in the controller that we have created in the previous post </p>
</body>
</html>

How to load Views in Controllers?

The views that we create in the views folder are loaded in controller in order to be displayed on the web. The syntax for loading a view in the controller is as follow:

$this->load->view('myView');

There is no need of giving extention of the file that is .php because codeigniter is an intelligent framework.

Full Code in controller for loading the view:

<?php class MyController extends CI_Controller {
// its the controller that we created in the previous post

public function index(){       // it is the by default function that is called by controller itself
$this->load->view('myView');     // its the view that we have created above
}
}
?>

I hope this would be a help for you. God bless you all. share this post and comment your queries, if any . Thankyou very much

Friday, January 26, 2018

Controllers and its functions in Codeigniter

What is a Controller ?

A Controller is simply a class file that is named in a way that can be associated with a URI. Consider this URI: example.com/index.php/ blog / In the above example,CodeIgniter would attempt to find a controller named blog.php and load it. When a controller's name matches the first segment of a URI, it will be loaded.

As you all know about the URI segments. In codeigniter, the first segment is the name of the controller. The second segment is the name of that controller's method which is being called. And the third segment can b any value that is passed in any method as a parameter. e.g

www.example.com/Mycontroller/method/parameter

Creating a Controller:

You can create a controller simply by opening the controller folder, its path is as follow:

....htdocs/codeigniter/applications/controller

After opening the controller folder, create a new file by giving the name of your choice and save it with a .php extension. Keep the UCfirst approch in naming your controller i.e keep the first letter of your controller's name capital. It is a good practise. e.g Mycontroller.php

After creating your file write the following code in that file. Firstly, to make that file work as a controller, you need to extend the parrent class of controller that is CI_Controller. Here is an example of syntax of declaring and defining a controller class:


This is valid:

<?php class MyController extends CI_Controller {
//name of the class is same as you have given the name to the controller file
}
?>

This is not valid:

<?phpclass myController extends CI_Controller {
//name of the class cannot have first letter in lower case
//class cannot have any other name than the one with which the controller file is saved
}
?>

Creating a Controller's Methods/Functions:
Now, after creating class of controller and extending it from the parent controller class, you can define methods or functions here in the controller. Every controller class calls a by default function that is index(). Here is the syntax to declare and define a method or function in controller e.g
<?phpclass MyController extends CI_Controller {
public function index(){
//default function
echo"I am a by default function";
}
public function myFunction(){
//user defined function
echo"I am a user defined function";
}
}
?>


Codeigniter downloading and installation

Codeigniter Downloading Instruction:

you can download the complete package of codeigniter simply by following the link given below:

https://www.codeigniter.com/

Recently the latest version of codeigniter has been launched that is Codeigniter 3.1.7. You can also get the documentation of codeigniter from the above mentioned link which is a plus point of codeigniter that it has clear and well organized documentation. You can download codeigniter simply by clicking the download button.

Codeigniter Installation Instructions:

CodeIgniter is installed in four steps:

  • Unzip the package.
  • Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at your root.
  • Open the application/config/config.php file with a text editor and set your base URL. If you intend to use encryption or sessions, set your encryption key.
  • If you intend to use a database, open the application/config/database.php file with a text editor and set your database settings.
These are the simple steps to install the codeigniter. If your developing on a local server then move the codeigniter folder to your local server htdocs folder.

In the event that you wish to build security by concealing the area of your CodeIgniter documents you can rename the framework and application organizers to something more private. In the event that you do rename them, you should open your principle index.php record and set the $system_path and $application_folder factors at the highest point of the document with the new name you've picked. 

For the best security, both the system}and}any application envelopes ought to be put above web root with the goal that they are not straightforwardly available by means of a program. Of course, .htaccess documents are incorporated into every envelope to help avoid coordinate access, however it is best to expel them from community on the off chance that the web server design changes or doesn't keep the .htaccess. 

On the off chance that you might want to keep your perspectives open it is additionally conceivable to move the perspectives envelope out of your application organizer.
After moving them, open your main index.php file and set the $system_path, $application_folder and $view_folder variables, preferably with a full path, e.g. ‘/www/MyUser/system’.