Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Module 1 - PRESENTATION 1

Code Igniter
- “A powerful PHP framework with a very small footprint, built for developers who need a simple and
elegant toolkit to create full-featured web applications.” It sounds very complex but it is pretty
straightforward
- Developed by Ellislab and used by millions worldwide. Developers prefer to use CI for its rapid
application development. It has rich and reusable libraries and class for your website’s foundation.
Why use Code igniter or CI.
- CI is an application framework
- CI is free
- CI is light weight
- CI is fast
- CI uses Model-View-Controller architecture
- CI generates Clean URLs
- CI is Extensible
- CI is thoroughly documented
- CI has a friendly community of users
Skills needed
The following are the items you need to know before starting to play around CI.
1. PHP
o A good understanding of the PHP language is a must. As long as you can perform simple
instructions (e.g. variable declaration/assignments, conditions, loops), you will nearly understand
everything here since the Code Igniter is made up of PHP!
2. HTML
o Well, you do need those html elements to manipulate to. You’ll use this for the layout design,
inputs and presentation of data.
3. CSS
o It’s good if you know Cascading Style Sheets (CSS), and even better if you’re using external
style sheets. There are more web design processors available if you want to level-up your
knowledge on styling however if you don’t know how, still, you can learn CI.
4. MySQL
o This means your SQL Basic queries are required. There will some modules on this book where
you need to manipulate I/O (inputs and outputs) from your data storage or your database. Models
communicate with the database and uses your fundamental knowledge on SQL.
5. JavaScript
o It makes the website interact with the HTML elements. If you do not know JS, maybe learn it
sometime, okay? You need these for future developments.
CI Installation
Step 1. Visit its website on https://www.codeigniter.com/
Step 2. Find your way to the Download link and click Download.
Step 3. Extract the downloaded .zip file and place it on your local server.
Step 4. If you can see this Welcome Message. Your installation was a success!

MVC Web Framework

Controller
- This contains the Business logic (Business Logic Layer).
- controls the Model and View
- a class that contains methods that decides what to call, or perform and display
- When a user sends a request on a browser, the controller will determine the process and perform
tasks based on its method
- it includes calling a method returning a value from a model, displaying and passing values on a view or
showing a 404 error page

Views
- The Presentation layer
- This contains designs, layouts or templates
- It is the collection of what will show up on your browser.
- The view is where the user sees and interacts with.
Models
- It is also referred as the Data Access Layer
- The Model communicates with your database
- It comprises of methods that execute queries using the Code Igniter’s Active Record pattern.
CI Directory
- Basically, CodeIgniter has 3 main folders:
o Application
 You will work with your website in Application Folder.
o System
 The System folder, keep it a habit to avoid making changes to any of the files inside. It
contains the CodeIgniter’s core files which is necessary to make it run.
o User Guide.
 User Guide contains the offline documentations you need in developing with websites in
CI.
- Cache: This directory will contain all kinds of cached files if you are using so. However, you will may
need to provide write access for the application to this directory as codeigniter will need to create
temporary files on this folder. To know how to use caching, please refer to CodeIgniter caching tutorial.
- Config: This directory includes settings/configuration related information like database settings,
route information, constants declaration, items to be auto loaded etc.
- Controller: This directory includes all controller definitions. It can be said as the entry point of
application also, every requests triggers a certain method of a controller.
- Core : If we need to extend the functionality of any core classes like controller, loader, router etc, then
we can declare new class here which will extend those core classes and put the implementation inside
those.
- Errors: This directory includes some basic template for showing various kind of errors like db error,
php errors, 404 errors etc, which we can change later as per our requirements.
- Helpers: This directory will include all helper files.
- Hooks: This directory will include all hooks declaration.
- Language: This directory will include language files. By loading different language files, we can make
our application multilingual supported
- Libraries: This directory will include all library class files that might need to be created for our
applications.
- Logs: This directory will include all application logs. To know how to write log for debug/error/info,
please more about error handling
- Migrations: This directory will include migration helpers.
- Models: This directory will include all model classes used in our application. This section can be said
as the ‘data access layer’ of our application.
- Third_party: This directory will include library files, but only those, which are imported from
third-party. So, difference between the ‘third_party’ and ‘libraries’ is that, one is for self-made
libraries, for app specific, other one is for importing party libraries.
- Views: This directory will include all view template files.
PRESENTATION 2
Views
- A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc.
- Views serves as User Interfaces where web users can interact, request and receive data. Controllers
can load views and pass data to them
Creating Views
- Views are php files containing html elements. They are stored in application/views folder.
- Let’s have an example of a view
o Create a file place it at application/views/ and give it a name index.php. now type the following
codes. .

- Now we need to load the index.php (view) to our controller. To load the view we need go to the
controller, name the file as Home.php directory should be like this: application/controllers/Home.php

- To load the view, the function is like this $this->load->view('name'); the name is the name of the view
file, in our example to load the view you type $this->load->view(‘index’);

Passing Data to View


- Views are used to present data. Most data passed in a view are from the controller or model (from the
database). Data can be a string value, an array or an object. $this->load->view method has an optional
second parameter, which is the data to be passed.
$this->load->view(‘view_file_name’, $data);
- All you need is to define an array or array index to be passed. If you know what php extract function
does, the passed array from the controller is automatically extracted when passed on a view. The
associated array names turn into variables.

  View
