CodeIgniter First Example
A controller is set up to handle static pages. A controller is a class that simplifies the work in CodeIgniter.
In a CodeIgniter framework URL a basic pattern is followed.
In the following URL,
http://abc.com/book/novel/
Here, 'book' is the controller class or you can say this is the controller name.
'novel' is the method that is called. It extends to CI_Controller to inherit the controller properties.
An Example to print Hello World
Create file in Controllers
In Controller create a file named "Hello.php". This file will be saved in the Controller folder of your CodeIgniter. Write the following coding.
Here, your controller class is Hello, extends to CI_Controller means they can access the methods and variables defined in the controller class.
$this loads views, libraries and command the framework.
Create file in Views
Create a file named "hello_world.php". Save this file in the View folder of your CodeIgniter. Write the following coding.
Run the Controller file
To run the file, follow the path http://localhost/CodeIgniter/index.php/Hello/
CodeIgniter URL
CodeIgniter URLs are SEO friendly. Instead of using a 'query-string' approach, it uses a segment-based approach.
Basic URL structure
abc.com/class/function/ID
class represents controller class that needs to be invoked.
function is the method that is called.
ID is any additional segment that is passed to controllers.
What is site_url();
You can pass a string or an array in a site_url() function. In this example we'll pass a string,
echo site_url('book/novel/fiction');
The above function will return something like this
http://abc.com/index.php/book/novel/fiction
In this example we'll pass an array,
$data = array('book', 'novel', 'fiction');
echo site_url($data);
What is base_url();
It returns your site base URL, if mentioned any, in the config file. On passing base_url(), it also returns the same thing as site_url() with eliminating index.php. This is useful because here you can also pass images or text files. Here also you can pass a string or an array.
In this example we'll pass a string,
echo base_url("book/novel/fiction");
The above function will return something like this http://abc.com/ book/novel/fiction
What is uri_string();
It returns the URI segment of a page. For example, if your URL is,
http://abc.com/book/novel/fiction
Then, uri_string() will return
Book/novel/fiction
What is current_url();
Calling this function means, it will return the full URL of the page currently viewed.
Please note -> calling this function is same as calling uri_string() in site_url().
current_url() = site_url(uri_string());
What is index_page();
It will return your site's index_page which you have mentioned in your config file. By default, it is always index.php file.
You can change it with the help of .htaccess file.
anchor()
It creates a standard HTML link based on your local site URL. For example,
Echo anchor('book/novel/fiction', 'My Collection, 'title="book name");
It will give the following result,
anchor_popup()
It is identical to anchor() but it opens the URL in a new window.
mailto()
It creates a HTML email link. For example,
Echo mailto('abc@abc_site.com', 'To contact me click here')
url_title()
It takes a string as an input and creates a human friendly environment. For example,
Output will be "CodeIgniters-examples"
If you'll pass a second parameter, it defines word delimiter.
Output will be "CodeIgniters_examples"
If you'll pass a third parameter, it defines uppercase and lowercase. You have Boolean options for this, TRUE/FALSE.
Output will be "codeigniters_examples"
Basic Site creation in CodeIgniter
Here, we'll learn how to create a basic site with the help of CodeIgniter.
In controllers folder we'll create a file named as Form.php
We have created different files for header, nav, content and footer and all these files are loaded in the contoller's file.
File header.php in application/views
File nav.php in application/views
File content.php in application/views
File footer.php in application/views
Final output is shown below with URL localhost/site_example/index.php/Form.
CodeIgniter Methods
In the earlier Hello World example, our method name is index(). By default Controller always calls index method. If you want a different method, then write it in the Controller's file and specify its name while calling the function.
Look at the URL, there is no method name is mentioned. Hence, by default index method is loaded.
Method other than index()
Here, we have mentioned a method called newFunction(). Now we have to call this new method to run our program.
Create a controller page Hello.php in application/controllers.
Look at the above snapshot, we have created a function newFunction.
Create a view page hello_world.php in application/views.
To run this program on our browser, follow path
http://localhost/CodeIgniter/index.php/Hello/newFunction
Look at the above snapshot, we created the Controller's function as newFunction and specified it in the URL after Controller's name.
Here, /index.php/Hello is Controller's name.
And /newFunction is the Function name.
Remapping Method Calls
Second segment of URI determines which method is being called. If you want to override it you can use _remap() method.
If you have mentioned _remap() method in your controllers, it will always get called even if URI is different. It overrides the URI.
CodeIgniter Helper
What is Helper
In CodeIgniter there are helpers which are there to help you with different tasks. Every helper file is a collection of functions aiming towards a particular role. Some of the helpers are 'file helpers' which help you to deal with the file, 'text helpers' to perform various text formatting routines, 'form helpers' to create form element, 'cookie helpers' set and read cookies, 'URL helpers' which assist in creating links, etc.Helpers are not written in Object Oriented format, instead they are simple, procedural functions, independent of each other.
To use helper files, you need to load it. Once loaded it is globally available to your controller and views. They are located at two places in CodeIgniter. CodeIgniter will look first for a helper in application/helpers folder and if not found there then it will go to system/helpers folder.
Loading a Helper
A helper can be loaded in controller constructor which makes them globally available, or they can also be loaded in specific functions which need them.It can be loaded with the following code:
To load URL helper,
Loading Multiple Helpers
To load multiple helpers, specify them in an array,html Helper Example
We are going to show you an example of html helper by using it in a basic website page. Here, we'll auto-load our helper.Go to autoload.php file via application/config/autoload.php
In application/controllers there is file Form.php
The first line is coded in php tag.
In application/views there is file content.php
Here also the heading is written in php tag instead of html.
But when we will see its open source (by pressing ctrl+u), you will see the following coding which simply shows html code and not php code what we have written above.
CodeIgniter Library
What is a Library
CodeIgniter provide a rich set of libraries. It is an essential part of CodeIgniter as it increases the developing speed of an application. It is located in the system/library.Loading Library
CodeIgniter library can be loaded as following,To load multiple libraries, use following code,
Creating Libraries
All the CodeIgniter libraries are placed in system folder. But in case if you want to use any other library in your application you can create it. There is no limitation for libraries. But your created libraries will be stored in the application/libraries folder. This is done to separate your local and global framework resources.There are three methods to create a library,
- Creating an entire new library
- Extending native libraries
- Replacing native libraries
Creating an entire new library
It should be placed in application/libraries folder.Naming Conventions
- File name first letter has to be in uppercase letter, like Mylib.php
- Class name first letter should also be in uppercase letter
- File name and class name should be same.
Suppose your file name is Mylib.php, then syntax will be as follows,
Loading Mylib.php
It can be loaded with the following line,
Accessing mylib.php
Once it is loaded, you can access your class using the lower case letter because object instances are always in lower case.
Extending Native Libraries
You can also add some extended functionality to a native library by adding one or two methods. It will replace the entire library with your version. So it is better to extend the class. Extending and replacing is almost identical with only following exceptions.- Class declaration must extend the parent class.
- New class name and filename must be prefixed with MY_.
Replacing Native Libraries
Naming the new file and class name same as the native one will cause the CodeIgniter new one instead of native one. File and class declaration should be exactly same as the native library.For example, to replace native Calendar library, you'll create a file Calendar.php in application/libraries. And your class will be,
Good job! For Sharing your best ideas... I will refer the people to best learning and training Institute for online courses... click the below link:
ReplyDeleteshort courses in pakistan
react native advanced concepts
sorting algorithms c++
graphic designing institute
mern stack course online
php training institute
e commerce online course in pakistan
Online short courses in Pakistan
Tech Inn Solutions
Good Job! You have Shared your well knowledge... I will refer the people to the best IT Solutions providers click the below link:
ReplyDeleteit solutions website
pakistan web developer
app development in pakistan
software developers in pakistan
digital marketing in pakistan
website maintenance packages
professional it consulting services
Cre8tivebot
Software Development in Pakistan