Controller
$data[“greeting”] = “Hello World”; //some_page.php
  <?php
$this->load->view(‘some_page’, $data); echo $greeting;
  ?>
  //outputs:
//Hello World
$data = array( //some_page.php
   ‘lname’ => ‘Juan’,  
   ‘fname’ => ‘Cruz’ User: <?php echo $lname.’, ’.$fname; ?>
);  
  //outputs:
$this->load->view(‘some_page’, $data); //User: Cruz, Juan

- Let’s pass some data to our index.php view Update the index method on the Home controller: Don’t
forget the second parameter $data

- And also to our index view:

- Output:
Passing More Data
- Let’s pass more values from our controller using Arrays.
- Arrays are like variables.
o Array store multiple values or elements that can be accessed by its index or position. Arrays
can be Indexed (numerical positions starting with 0), Associative (or named positions), and
multi-dimensional (arrays within array).
- Say we have an array of products on our controller application/controller/Products.php

- And displays it on our view at application/views/products.php

- Output:

Understanding URI
- What is URI? URI stands for Uniform Resource Identifier. It is a string of characters used to
identify a resource
- forms of URI
o URL (Uniform Resource Locator) and URN (Uniform Resource Name)

URI, URL and URNs


- To make it easier to understand, for an example, there was Juan Dela Cruz as our given URN. There are
many Juan Dela Cruz in the country, thus we need to know whose Juan dela Cruz of what place or
address are we looking for. When you are searching for Juan Dela Cruz of Bulacan, it is now URL. Did
you get the idea?
- URN can be of any given name or address. We use URL to define what method and address is the
requested web page.
- The combination of both URL and URN resource is what we call specifically URI.

  URN
URL
https://facebook.com.com/dustoutlaw23 /profile/dustoutlaw23
https://www.youtube.com/channel/ /channel/
UCNHXyYCEqfmO8jBTlbFvLHA UCNHXyYCEqfmO8jBTlbFv
LHA

URL
- the location that includes the components: the path/method and the domain. It is basically called Web
Address
URN
- the historical name that serve as persistent, location-independent identifiers allowing the simple
mapping of namespaces into a single URN namespace
URI Segments
- After the domain name, the controller comes the 1st segment of the URI. The second follows the
method. The 3rd, 4th and so on are the parameters. These parameters are mostly required by the method,
it can be used as control, settings or input.
o Example: http://suzuki.com.ph/motors/motorcycles/big-bikes/bandit-650/

  Return String
Code
$this->uri->segment(1) motors
$this->uri->segment(2) motorcycles
$this->uri->segment(3) big-bikes
$this->uri->segment(4) bandit-650

 To see how it works, add the spaghetti method on our Products controller:
application/controlers/Products.php

 $this->uri->segment() method of the URI Class is automatically loaded in CI.

Removing Index.php
- http://localhost/index.php/controller/method/param
- Create the .htaccess file

- Save it to your server’s root directory or your project’s directory

- You can now type in the URL without the index.php! (e.g. http://localhost/controller/method/param)
Routing
- For a quick review, here’s an example of CodeIgniter URL:
o http://www.websitedomain.com/controller/method/param1/param2...
- And also you can do this:
o http://www.websitedomain.com/profile/dustin

- This is how routing works, instead of requesting an address with controller-method-params relationship,
you can request a specific address as alias or redirect (re-route) to a different controller.
- Routing is the process of redirecting or remapping a controller class or method. Defining or creating
your own routing rules are set in the CI’s application/config/routes.php file. Constructing a routing rule
uses wildcards or regular expression.

Examples:
o $route[“bikes”] = ‘motorcycles’;
o A URL that contains ‘bikes’ as controller will be remapped to ‘motorcycles’ controller class
o $route[“profle/dustin”] = ‘profile/view/23’;
o A URL containing the first segment ‘profile’ and second segment ‘dustin’ will be rerouted to
‘profile’ controller, to the view method and to the profile id.
o $route[“bikes/(:any)”] = ‘motorcycles/bike_lookup’;
o A URL that contains any character or word on the second segment will be redirected to
‘motorcycles’ controller class and bike lookup method as it will search the motorcycle database
for any given word or character.
o $route[“bikes/(:num)”] = ‘motorcycles/view/$1’;
- Setting the Default Controller
o $route['default_controller'] = 'welcome';
o By default, its value is the CodeIgniter’s welcome controller class. When a URI segment or
Controller class is not present, CI will load its default controller configuration at
application/config/routes.php file.
404 Page
- Have you ever encountered “Error 404: Page not found”? This happens when you try to reach a
webpage but could not be found on the server. This can be because you typed In the URL incorrectly,
the URL was moved or it does not exist. CodeIgniter allows you to show this client-side error and use it
to catch errors on your code intentionally or not.
- When an Error 404 is met, application/views/errors/html/error_404.php file is shown. Now you know
where to customize this error In CodeIgniter.
- You can display the error 404 page by using the function show_404() in the controller method.

You might also like