Home

- LinuxFocus

image

Contents

1. Get the Facebook token Saccess token S this gt facebook gt sgetAccessToken Loading the fbmodel Sthis gt load gt model fbmodel Updating the token Sthis gt fbmodel gt set_token Saccess_ token Get a Facebook user s profile details Suser profile this gt fbmodel gt get_user_profile Getting the Facebook user ID Suid Suser profile id Retrieving a Facebook user s details me this gt fbmodel sget_me_by fql uid Get a Facebook user s friends Sfriends this gt fbmodel gt get_friends Sview params array me gt me friends gt Sfriends Loading the view Sthis gt load s gt view fbview Sview_params else The Facebook parameters for the Facebook login URL where scope consists the Facebook app s permissions Sa_params array fbconnect gt 0 scope gt offline access publish _stream cookie gt true The Facebook login URL page Slogin_url this gt facebook gt sgetLoginUrl a_params 138 Chapter 6 Redirecting the Facebook user to the login URL After the Facebook user confirms the permissions required by the app he is redirected back to the index page header Location Slogin_url The model file The model PHP file is located at application models fbmodel php The model is responsible for interacting with the Facebook SDK and retrieving the Facebook user s details an
2. if S this sinput gt is AJAX request exit none AJAX calls rejected see http codeigniter com user_guide libraries input html a this gt input gt post a get the posted a parameter b this gt input gt post b get the posted b parameter result int a int b data array result gt result to add more array parameters p2 gt p2 p3 gt p3 echojson_encode data return the JSON encoded array return closing the class definition We call this controller via an HTTP request URL as an HTTP or HTTPS request For example http mydomain com index php my_handler Let us review several usage scenarios with this controller class definition Note that you can enable Codelgniter to operate without the index php file in the path for more information refer to the index php file issue discussed later in this chapter In this section we will review different use cases for the CI controller as well as the naming rules associated with the controller The following are the cases that are mainly used for calling a controller e Directly from a browser e From a view HTML page script using a CI PHP anchor helper embedded in the page e From a view HTML page using a JavaScript jQuery AJAX call embedded in the page e Fromacrontab PHP script using cURL to call a controller The controller has its own naming rules and usage guidelines that we will review now The controller
3. The Cl libraries scope and usage The CI library does not have access to the controller resources by default unless the CI ci amp get_instance is called and ci is used instead of this to access the CI resources for example instead of this gt db gt query sql we shall use ci gt db gt query sql and so on We can extend the CI library using the third party libraries from where CI echo system the CI community of developers worldwide share knowledge sources and solutions for code and open issues or develop our own libraries from scratch or extending other libraries Any application library will be located under application libraries in the project s directory In addition optional resources such as the library configuration file that is required for library configurations can be placed under the project root or elsewhere A good practice is to place them under the project root for enhanced security provided by CI For example lt PROJECT_ROOT gt application config lt LIB_ NAME gt config php or even additional resources such as the images CSS HTML additional class libraries may be required under another application lt LIB_ ADDITIONAL RESOURCES gt such as application assets The library integration and the usage within the CI project are as follows e Adding the library code resources to application libraries my_lib php optionally adding related resources if any such as a library configuration file and o
4. else login failed ret result notok inform the login form to alert user for the failure Sret msg Invalid User Pass Try Again return ret logout method called auth logout function logout destroy the current session Sthis gt session gt sess destroy redirect auth functionadmin_main_menu Shall render an admin main menu page Sview_setup uid this gt session suserdata user_id Sview_setup user_name Sthis gt session gt userdata user name Sview_setup role S this gt session gt userdata role Sview_setup menu Add User Modify User Delete User Sthis gt load sview logged_in view php S view_setup functionuser_ main _menu Shall render a regular user Sview_setup uid this gt session suserdata user_id 58 Chapter 3 Sview_setup user_name this gt session gt userdata user name Sview_setup role this gt session suserdata role Sview_setup menu View Content Modify Your Account Logout Sthis gt load sview logged_in view php S view_setup The model file The model file application models users model php will serve the controller for authenticating user credentials against the user table If successful the model will return the user database row to the caller auth admin_main_menu or auth user_main_menu will be called as per the mod
5. Generates headers that force a download to happen access public param string fullPath return void function download_large files fullPath File Exists if file exists fullPath Parse Info Get Extension Sfsize filesize SfullPath Spath parts pathinfo fullPath ext strtolower S path_parts extension Determine Content Type switch Sext case pdf Sctype application pdf break 115 Helpers case exe Sctype application octet stream break case zip ctype application zip break case doc ctype application msword break case xls ctyp application vnd ms excel break case ppt ctype application vnd ms powerpoint break case wmv Sctype video x ms wmv break case gif Sctype image gif break case png ctype image png break case jpeg case jpg ctype image jpg break default ctype application force download Sfile handle fopen fullPath rb header Content Description File Transfer header Content Type Sctype header Content Length fsize header Content Disposition attachment filename basename fullPath while feof file handle Sbuffer fread file handle 1 1024 1024 echo Sbuffer 116 Chapter 5 ob_flush flush These two flush commands seem t
6. Programming with Codelgniter MVC Build feature rich web applications using the Codelgniter MVC framework PACKT Programming with Codelgniter MVC Build feature rich web applications using the Codelgniter MVC framework Eli Orr Yehuda Zadik PACKT PUBLISHING BIRMINGHAM MUMBAI Programming with Codelgniter MVC Copyright 2013 Packt Publishing All rights reserved No part of this book may be reproduced stored in a retrieval system or transmitted in any form or by any means without the prior written permission of the publisher except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However the information contained in this book is sold without warranty either express or implied Neither the authors nor Packt Publishing and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However Packt Publishing cannot guarantee the accuracy of this information First published September 2013 Production Reference 1160913 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB UK ISBN 978 1 84969 470 4 www pac
7. default t Ea tE default en t a defaul defaul defaul defaul defaul defaul defaul password database dbdriver dbprefix pconnect db debug cache_on cachedir char_set dbcollat mypass1 mydatabasel mysql mae TRUE TRUE TRUE my utf 8 ut 8 general_ci By setting the parameters for another database entry named dbentry2 we shall define the following parameters db db db db db db db db db db db db dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 dbentry2 hostname username password database dbdriver dbprefix pconnect db debug cache_on cachedir char_set dbcollat localhost mySecondDB mypass2 mySecondDB mysql we TRUE TRUE TRUE waea utfi8 utf 8 general _ci There is no need to connect and load the default database as it is done automatically when loading the database class however the call is Sthis gt load gt database Or for referring to a specific database entry name it is Sthis gt load gt database default In order to connect and load the dbent ry2 database settings stated earlier use the following code Sthis gt dbentry2 this
8. public function edit Sproduct_id Loading the url helper Sthis gt load gt helper url Manually loading the database Sthis gt load gt database Loading the model class Sthis gt load gt model productmodel Sa_post_ values Sthis sinput gt spost Checking if a form was submitted if a_post_values a_form values a_post_values else Get the values of the database Sa_db values this gt productmodel sget_ product product_id 128 Chapter 6 a_form_values array product_id gt Sa_db values 0 gt product_id product_name gt a_db values 0 gt product_name product_sku gt a_db values 0 gt product_sku product _quantity gt S a_db values 0 gt product_quantity Sview_params this gt load_form product edit product_id Sa_form_values Validating the form if this gt form_validation gt run FALSE Validation failed Sthis gt load sview productform view_params else a_fields array product_name product_sku product _quantity for Sindex 0 S index lt count a fields S index Ss_ field a fields S index Sdata s field S this gt input gt post s_ field Sthis gt productmodel supdateProduct S product_id data redirect product End of file product php Location application controllers product php The model file The model
9. MySQL database USERS DB sql This is a database table that we will upload to our database 55 Controller Usage and Scope Let us assume the URI to the project root is http mydomain com myproject Hence the URI to execute the auth controller for login will be http mydomain com myproject auth The controller file The controller file controller auth php will initially load the CI form helper this helper will be used to construct and operate the login form For more on helper usage and scope refer to Chapter 5 Helpers users_model written especially to serve the controller for authenticating users credentials against the user table will be loaded The controller auth index will be called from both the initial stage as well as after a login_view submission The session is a well known issue in PHP and is out of the scope of this book However CI enables the storing of operated sessions with served clients via the database in a table named ci_sessions This way the sessions are much more organized for the project to manipulate with search session and load session parameter In order to use a database stored session we shall edit config config php config sess use database TRUE Enforce storing sessions data in the database Also we will add a session library as we want to use it for this example along with other commonly used config autoload php libraries Sautoload libraries array
10. Returning array of one object element containing the product details return S query gt result This method adds a product to the products table parameters data The data to insert into the table public function addProduct data Calling the CI s db object method for inserting a product data into the products table Sthis gt db gt insert products data This method updates a product row in the products table parameters product_id The product id 134 Chapter 6 data The updated data public function updateProduct product_id data Calling the CI s db object s methods for generating the SQL queries Sthis gt db gt where product_id product_id Calling the CI s db object method for updating the product data in the products table this gt db supdate products data This method checks whether the quantity exceeds it s limit private function check quantity product_id Calling the CI s db object s methods for generating the SQL queries Sthis gt sdb gt select product_quantity this gt db gt from products Sthis gt db gt where product_id product_id Calling the CI s db object method for executing the query query this gt db gt get Calling the result s method row which returns the SQL query result row row query gt row if row gt product_ quantity lt 7 return
11. So json json_decode s_ json Processing data received from the LinkedIn library Sa_connections array j for Sindex 0 Sindex lt o json gt count index if o_json gt values S index gt id lt private gt continue if isset o_json gt values S index gt pictureUr1 Spicture url o_json svalues S index spictureUrl else Spicture_url lt gt 99 Libraries S a_connections array lt picture_url gt gt Spicture_ url lt name gt gt o_ json gt values S index gt firstName 0 _json gt values Sindex gt lastName lt headline gt gt 0 _json gt values Sindex gt headline lt industry gt gt o_json gt values Sindex gt industry lt url gt gt o_json gt values Sindex gt siteStandardProfileRequest gt surl Sview_params lt connections_count gt 0 _json gt _total Sview_params lt connections gt a_connections Load the view for displaying the LinkedIn user gt s connections Sthis gt load sview lt linked connections gt view_params This function the calls library method me to get the LinkedIn company gt s details param Scompany_id integer Linkedin company id public function companies company_ id Get the LinkedIn company gt s details s json this gt linkedin gt company company_id o_company details json_decode s_json Sa_company details array lt
12. gt lt td gt td gt lt php echo product gt product_sku gt lt td gt td gt lt php echo product gt product_ quantity gt lt td gt td gt lt a href lt php echo site _url product edit Sproduct gt product_id gt gt Edit Product lt a gt lt td gt lt tr gt lt php endforeach gt lt table gt NAN NAN lt p gt lt a href lt php echo site _url product add gt gt Add Product lt a gt lt p gt lt body gt lt html gt 131 Models Example 2 a business logic example In this example we will demonstrate business logic Ordering a product will trigger the model to update the product s quantity and check whether it s smaller than a certain amount This example will be constructed from the following controllers model and view e application controllers order php This controller loads the model productmodel e S this gt load gt model productmodel This controller renders the view orderview which displays all the products and where each product has links to ordering a product e application models productmodel php This model contains functions which retrieve products updates its quantity and checks its quantity e application views orderview php The view displays all the products in a table where each row has a link for ordering the product Let us assume the URI to the project root is http mydomain com myproject and http mydomain com myp
13. public function index Loading the form helper Sthis gt load gt helper form Loading the form validation library Sthis gt load gt library form_validation Sview_params form attributes array id gt myform contact name details Sview_params form contact_name label array text gt Your name for gt name Sview_params form contact_name field array name gt contact_name id gt contact_name value gt isset POST contact_name POST contact_name maxlength gt 100 size gt 30 class gt input contact name details Sview_params form contact_email label array text gt Your email for gt email Sview_params form contact_email field array name gt contact_email id gt contact_email value gt isset POST contact_email POST contact_email maxlength gt 100 size gt 30 class gt input contact message details Sview_params form contact_message label array text gt Your message for gt message Sview_params form contact_message field array name gt contact_message id gt contact_message value gt isset POST contact_message POST contact_message rows gt 10 cols gt 100 class gt input Setting validation r
14. return this gt soauth_consumer gt getRequestToken S request_token_url S this scallback_url This method returns the LinkedIn authorize URL param Soauth_token string oauth token for the LinkedIn authorzation URL return string URL of the LinkedIn authorization URL public function generateAuthorizeUrl Soauth_token return this gt authorize path oauth_token Soauth_token This method sets the token and secret keys of 92 Chapter 4 the oauth object of the oauth protocol param Soauth_ token string oauth token param Soauth_token_secret oauth_token_secret public function setToken Soauth_token oauth_token_secret this gt oauth_consumer gt setToken Soauth_token Soauth_token_secret This method requests the LinkedIn tokens for accessing the LinkedIn resources It returns an array with the following keys oauth_token oauth_token_secret param Soauth verifier string return array Array with the following keys oauth_token oauth_token_secret which are used to access the LinkedIn resources URL public function getAccessToken Soauth_ verifier t ry Returns an array with the following keys oauth_token oauth_token_secret These keys are used to access the LinkedIn resources URL return this gt oauth_consumer gt getAccessToken this gt saccess token path
15. 53 Controller Usage and Scope Define the To Email s Sthis gt email s gt to array Name 1 lt namel name com gt Name 2 lt name2 name com gt Set the e mail subject Sthis gt email gt subject This is the Subject can be ANY UTF 8 chars Define the e mail body in HTML format as we set the message to be HTML typed Sthis gt email gt message lt H1 gt Hello there lt H1 gt lt p gt This Email is sent from CI via its cool e mail library lt BR gt lt font color green size 3 gt lt b gt See Attached Files lt b gt lt font gt lt BR gt Attachedfiles lt BR gt lt ul gt lt li gt 1 File One lt li gt lt li gt 2 Second File lt li gt lt p gt i Load the attachments path this gt doc_root_path Doc root For example home yourdomain com public html Let say attachment under public html as attachments Sattachment_pathl S path attachments filel jpg Sattachment_path2 S path attachments file2 php Set the two attachment file paths Sthis gt email gt attach Sattachment_pathl1 Sthis gt email gt attach Sattachment_path2 We have the e mail object ready Let us send it execute send and check the result status if Sthis gt email gt send The e mail was sent successfully echo Your email was sent else We had some problems let s show what was wrong echo this gt email gt print_ debugger funct
16. Library lt title gt lt head gt lt body gt lt div id results gt lt All The Formatted Table is rendered by the table library instance using the controller defined settings and the table of users we have fetched from the DB gt lt php echo this gt table gt generate Susers gt lt div gt lt body gt lt html gt Example 2 using third party libraries such as the Google Maps Cl library wrapper In this example we will see how to install and use the Google Maps CI library with some cool services First we need to download the library files from http biostall com codeigniter google maps v3 api library In the downloaded TAR file we shall find the following libraries e Googlemaps php This is the Google Maps API library for CI We shall place it at application libraries e Jsmin php This is an auxiliary code for the library to generate the JavaScript generated code for enabling the smart Google Maps UI interaction We shall also place it at application libraries e Google Maps V3 API This is a PDF file for in depth possible library settings and usage 71 Libraries In this example we will provide an initial page showing several marked places together on the Google Map window that we will create in our application In that visualized view we will enable the user to zoom into predefined selected places we have marked on the map using the CI anchor URL helper This example wi
17. adding the index php file to the URI after receiving the URI request from the browser and before executing it The result is that the user who is browsing will not see it but it will call the desired resource properly in a similar way to how we used index php The language setting is done as follows Sconfig language english It is recommended that you leave this as default Note that even if we use other languages such as Arabic or Hebrew it will be fine We just make sure that our PHP files are saved as UTF 8 without BOM byte order mark is a unicode character that marks the file encoding method supporting multilanguage schemes for more information refer to http en wikipedia org wiki Byte_order_mark to inform the browser that receives the rendered HTML page to process it as a UTF 8 file The exact meaning of this tag is out of the scope of this book and can be learned from HTML standard Sconfig charset UTF 8 Additionally it is highly recommended for multi language support to add the following line in our view file s HTML header lt meta http equiv Content Type content text html charset utf 8 gt These settings inform the browser to process the rendered HTML page whose characters are encoded as UTF 8 which is the most common multilanguage standard for non English languages such as Hebrew Arabic and Chinese Do not touch these settings it is very useful to support multiple languages Sconfig enab
18. although elaboration on this can be found at http php net The following is a simple example of how a public method calls a private method to get some data public function get something Ssome_data this gt _get_it private function get it return hello 45 Controller Usage and Scope Using loaded resources The loaded resources can be used after loading as follows Using loaded model methods Sstatus Sthis gt some_model ssave_data table row Srows Sthis gt some_model sget_table table name Using loaded library methods Sanother data S this gt some_library gt method some_data Using loaded helpers Syour_ip get_your_ip myhtml_helper function NOTE a helper defines regular functions Calling a controller o The controller is automatically instantiated by the CI core and its methods are called via HTTP URIs For more information refer to Chapter 2 Configurations and Naming Conventions Ways to call a controller Calling only the contractor and later calling the index method if defined as follows URI base_url mycontroller mycontroller Calling the method of the mycontroller class without parameters SURI base_url mycontroller mymethod Calling the method of the mycontroller class with two parameters aand b SURI base _url mycontroller mymethod a b Cl controller use cases There are several different use
19. and customizing today s systems applications and frameworks Our solution based books give you the knowledge and power to customize the software and technologies you re using to get the job done Packt books are more specific and less general than the IT books you have seen in the past Our unique business model allows us to bring you more focused information giving you more of what you need to know and less of what you don t Packt is a modern yet unique publishing company which focuses on producing quality cutting edge books for communities of developers administrators and newbies alike For more information please visit our website www packtpub com Writing for Packt We welcome all inquiries from people who are interested in authoring Book proposals should be sent to author packtpub com If your book idea is still at an early stage and you would like to discuss it first before writing a formal book proposal contact us one of our commissioning editors will get in touch with you We re not just looking for published authors if you have strong technical skills but no writing experience our experienced editors can help you develop a writing career or simply get some additional reward for your expertise PACKT PUBLISHING Codelgniter 1 7 Professional Development Adam Griffiths Codelgniter 1 7 Professional Development ISBN 978 1 84951 090 5 Paperback 300 pages Become a Codelgniter expert with professional tool
20. as the following e Your classes functions and parameters should have short names and if constructed from several words they should use the underscore separator as follows several lowercase words naming with under score get_file properties e When defining a string value in case the string does not have a parameter to evaluate we shall use a single comma as follows Smy_string the string e Incase we want our string to have a parameter such as name we write our string using double quotes name big string parameter Smy_ string This is a name e The Boolean and contacts all should be in upper case Sthis vale FALSE While in javascript we shall use true false to distinguish For more general CI PHP style guide refer to http codeigniter com user_ guide general styleguide html Summary In this chapter we have reviewed and practiced CI naming conventions rules and usage with a set of examples for parameters classes controllers models helpers libraries and views Following the naming conventions we have reviewed CI configurations We reviewed the major configuration files such as config php database php autoload php and routes php in depth We also reviewed how we can use the configuration parameters while configuring several databases to be used in our project In addition to it we also saw how we can add more project specific configurations 39 Controller Usage
21. as well Integrating jQuery or other client side libraries CI provides the freedom to integrate any client side libraries so that CI does not have to be specially configured to or we do not need to perform any special CI declarations The client side integration is performed in the same fashion as if no platform is being used they are completely transparent However CI provides client side jQuery code generation services via PHP such as building the JavaScript library to create jQuery code as part of the controller coding S this gt load gt library javascript 152 Chapter 7 However for the latest jQuery and many other JavaScript based solutions today there s no need to use this fashion of rendered JavaScript portions but we can use the JavaScript A Z in the view itself instead A great resource for the wow level JavaScript libraries can be found in the largest resource for the JavaScript libraries we ve found so far at http www jsdb io Many more cool links for client side platforms can be found at https delicious com eliorr1961 Note that the directory path for the sources in the CI views is calculated as if the view file is in the project root For example let s say the JavaScript library is located at lt Project_root gt javascript myjs js And the view is located either at the lt Project_root gt application views viewl1 php or lt Project_root gt application views topicB view2 php v
22. callback URL parameter is the URL to return to from the LinkedIn Authorization URL where the LinkedIn user shall confirm the LinkedIn app s required permission The LinkedIn server responds and returns the oauth token public key and the ouath token secret key Client gt Server request token URL parameter callback URL lt Server returns oauth token ouath token secret 2 The client sends the request to the LinkedIn server auth URL using the oauth_ token token received from https api linkedin com uas oauth authorize oauth_token oauth_token where oauth_token is the oauth token returned from the server at phase 1 Client gt Server auth URL GET parameter oauth token 86 Chapter 4 The LinkedIn server returns the oauth token the oauth token secret and the cauth verifier to the client Client lt Server oauth token oauth token secret oauth verifier The client sends the request to the LinkedIn Server access token path at https api linkedin com uas oauth accessToken Client gt Server access token path parameter oauth_ verifier from phase 2 lt Server returns oauth token ouath token secret This example will be constructed from the following controller library and view application controllers linkedinfo php The controller that uses the LinkedIn library for authentication and displaying the output returned by the library application libraries linkedin handeler php The linkedin handler
23. cases for a CI user defined controller Commonly the CI controller will handle initiating rendering HTML pages enable us to let the user navigate and view the different web application pages we defined However the controller may also provide other services such as AJAX server side controller serving asynchronously the client side browser requests and commonly return back the JSON formatted data instead of a rendered view The exact scope and usage of AJAX is not part of the CI framework but it is very useful standard de facto technology To learn more about AJAX please refer to AJAX Asynchronous JavaScript and XML on Wikipedia http en wikipedia org 46 Chapter 3 The main usage categories for our controller in a CI framework are as follows Rendering views These type of controllers mostly performs some preparations for data and render the requested view along with the prepared data to be displayed to the user for the next user session state with web application A special case is the home page view rendering So that the user refers to the Project root directory via URI such as http mydomain com my_ci_project Where the CI routes will define maincont roller as the home page or default controller as follows under the project root application config routes php So that the default controller will be defined as follows Sroute default_controller maincontroller Then the call to http mydomain
24. code Define the address we want to be on the map center Sconfig center London UK to be on the map center Set Zoom Level Zoom 0 World 18 Street Level Sconfig zoom 16 S this gt googlemaps gt initialize config Add visual flag Sthis gt add_visual_flag Sconfig center j data this gt load_map_ setting Load our view passing the map data that has just been created 73 Libraries Sthis gt load sview google map _view data functionBombay Initialize our map Here you can also pass in additional parameters for customizing the map see the following code Define the address we want to see as the map center Sconfig center Bombay India Sconfig zoom 16 City Level Zoom S this gt googlemaps gt initialize config Add visual flag Sthis gt add_visual_flag Sconfig center data this gt load_map_ setting Load our view passing the map data that has just been created Sthis gt load sview google map _view data The class Gmaps continues with several more functions as follows function rehovot Initialize our map Here you can also pass in additional parameters for customizing the map see the following code Sconfig center Rehovot Israel Sconfig zoom 16 City Level Zoom Sthis gt googlemaps gt initialize config Add visual flag Sthis gt add_visu
25. contains the required details return fqlResult This method returns an array of a Facebook user s friend public function get friends Getting the CI main class to get access to the Facebook library Sci amp get_instance Getting the Facebook user s friends Sfriends ci gt facebook gt api me friends Returning an array which contains a Facebook user s friend return S friends The view file The view PHP file is located at application views fbview php This view file displays a Facebook user s details and a table with their friend details The code and inline explanations are as follows lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt 140 Chapter 6 lt titlesMy facebook details lt title gt lt head gt lt body gt lt div id my details gt lt div id picture gt lt img src lt me 0 pic_big gt gt lt div gt lt div id my_name gt lt S me 0 name gt lt div gt lt div gt lt table gt lt tES lt th gt Name lt th gt lt thsLink to friend lt ths lt tr gt lt php foreach friends data as friend gt lt tr gt lt td gt lt Sfriend name gt lt tds gt lt td gt lt a href http www facebook com lt Sfriend id gt gt To friend lt a gt lt td gt lt tr gt lt php endforeach gt lt table gt lt body gt lt html gt Summary In this c
26. database session xmlrpc In case of a submission the input post for the password will not be null and the controller will proceed with the credentials checkup using the users_model model If successful the user record fields will be kept in the session and the controller methods auth admin_main_menu or auth user_main_menu will be called as per the model returned user role If the logged in user issues the logout anchor auth logout will be called to destroy the session and redirect the user to the login form The following is the code class Auth extends CI Controller function _construct parent construct Sthis gt load gt helper form Sthis gt load gt model users_model 56 Chapter 3 called with auth is called with no specific method and simply calls the login method function index Sthis gt login functionlogin The message to user in case of login failure smsg n if S this gt input gt post password The caller is from the form submission we will check credentials validity using the local method check_login Sstat this scheck_login Extract failure message to user if any msg stat msg if S stat result ok Successful login See what We have admin_user or regular user if this gt session suserdata role admin_user Issue the controller for admin user main menu redire
27. directory tree especially the application folder which is the heart and soul of any CI project In the next chapter we will review the configurations such as database and naming conventions that are essential for the CI project 20 Configurations and Naming Conventions This chapter initially introduces the CI naming conventions These conventions include the rules style guide and Codelgniter naming spirit The second part of this chapter will review CI project configurations for built in resources as well as user defined or third party add on libraries Note that we will actually build our own project code in the subdirectory application described in Chapter 1 Getting Started with optionally relative resource directories for our project s self made resources such as CSS Media jQuery libraries resources or third party add ons extending the base CI downloaded from the Ellis Labs site or GitHub We should remember that developing a CI project is done by replacing expanding the default provided controllers views models and other resources in a well defined OOP fashion We should extend controllers models and add additional views as well as use defined helpers or libraries We can add these from third party libraries or helpers or develop new ones for our special project business logic and needs The initial step after installing the CI is making the proper configurations for our project requirements such as database
28. executes requests to the models and views The controller mediates between the input mostly received from a user interacting with a web browser that executed the rendered view The browser runs a received rendered view by the controller through an HTTP protocol The controller is the heart of the application It performs model database updates business logic calculations renders views to the user and responds to an asynchronous AJAX Asynchronous JavaScript and XML request sent from the client side The view code defines the presentation and user input logic to be rendered by the controller as HTML and JavaScript to the browser The browser receives the rendered view via the HTTP response to be executed locally The browser executing that content can present data such as a mix of text charts diagrams and images There are legacy CMS Content Management System web development platforms focused on CMS functionality and maintenance such as a mature platform named DRUPAL It might be very useful for content oriented projects but less appealing if the project aims to develop a new rich set of functionality that is web apps with many inputs and customized UI operations If the project s requirements involve a low footprint and fast response high performance Codelgniter is found to have excellent results To sum up in terms of flexibility code reusability light infrastructure enabling developer creativity code clarity highest performanc
29. false else return true This method updates a product quantity and return true or false if quantity reaches it s limit public function update quantity product_id sql UPDATE products SET product quantity product quantity 1 WHERE product_id product_id Sthis gt db gt query sql Checking if the quantity reached it s limit if this gt check quantity product_id return true else return false 135 Models The view file The PHP view file is located at application views orderview php This view file displays a table with the products list The following are the code and inline explanations lt DOCTYPE html gt html lang en gt head gt meta charset utf 8 gt title gt Products List lt title gt head gt body gt table gt N AN A AN AN ANA ANN tr gt lt th gt ID lt th gt lt th gt Name lt th gt lt th gt SKU lt th gt lt th gt Quantity lt th gt lt thsActions lt ths lt tr gt lt php foreach Sproducts as product gt fa Oe ee td gt lt php echo product gt product_id gt lt td gt td gt lt php echo product gt product_name gt lt td gt td gt lt php echo product gt product_sku gt lt td gt td gt lt php echo product gt product_ quantity gt lt td gt NN AN AN AN td gt lt a href lt php echo site url order product Sproduct gt product_id gt gt Order
30. fetch Sapi_url S params method headers Receiving the last response with json containing the user s connections s_json S this soauth_consumer gt getLastResponse return s_json catch OAuthException E echo lt pre gt var_dump this gt oauth_consumer echo lt pre gt lt br gt lt br gt 94 Chapter 4 echo Response SE gt lastResponse exit This function returns a LinkedIn company details It returns a JSON string containing these values param Integer company_id company id return json string String containing a company details z public function company company id Sparams array Sheaders array method OAUTH_HTTP METHOD GET Sapi_url this gt api_url companies Scompany_id The following company s details are required company_id number of employees foundation year number of the company s followers Sapi_url id name website url twitter id employee count range specialties founded year num followers format json try Request for a LinkedIn company s details Sthis gt oauth_consumer gt fetch Sapi_url S params method headers Receiving the last response with json containing the company s details s_json this soauth_consumer gt getLastResponse return s_json catch OAuthException E echo lt pre gt var_dump this gt soauth_consumer echo lt p
31. file as follows Sparaml valuel We can easily access our configuration file parameters to load all the parameters into the array Sarray S this gt config gt load my_config TRUE The second parameter TRUE assures us that our configuration parameters will be defined in an array prefixed with the configuration file name Consider paraml Sarray my_config param1 Or Sparam1l S this gt config sitem paraml my_config param1 will have the value value1 that we have set in the configuration file that we built 29 Configurations and Naming Conventions Understanding and using Cl naming conventions The CI naming conventions are essential to understand and use in order to properly develop with CI They enable you to write minimal code using a strict and concise set of rules The full CI naming conventions and style guide can be found at http codeigniter com user_guide general styleguide html The naming conventions refer to the naming of parameters functions methods class related PHP file name storing code project resource paths and so on Here are the specific issues we will review e Extending CI resources such as the CI controller or model in our project resources class extends class fashion for example extending CI_ controller or CI_mode1 see the examples discussed in the Controller definition naming rules section e Defining views and rendering them by a controller wi
32. gt input gt get cauth_problem redirect S this son_failure url Set the oauth consumer tokens Sthis gt setToken this gt CI gt input gt get oauth_token Sthis gt CI gt session gt suserdata oauth_token_secret Sending request to the LinkedIn access _token_path to receive the array which it s keys are tokens oauth_token oauth_token_secret for accessing the LinkedIn resources Saccess_token_reponse this gt getAccessToken this gt CI sinput gt sget oauth_verifier 91 Libraries Setting the session variables with the tokens oauth_token oauth_token_secret for accessing the LinkedIn resources Sthis gt CI gt session gt set_userdata oauth_token Saccess_token_reponse oauth_token Sthis gt CI gt session gt set_userdata oauth_token_secret Saccess_token_reponse oauth_token_secret Redirecting to the main page redirect This method sends the request token to LinkedIn return array keys oauth_token oauth_token_secret ah public function getRequestToken The LinkedIn request token url Srequest_token_url this gt request_token_url The LinkedIn app permissions Srequest_token_url 2scope r_basicprofile r_emailaddress r_ network Getting the response from the LinkedIn request token URL The method returns the response which is an array with the following keys oauth_token oauth_token_secret
33. gt load gt database dbentry2 TRUE To use the default database with the database class library db use Sql To use the dbentry2 database use q2 this gt db gt query this gt dbentry2 gt db gt query select from mytable select from DB2table 28 Chapter 2 routes php Define the default controller that will be executed when referred via the URI to the base_url of the project let s say http mydomain com myapp so that myapp is a subdirectory of public_html in the sever and we have home_page_cont roller Sroute default_controller home_page controller When the user issues http mydomain com myapp due to the route configuration for the home controller the URI that CI will issue will be as if the user is referring to http mydomain com myapp home_page_controller Sroute 404 override In the preceding example the default application errors error_404 php page will be executed in case the user refers to a non existing project controller such as http mydomain com myapp sadfasdfsdfsdi We may decide for example to pop up a message for the non existing page and route to the default URI to minimize user inconvenience Defining and using your own configurations CI enables us to define our own configurations and easily access them via the config class For example with application config my_config php let s say we define a parameter in that config
34. id gt gt company_id lt name gt gt o_ company details sname lt specialties gt gt S o_ company details gt specialties svalues lt websiteUrl gt gt So_ company details swebsiteUrl lt employeeCountRange gt gt S o_ company details gt employeeCountRange sname lt foundedYear gt gt o company details gt foundedYear lt numFollowers gt gt o_company details gt numFollowers Load the view for displaying the LinkedIn company gt s details Sview_params Sa_company details Sthis gt load gt view lt linked company gt view_params This function calls the library method me to get the LinkedIn company gt s updates 100 Chapter 4 param S company_id integer Linkedin company id public function company updates company id Get the LinkedIn company gt s updates s json Sthis gt linkedin gt company_updates company_id So_company updates json_decode s_json Processing the data received from the LinkedIn library Sa_updates array Sa_json_updates o company _updates gt values for Sindex 0 S index lt count a_json updates Sindex So_ update a_json updates index if isset 0 _update gt updateContent gt companyJobUpdate Sa_updates array lt type gt gt lt Job Update gt lt position gt gt o_update supdateContent gt companyJobUpdate gt job gt position stitle lt url gt gt S o_update gt
35. is most commonly called from the view using an anchor tag However it may also be called using AJAX or even a crontab PHP script using a PHP function file or CURL based request 32 Chapter 2 Example 1 calling the controller index method Controllers are mostly called via a user interaction session on a rendered view processed by a client browser The controller method is called to issue another process such as and AJAX request or processing the request and rendering it back to the client browser additional view or web page To define the controller call within a view definition application views we define an anchor to be executed by the browser per user request Note that in these examples we use another URL helper named anchor anchor Suri text Shtml_attributes Note that in order to use the CI anchor helper function we will initially load the helper URL via config autoload php Sautoload helper array url Another way to do this is to load the anchor helper in the controller rendering the view where we want to use an anchor Sthis gt load gt helper url e URI The URI path to call a controller or any URI we want to execute e text The anchor label shown to the user to click on e html_attributes Any HTML attributes that can be defined for an HTML anchor element An example of the resulting HTML that will be executed by the client side browser is as follows lt a href myapp
36. is set as follows default exten gt s 1 Dial Zap 1 30 exten gt s 2 Voicemail ul100 exten gt s 102 Voicemail b100 exten gt i 1 Voicemail s0 When we wish to draw your attention to a particular part of a code block the relevant lines or items are set in bold default exten gt s 1 Dial Zap 1 30 exten gt s 2 Voicemail ul100 exten gt s 102 Voicemail b100 exten gt i 1 Voicemail s0 Any command line input or output is written as follows cp usr src asterisk addons configs cdr mysql conf sample etc asterisk cdr mysql conf New terms and important words are shown in bold Words that you see on the screen in menus or dialog boxes for example appear in the text like this clicking the Next button moves you to the next screen GA Warnings or important notes appear in a box like this Q Tips and tricks appear like this Reader feedback Feedback from our readers is always welcome Let us know what you think about this book what you liked or may have disliked Reader feedback is important for us to develop titles that you really get the most out of To send us general feedback simply send an e mail to feedback packtpub com and mention the book title through the subject of your message 4 Preface If there is a topic that you have expertise in and you are interested in either writing or contributing to a book see our author guide on www packtpub com authors C
37. mail product product_id reached it s limit Order product product_id j redirect product The model file The model PHP file is located at application models productmodel php In this example the methods of the CI object db are called for generating and executing the SQL queries Please refer to the CI database s library available at http ellislab com codeigniter user guide database index html 133 Models The code and inline explanations are as follows lt php class Productmodel extends CI_ Model The model s constructor method public function _ construct Call the model constructor parent construct This method retrieves the products list and returns an array of objects each containing product details public function get _products Calling the CI s db object s method for generating the SQL queries query this gt db gt get products returns an array of products objects return S query gt result This method retrieves a specific product s details identified by product_id as a parameter public function get _product product_id Calling the CI s db object s methods for generating the SQL queries Sthis gt db gt select Sthis gt db gt from products Sthis gt db gt where product_id Sproduct_id Calling the CI s db object method for executing the query query S this gt db gt get
38. oauth verifier catch OAuthException E echo lt pre gt var_dump this soauth_consumer echo lt pre gt lt br gt lt br gt echo Response SE gt lastResponse exit This function returns a LinkedIn user s details It returns a JSON string containing these values return json string String containing user s details public function me Sparams array 93 Libraries Sheaders array method OAUTH_HTTP METHOD GET Sapi_url this gt api_url people format json try Request for a LinkedIn user s details Sthis gt oauth_consumer gt fetch Sapi_url S params method headers Receiving the last response with json containing the user s details s_json this soauth_consumer gt getLastResponse return s_json catch OAuthException E echo lt pre gt var_dump this gt soauth_consumer echo lt pre gt lt br gt lt br gt echo Response SE gt lastResponse exit T This function returns a LinkedIn user s connections It returns a JSON string containing these values return json string String containing user s connections public function connections params array Sheaders array method OAUTH_HTTP METHOD GET Sapi_url this gt api_url connections count 10 amp format json try Request for a LinkedIn user s connections Sthis gt oauth_consumer gt
39. opens a new Tab gt lt div gt lt div style clear both gt lt div gt lt HR gt lt HR gt lt php echo anchor home_page page_b Navigate me to page B gt lt body gt lt html gt 50 Chapter 3 The controller has a page_b method to render the following view file It will prepare the parameters since and past for this page to be used inline in the rendered view page_b view as follows The view file is views page_b view php lt DOCTYPE html gt lt meta content text html charset utf 8 gt lt PHP data from controller since past gt lt body style text align left color blue gt lt H1 gt Page B lt H1 gt lt HR gt lt HR gt lt div style float left gt lt We render the provided controller parameters since amp Spast gt Since lt since gt past lt Spast gt years lt div gt lt div style clear both gt lt div gt lt HR gt lt HR gt lt php echo anchor home_page Back to Home Page gt lt body gt lt html gt The configuration file Initially we shall define at application config routes php the default controller to be called For example Sroute default_controller home_page So that in case you will issue URI of the project root in the browser lets say http mydomain com myproject http mydomain com myproject home_page will be called Optionally we can configure CI to eliminate
40. product to the products table Parameters data The data to insert into the table public function addProduct data Calling the CI s db object method for inserting a product data into the products table Sthis gt db gt insert products data This method updates a product row in the products table parameters product_id The product id data The updated data public function updateProduct S product_id data Calling the CI s db object s methods for generating SQL queries Sthis gt db gt where product_id Sproduct_id 130 Chapter 6 Calling the CI s db object method for updating the product data in the products table this gt db supdate products data The view file The view PHP file is located at application views productsview php This view file displays a table with the products list The following are the code and inline explanations lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Products List lt title gt lt head gt lt body gt lt table gt lt tr gt td gt ID lt td gt lt td gt Name lt td gt lt td gt SKU lt td gt lt td gt Quantity lt td gt lt E A td gt Actions lt td gt lt php foreach products as product gt lt tr gt td gt lt php echo product gt product_id gt lt td gt td gt lt php echo product gt product_name
41. project Initially the requested URL is processed via the routes based on the routes configuration conf routes php For more information refer to Chapter 2 Configurations and Naming Conventions The specific CI controller is instantiated and the specific method is called The CI method may be assisted by any of the project resources such as models libraries and helpers for operations business logic and database queries The CI controller generally uses a view that defines a web page to be responded to via an HTTP response 43 Controller Usage and Scope The user defined CI controller Each CI project must have one or more user defined controllers in order to operate The user defined controllers are the starting point of any CI user interaction Calling the controller and its methods can be done in several ways The controller can be called via project root URI submission to a browser the project default controller will be called by issuing the user anchor from a rendered view by a client side AJAX request for actions updating page selectors or even by a crontab Linux known scheduler service scheduled action executed repeatedly as a URI of a certain controller method We can see that the controller scope is a general manager of all the other project resources such as models views helpers and libraries governing all to address execution requests from the user or a scheduled request Any application controller
42. session auto loaded helpers and the libraries we want The CI has a set of configuration files defined in the project directory located at application config These configurations are loaded initially whenever we execute any of our project s CI controllers via a URI call using a browser or issuing an HTTP request via code The major configuration files are config php database php autoload php and routes php We should review each of the major configuration files with its configuration value which includes recommended value and possible values Configurations and Naming Conventions Cl directory tree The following is the classic directory tree structure of Codelgniter 2 J CLPROJECT_ROOT 4 B application B cache B config Name E htaccess E activate_user php W controllers j E index htmi Wi core I errors I helpers Ii hooks B language B libraries B logs Db models B third_party B views mY system B user_guide B logistics Eil test php B welcome php m Date modified 23 05 2013 03 46 24 05 2013 20 52 23 05 2013 03 46 24 05 2013 21 37 23 05 2013 03 46 Type HTACCESS File PHP File Firefox HTML Doc PHP File PHP File Note that when we add new plugins and other resources such as bootstrap new directories of resources may be added with a name of your choice so that you can refer to them from the specific resource you are developing using the CI
43. such as rows of a query result A standard GET parameters URI looks like this http ourdomain com index php example2 more a 1 amp b 2 amp c 3 However let s remember that in CI the URI is passed in this manner http ourdomain com index php example2 more 1 2 3 For more information see Chapter 2 Configurations and Naming Conventions 12 Chapter 1 Looking at the URI we will build the controller example2 php with the function named more with the three parameters passed to it We will build the following two scripts e The controller class application controllers example2 php e The view script application views example2more php The controller file The controller is responsible for rendering the view with parameters such as mega title and message The following is the code sample of the controller lt php class Example2 extends CI Controller This function gets parameters and passes them to the view example2more The example url nttp ourdomain com index php example2 more 1 2 3 so a 1 b 2 c 3 public function more a b c The parameters in view params are extracted in the view example2more php In this example 2 variables will be generated by CI in the view page example2more php variable mega_title value Codeigniter Passing url parameters to view variable Srows value array a gt a b gt b c gt c Srows array a gt a
44. template swrite view header header_view Sdata Sthis gt template gt write view upper _navigation upper_ navigation view data Sthis gt template swrite view content content_view data Sthis gt template swrite view footer footer_view data Now the template regions are rendered with their region view files We can render the whole template with all its regions as follows Now we have all the regions rendered into the template instance buffer we can render them all to the desired template base page Sthis gt template gt render 155 Views The point to remember is that templates have great pros but also some cons The template dictates a very strict way of rendering a template base page that does not always have the desired flexibility so that we may find ourselves writing several templates and switch between them according to the UI situation Example 1 HTML5 location powered by Google Maps In this example we will expand the Google Maps integration example from Chapter 4 Libraries so that there will be a new option of showing the user where they are located on the Google Map For doing so we will use the HTML5 navigator geolocation service to request the browsing user to share its location with the application If the user agrees and is using an advanced browser such as the latest Firefox Chrome builds that support to this service Once we get the values
45. the controller index method 33 Example 2 calling the controller and calc method without arguments 34 Example 3 calling the controller and calc method with arguments 34 Example 4 calling AJAX to an AJAX oriented method with arguments 36 Loading libraries models and helpers 37 Miscellaneous naming conventions 39 Summary 39 Chapter 3 Controller Usage and Scope 41 Scope of the Cl controller 43 The user defined Cl controller 44 Extending the Cl controller 45 Cl controller use cases 46 Example 1 the default homepage controller 49 The controller file 49 The view file 50 The configuration file 51 Example 2 sending e mails with attachments 52 The controller file 52 Example 3 admin and regular user log in 55 The controller file 56 The model file 59 The database file to upload for this example 60 The login_view view file 60 The login_in_view view file 62 Summary 63 Chapter 4 Libraries 65 The Cl libraries scope and usage 67 Available Cl libraries 68 Example 1 using the built in libraries 69 The controller file 70 The view file 71 Example 2 using third party libraries such as the Google Maps Cl library wrapper 71 The controller file 72 The view file 1S Example 3 building a library such as the Flickr API wrapper 76 The flickr_wrapper php library file 77 The flickr_recent php controller file 82 The flickr_recent_view php view file 84 ii Table of Contents Example 4 the LinkedIn API w
46. the need to use index php as part of the URI path to call our CI project controller s Refer for information to Chapter 2 Configurations and Naming Conventions 51 Controller Usage and Scope Example 2 sending e mails with attachments In this example we will see how the controller can load a mail service library and use it to send mail attachments The CI mail library is not an auto loaded library and hence will be loaded by the controller we are building for sending e mail The CI mail library makes it easy to send subject messages of non English languages supporting UTF 8 both for the subject and the mail body Adding attachments to an e mail becomes a piece of cake using the CI mail library We only need to have the files on a known directory path in our server and refer to them to attach them to the mail We can attach one or more files to create the mail body HTML TEXT is defined via a simple configuration setting to the CI mail library This example will be constructed from the mail controller only you may add a rendered view later on to add to the example report on the mailing list sending a report of a list of e mail destinations instead of just one or two destinations Let us specify the default controller filename as controller mail Let us assume the URI to the project root is http mydomain com myproject Hence the URI to execute the controller for sending the mail will be http mydomain com
47. usage Several examples for defining the models extending the CI model and using them will be provided Chapter 7 Views explains the CI views concept as the generators for the client side visualization and user interaction provided via HTTP The view of the PHP part and scope visual content HTML CSS and program JavaScript AJAX jQuery for the client browser will be reviewed The view s scope definition and controller rendering guidelines with practical practice and tips and tricks will be covered in this chapter Appendix Appendix References refers to recommended external resources related to Codelgniter s formal resources as well as the ECHO system of the developer s community What you need for this book In order to understand this book s content the user is required to at least have PHP programming language experience with some PHP OOP object oriented programming and MySQL knowledge Who this book is for This book is for PHP web application developers who are interested in developing applications using OOP MVC concepts and specifically the CodeIgniter platform Conventions In this book you will find a number of styles of text that distinguish between different kinds of information Here are some examples of these styles and an explanation of their meaning 3 Preface Code words in text are shown as follows We can include other contexts through the use of the include directive A block of code
48. view shows the logged in user some information about its account such as the user name and role as well as the menu available for its user category The view is located at application views login_in_view php This view is using parameters provided by the suser_nam Suid role and menu controller to be shown to the logged in user From this view the user may issue a logout anchor that calls auth logout to destroy the session and redirect the logged in user to the login view 62 Chapter 3 Many of the CI form helper functions are loaded by the auth controller When user issues are submitted the input is initially checked at the client side before issuing a submission call to application auth The following is the code lt DOCTYPE html gt lt meta http equiv Content type content text html charset lt DOCTYPE html gt lt meta http equiv Content type content text html charset utf 8 gt lt html gt lt body gt lt Hl gt Welcome lt user_name gt lt H1 gt lt H1 gt You are logged in lt H1 gt lt HR gt lt HR gt lt H3 gt Your User ID is lt Suid gt lt H3 gt lt H3 gt Your System Role is lt role gt lt H3 gt lt H3 gt Your Menu options lt Smenu gt lt H3 gt lt php echo anchor auth logout Logout gt lt body gt lt html gt Summary In this chapter we have reviewed the CI controller scope and the different controller us
49. 8 Chapter 1 Folders overview The root folder contains the index php file which handles all the URI requests The index php file will process them with the CI core and apply our application controllers using the models libraries and helpers loaded by the controllers and rendered views license txt which is the Cl s license file htaccess is used for configuring the CI routing and removing index php from the URL JavaScript CSS and HTML is incorporated into the rendered PHP output and their usage is elaborated in Chapter 7 Views Let s review the folders and their content application The application directory folder is the root directory of our main activity project coding zone This is the heart of the Cl developed application project Mandatory components Let s take a look at the mandatory components e application config This folder contains all the CI application configuration files which are covered in Chapter 2 Configurations and Naming Conventions e application controllers This folder contains all the application controllers in the CI application project A controller as mentioned in the Preface is a component in the MVC design architecture that handles the request by the user and presents the data shown to the user A controller in CI is a class extending a base class of the CI controller The class methods can be executed or called with a proper URI The naming conventions related to the controller defini
50. 96 controller index method calling 33 Cross Site Request Forgery CSRE XSRF 27 CRUD Create Read Update and Delete 122 CRUD example about 125 controller file 126 model file 129 172 view file 131 cURL Client URLs 48 Cyber Ears URL 170 D data retrieving from Facebook 136 database php file 27 28 database query example about 14 controller file 15 model file 16 view file 16 DataMapper Codelgniter ORM library 123 default homepage controller example about 49 controller file 49 view files 50 51 DirectAdmin 48 Doctrine ORM 123 E e mail sending with attachments 52 F Facebook data retrieving from 136 Facebook data retrieval example about 136 controller file 137 model file 139 view file 140 flexibility CI view scope 147 Flickr API wrapper building 76 Flickr API wrapper example about 76 controller file flickr_recent php 82 83 library file flickr_wrapper php 77 82 view file flickr_recent_view php 85 flickr com 76 flickr_recent php file 82 flickr_wrapper php file 77 folders overview 9 functions CI form helper form_checkbox 149 form_close 149 form_dropdown 149 form_input 149 form_multiselect 149 form_open 149 form_password 149 form_radio 149 form_textarea 149 form_upload 149 G get_instance 108 Git community URL 68 Google Maps CI library about 71 installing 71 Google Maps integration example controller PHP file 156 expanding 156 view PHP file 159 Go
51. BASEPATH defined parameter as the directory path to the CI_PROJECT_ROOT directory If we add a new directory under the project root let s say bootstrap the path for including resources such as CSS JavaScript or images for example hello png will be path BASEPATH bootstrap hello png config php The CI main configuration files have the following major configurations config base_url The default is an empty string so that CI can calculate the base URL of our project root directory We shall refer to the base URL in many places in our code mostly to execute controllers To get the base URL we should call base_url base_url defined in the URL helper mentioned before 22 Chapter 2 The base_url function in the URL helper function returns the URI string to the CI project base For example if the CI project is developed on a domain named example com under a public_html directory named mydev and we have a controller named find a method named stock and a directory named myprod we can call the find or stock method in both the myprod and mydev projects using base_url url base_url index php find stock In the mydev project we will get Surl http example com mydev index php find stock In the myprod project we will get url http example com myprod index php find stock Hence in order to call a controller class named my_class we use SURL base_url
52. It is highly recommended to leave this as FALSE as CI provides a solution to pass parameters within URI as shown in the calc example at the beginning of this chapter The log threshold for the severity level is such that any event that is of the same or higher severity level will be logged to CI The supported threshold levels and their meanings are as follows e 0 Disables logging error logging turned off e 1 Error messages including PHP errors e 2 Debug messages e 3 Informational messages e 4 All messages config log threshold 4 4 is the highest level for all CI events from notice level events and worse 25 Configurations and Naming Conventions The preceding configuration will generate error logs according to the log_threshold level at application 1logs if the error log was enabled Note that enabling the errors log will cause performance reduction in our web application Use it only if you must for debugging needs Otherwise set it to 0 Sconfig log path The default log file path in the CI project is application logs Do not touch this configuration unless you have a clear reason The date time format Sconfig log date format Y m d H i s The default date time format setting is 2012 06 18 14 54 11 It is recommended to not touch this configuration The cache file path Sconfig cache path The default is application cache It is recommended not to touch thi
53. O VALUE ON_ZERO SET time_zone 00 00 Table structure for table users CREATE TABLE IF NOT EXISTS users id int 11 NOT NULL AUTO INCREMENT user _name varchar 128 NOT NULL password varchar 128 NOT NULL role varchar 128 NOT NULL PRIMARY KEY id ENGINE MyISAM DEFAULT CHARSET utf8 AUTO INCREMENT 3 Dumping data for table users INSERT INTO users id user_name password role VALUES 1 reg_user bbb8aae57c104cda40c93843ad5e6db8 regular_user 2 admin_user 0d777e9e30b918e9034ab610712c90c admin_user The login_view view file The login_view view is rendered by the application auth index index method in order to show non logged in web visitors to a login page to enable to challenge them with a login stage Following a user entering the user name and password and submitting the login_view view form the application auth login will be called and will check the credentials using the users model In case of a successful login and based on the logged in user category fetch from the users model one of the auth methods will be called as follows 60 Chapter 3 e auth admin main menu In case the user has the admin role to render the successful login view for the admin user e auth user main menu In case the user has the admin role to render the successful login view for the regular user The view is located at application views
54. PHP file is located at application models productmodel php In this example the methods of the CI object db are called for generating and executing the SQL queries Please refer to the CI database library at http ellislab com codeigniter user guide database index html The following are the code and inline explanations lt php class Productmodel extends CI_Model The model s constructor method public function __ construct 129 Models Call the Model s parent constructor parent construct This method retrieves the products list and returns an array of objects each containing product details public function get_products Calling the CI s db object s method for generating SQL queries query this gt db gt get products returns an array of products objects return S query gt result This method retrieves a specific product s details identified by product_id as a parameter public function get product S product_id Calling the CI s db object s methods for generating SQL queries Sthis gt db gt select Sthis gt db gt from products Sthis gt db gt where product_id Sproduct_id Calling the CI s db object method for executing the query query S this gt db gt get Returning array of one object element containing product details return S query gt result This method adds a
55. Product lt a gt lt td gt lt tr gt lt php endforeach gt lt table gt lt body gt lt html gt Example 3 retrieving data from Facebook In this example we will use the CI built in model to retrieve data from Facebook The example displays a Facebook user name and picture and displays the user s Facebook friends 136 Chapter 6 This example uses Facebook PHP SDK as a CI library It can be downloaded from https github com facebook php sdk For more information refer to Chapter 4 Libraries This example will be constructed from the following controllers model and view e application controllers fbpage php This controller loads the model fbmodel e Sthis gt load gt model fbmodel This controller renders the view fbview which displays the user s Facebook picture and name and table which contains the user s friends names and links to their profiles e application models fbmodel php This model contains functions that retrieve data from Facebook e application views fbview php This view displays Facebook data Let us assume the URI to the project root is http mydomain com myproject and http mydomain com myproject fbpage The source code is provided with this book via URLs The controller file The controller PHP file is located at application controllers fbpage php The controller is responsible for getting the access token from Facebook and redirecting t
56. Sparams Sencoded_params array foreach Sparams as k gt v Sencoded_params urlencode k urlencode v call the API and decode the response HW url http api flickr com services rest implode amp Sencoded_params This will create get query URI paraml vall amp param2 val2 and so on 81 Libraries Srsp Sthis gt _file_get_contents_ curl url return rsp obj unserialize rsp The class code continues as follows This function assure we can get a url content into a buffer it requires that a PHP curl library is installed private function file get contents curl url if function_exists curl_init exit PHP curl library is not enabled please fix Sch curl init curl_setopt ch CURLOPT HEADER 0 curl_setopt ch CURLOPT_RETURNTRANSFER 1 curl_setopt ch CURLOPT_URL url data curl_exec ch curl close ch return data private function _is filtered photo photo_rec o width gt 4416 latitude gt 0 More can be added Photo width shall be larger than S this gt DEFAULT_RES if int photo_rec o width lt int Sthis gt DEFAULT RES return FALSE GPS info required amp Found if this gt GPS ENABLED amp amp S photo rec latitude return FALSE if we are here the filtered photo passed successfully return TRUE The
57. T PHOTOS gt 50 Latest 100 photo uploads i Sthis gt flickr wrapper gt set_params Ssettings Sphotos_to filter 83 Libraries Sthis gt flickr wrapper gt flickrPhotosGetRecent j filter_ photos Sthis gt flickr_wrapper gt filter_ photos photos_to_ filter data Array Sdata photos filter photos Sdata settings settings Sthis gt load sview flickr recent _view php Sdata The flickr_recent_view php view file The flickr_recent_view php view file is rendered by our controller named Flickr_recent defined previously This controller uses our developed flickr _wrapper library in order to get the recent Flickr uploaded photos with their associated information The view file is located at application views flickr recent view php This view uses the CI parser for the PHP inserted parameters using the lt Sparam gt notation The following is the code lt DOCTYPE html gt lt html gt lt head gt lt meta charset UTF 8 gt lt div gt lt H1 gt Flickr Recent Uploads lt H1 gt lt p gt lt Show the applied filter settings first gt lt table border 1 style background color b0c4de gt lt tr gt lt td gt Photos in Poll lt td gt lt td gt lt settings RECENT PHOTOS gt lt td gt lt tr gt lt tr gt lt td gt Min Width Filter lt td gt lt td gt lt settings DEFAULT_RES gt Px lt td gt lt tr gt lt tr gt l
58. V gt lt button id user_ideas style cursor pointer position relative top 0px title Share your feedback ideas gt Add A New Feedback lt button gt lt BR gt lt button id feedback_ log style cursor pointer position relative top 0px title Your feedback log gt See Your Feedback Log lt button gt lt DIV gt lt div id feedback_log_view style display none width 800 px border style solid border color black overflow x auto height 200 px overflow y auto gt lt DIV gt lt H2 gt lt php echo anchor index php auth logout Logout gt lt H2 gt lt div id ideas form title Your Feedback To Improve gt lt form gt lt fieldset gt lt span id user name class text ui widget content ui corner all gt Thanks lt user name gt Please share your feedback with us lt span gt lt textarea name idea_desc id user feedback rows 10 cols 83 placeholder Your ideas gt lt textarea gt lt fieldset gt lt form gt lt div gt lt body gt lt html gt 167 Views Summary In this chapter we have reviewed the CI views scope as well as their general MVC scope and the different types of views and usage In addition we showed how to integrate our Codelgniter code with the third party template plugin the CI library configuration and additional code assets for providing the view template services to the application controllers
59. We have also learned the examples of integrating the jQuery UI and AJAX in the CI view with the CI controllers models 168 Appendix In this appendix we will provide a set of proactive updates and web resources regarding the Codelgniter community and its featured Cl based sites CI formal resources are as follows CI developer Ellis Labs http ellislab com codeigniter CI forms for different topics http ellislab com forums CI developer online chat http ellislab com codeigniter irc CI user guide http ellislab com codeigniter user guide CI developments of the community GitHub https github com EllisLab CodeIgniter CI downloads center http ellislab com codeigniter user guide installation downloads html Featured CI plugins are as follows CI sparks plugin library http getsparks org packages browse latest ClI based CMS PyroCMS https www pyrocms com CI grid plugin http www grocerycrud com CI GoCart e commerce plugin http gocartdv com CI PayPal Library https www x com devzone articles paypal library for php codeigniter framework Codelgniter Google Maps V3 API Library http biostall com codeigniter google maps v3 api library Facebook PHP SDK and Codelgniter http www dannyherran com 2011 02 facebook php sdk and codeigniter for basic user authentication Appendix Sites an Agile toolkit for CodeIgniter developers http agiletoolkit org blog agil
60. _key otherwise exit if isset Sparams api_key exit FATAL must be constructed with api_key Sthis gt set_params Sparams Just for debugging needs we may drop those later error reporting E ALL ini_set display_errors 1 change settings on the fly function set_params key array sets array of instance parameters foreach key _array as key gt val switch key case DEFAULT_RES Sthis gt DEFAULT_RES val break case GPS ENABLED S this gt GPS ENABLED val break case RECENT PHOTOS this gt RECENT PHOTOS val break case api_key this gt api_key S val break We can add many more here default exit FATAL set_params invalid param key The class code continues while shifting our focus on accessing the recent public photos Pulls recent public photos as multi dimensional array function flickrPhotosGetRecent 78 Chapter 4 build the Params for API Sparams array api_key gt Sthis gt api_key method gt flickr photos getRecent extras gt o dims owner_name date taken media path_alias url_sq geo per page gt this gt RECENT PHOTOS format gt php serial rsp_obj this gt flickrRestAPI params check if ok or successful result if rsp_obj stat ok Get the array of all the photo records in this cycle return Srecent_ photos rsp_o
61. able at https github com ianckc CodeIgniter Google Maps Library Many more third party libraries can be found following the CI forums at http codeigniter com forums Example 1 using the built in libraries In this initial example we will see how to use the CI built in library Here we will use the CI library CI_Table as well as the CI_db library which for a given database table view and some optional CSS settings will enable us to render the table nicely with all the HTML table tags and CSS settings in just a single line of code In this example we will use the same user s table that we used for the controller example in Chapter 3 Usage and Scope of Controllers This example will be constructed from the following controller and view e application controllers builtins php This controller loads the built in CI library table as well as the db library which is autoloaded for more information refer to Chapter 2 Configurations and Naming Conventions to get the user s table content and set up the table to render using the table library this gt load gt library table 69 Libraries The controllers prepare vectors of map settings and the list of places and possible controllers to zoom into each of the places and render a view named google map view e application views users_view php This view will use the table library service to render a nicely formatted table as loaded from db and configured
62. age categories targeting view rendering and serve AJAX client requests or apache crontab scheduled processing requests We have reviewed the various resources that the controller can be assisted with such as the helpers libraries and models Eventually we have made several usage examples as follows e Example 1 default home page controller e Example 2 sending e mails with attachments e Example 3 admin and regular user log in 63 Libraries This chapter covers the CI libraries topic and the different types of libraries and their different usage categories with several code examples of web applications The CI development platform provides us with the built in libraries enables us with an easy procedure to integrate third party libraries and also allows us to develop our new libraries and share them with the community if we wish to The CI libraries are powering efficiency code reusability separation and simplicity The benefits achieved are as follows Efficiency In means of minimal loaded resources This feature achieved by the fact that the CI library may be loaded only by the specific CI project controller s or even only in specific method s where the library s services are required Hence the overhead memory of the library resources during execution time is minimized in each controller operation state Reusability Reusability means writing once a function code and reusing it across the project resour
63. al S this gt input gt post param1 For example within the class code we issue an AJAX call to a function as shown in the next example Example 4 calling AJAX to an AJAX oriented method with arguments In this example we enable the end user to issue an AJAX call to a specific class method with its parameters enabled via HTTP post lt script src https AJAX googleapis com AJAX libs jquery 1 8 1 jJquery min js gt lt script gt lt script type text javascript gt function AJAX call aval name a val b val name b val AJAX url lt PHP echo base_url index php my_handler AJAX calc gt AJAX type POST url AJAX _ url data a aval b b val dataType json success function data result html data result AJAX Call end lt script gt lt form onsubmit AJAX_call gt lt label gt Enter A lt label gt lt input type text name a gt lt label gt Enter B lt label gt lt input type text name b gt Result lt div id result gt The Result Will Be Shown Here lt div gt lt input type submit value Calculate gt lt form gt 36 Chapter 2 Enter two numeric values for A and B and click on the Calculate button We will get the integer casted as A and multiply it by the integer casted as B in the div section with id result Loading libraries models and helpers To reuse other librar
64. al_flag Sconfig center data this gt load_map_ setting Load our view passing the map data that has just been created Sthis gt load gt view google map view data function load_map_ setting data array Slocations array j Scontrollers array Set controllers list for zoom in Slocations London UK Slocations Bombai India Slocations Rehovot Israel Set controllers list for zoom in Scontrollers london 74 Chapter 4 Scontrollers bombai Scontrollers rehovot Sdata map this gt sgooglemaps gt create_map Sdata locations S locations S data controllers S controllers Sdata map this gt googlemaps gt create_map return data The class Gmaps continues with several more functions as follows function add_visual_flag place Smarker array Setup Marker for the place and the title as the place name Smarker position S place Smarker title place Sthis gt googlemaps gt add_marker marker The view file The view file will render the provided Google Maps JavaScript and HTML portions as well as render the list of places It also provides zoom in and zoom out navigation options to the places supported by the controller lt DOCTYPE html gt lt meta http equiv Content type content text html charset utf 8 gt lt html gt lt head gt lt script src
65. ame_sel phone sel err _sel SSN_val the value of the SSN user typed in to search name_sel the name input that the Ajax will update if SSN found phone_sel the phone selector to be update if SSN found as well err_sel the error message area to explain error such as SSN not found or some other error occurred varajax_url lt php echo base _url gt ajax get_person_info ajax type POST Very important POST is the best url ajax_url the URI of the AJAX server side controller method that will 150 Chapter 7 processes this request DE data SSN SSN_val SSN is the parameter name SSN val is the value dataType json the retuned data expected to be JSON success function data the data is the array conversion of the JSON data retuned to ease our usage in the JavaScript if data result found Let s show the name and phone of the person with the given SSN name_sel val data name phone_sel val data phone else SSN Not found in the database Let s notify SSN has no person match S err_sel css color red j err_sel text No person found with SSN SSN val error Ajax error occurred such as Ajax server not found and so on function msg alert Error msg S err_sel css color red S err_sel text Error msg Wait for the document to be ready and bind the user click
66. an that the customized validation on the CRUD operation is enabled as well Using an ORM plugin may reduce the need to self develop our own CI models so that only special business logic activities are left to be implemented Today ORM plugins provide predefined validation services as well as user defined services to enforce validations on CRUD requests from the application controllers libraries or helpers requesting the database CRUD services 122 Chapter 6 There are pros and cons of using ORM On one hand it simplifies a lot of the database model development for the database On there other hand it dictates various rules on the database scheme definition such as defining user tables for an ORM object user or defining the auto increment primary key field name such as ID and so on There are several ORM plugins for CI the most well known and well documented ones with a large network of community developers are the following e Doctrine ORM docs doctrine project org This ORM plugin with the well documented CI integration guidelines is available at http docs doctrine project org en 2 0 x cookbook integrating with codeigniter html e DataMapper CodeIgniter ORM library datamapper wanwizard eu It provides the CI library such as user guide web navigator e Both ORM libraries provide more than just the table based CRUD services but can be configured to handle the cross table relationships of the for
67. and Scope This chapter covers the CI controller scope and the various controller usage categories with several code examples of web applications The controllers are the front line decision makers of how to process or route a request and how to respond to actions such as a rendered view sending back to the browser as an HTML page an AJAX response to let the current requesting page update certain selector areas by the response or even just to update the database seamlessly The controllers use the available models helpers libraries and views to respond to the caller be it a web browser URL or a cron process issuing the CURL types POST GET requests automatically The CI built in controller behaves like an abstract class in the project scope so any of our project controllers must be extensions of a built in CI controller Our developed controller will inherit the CI built in controller capabilities and built in resources such as auto loaded helpers libraries and models and let us code any specific services as controller methods to address our project requirements and rendering the needs of the view As mentioned before the controller is part of the MVC development framework that operates with models and applies business logic which most commonly renders a view back to the client web browser to proceed with user interaction The web user refers via a browser to a URL In CI the view is implemented as a call to a CI controller metho
68. ation database objects Generally this includes the database operations but implementing the same operations and invoking external web services or APIs is not unusual at all e Encapsulating the application logic For example the model can make data validations before storing a data object and can alert the calling application module about the problem The most common misuse of the CI database class is using it directly from the controller view or helper A good practice is to develop the model classes to handle all the application database services Hence all the other application modules can benefit and reuse those models The CI models are special classes designed to handle databases or external information resources such as Facebook we will see an example of this in this chapter The CI models are the PHP classes that are designed to work with information in the database Models This chapter will primarily focus on the following topics e The CI model scope fo The model resource path o Loading a model Using model methods o Connecting to a database o Business logic e Object Relational Mapping ORM e Example 1 a CRUD example e Example 2 a business logic example e Example 3 retrieving data from Facebook We will begin by briefly reviewing the CI model scope and will proceed with several usage examples covering different use cases that are combined in a real project Scope of
69. ation helpers ssl_ helper php The helper integration and usage within the CI project is as follows e Add the helper code resources to application helpers myhelper_ helper php e Load the helper automatically or via the controller Automatically load a helper myhelper for all CI projects as follows Sautoload helper array url myhelper For loading in certain specific controllers constructors or methods use the following this gt load gt helper myhelper e Use the following helper methods result this gt myhelper gt called method paraml aram2 108 Chapter 5 Available Cl helpers CI and the CI developers community network provide many helpers covering a rich set of topics We will review CI helpers as well as popular resources for third party CI helpers We are also encouraged to build our own helpers which can be used by others and share them with the following communities e The Git community https github com e Cl forums http codeigniter com forums Cl system helpers The list of CI built in helpers is as follows they can be found in the CI Directory Tree by going to system helpers e Array Helper e CAPTCHA Helper e Cookie Helper e Date Helper e Directory Helper e Download Helper e Email Helper e File Helper e Form Helper e HTML Helper e Inflector Helper e Language Helper e Number Helper e Path Helper e Security Helper e Smiley Helper
70. b gt b c gt c Sview_ params array mega_title gt Codeigniter Passing url parameters to view rows gt S rows this gt load gt view example2more view params closing the class definition End of file welcome php 13 Getting Started The view file The following is the corresponding rendered view lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt lt php echo mega_title gt lt title gt lt head gt lt body gt lt table gt lt tr gt lt td gt Key lt td gt lt tdsValue lt td gt lt tr gt lt php foreach Srows as key gt value gt lt tr gt td gt lt php echo key gt lt td gt td gt lt php echo value gt lt td gt lt tr gt lt php endforeach gt lt table gt lt body gt lt html gt A A Example 3 the database query by a model rendering results to a view In this example we will show you how the CI controller uses the CI model to retrieve data from the database and render it to a CI view The URL will be http ourdomain com index php user First we will have to configure the database settings in the configuration file application config database php We should keep the default database settings unchanged and only change the following configuration parameters db default hostname 127 0 0 1 In many cases when the hostname s value
71. bj photos photo else Query failed we shall return NULL to the caller return NULL Get the Photo EXIF that has a lot of info related to the photo for a given photo id The class code continues where we will see how to access additional information related to the image function GetPhotoExif photo_id build the API URL to call Sparams array api_key gt Sthis sapi_key method gt flickr photos getExif photo_id gt photo_id format gt php serial rsp_obj this gt _flickrRestAPI S params display the photo title or an error if it failed if rsp_obj stat ok Array photo gt Array id gt 8002716747 79 Libraries secret gt 559f87aea0 server gt 8030 farm gt 9 camera gt Casio EX H20G exif gt A LOT OF EXTRA INFO Sphoto_ camera rsp_obj photo camera We can add more interesting items for our app here Sparams array camera gt Sphoto_camera full_exif gt rsp_obj All EXIF info for the photo_id return params else Request Failed We shall return error return NULL Let s see how we can apply photo filtering with the following code apply photos filtering on a provided photos array based on the current settings function filter photos photos Sfiltered photos array j foreach S photos as photo if t
72. by the controller __ Let us assume that the URL to the project root is http mydomain Ge com myproject http mydomain com myproject builtins The source code is provided with this book via URLs The controller file The following is a step by step example of the controller code for each operation lt php Use CI built In libraries class Builtins extends CI_Controller function _ construct parent construct Load the table library that generates the HTML tags for showing the table structure within a view Sthis gt load gt library table public function index Load the users list from DB into the view Sdata users this gt db gt get users Create custom header for the table Sheader array id User Name Hashed Password Position Set the headings Sthis gt table gt set_heading S header Set table formatting Stable format array table open gt lt table border 1 cellpadding 2 cellspacing 1 class mytable gt Sthis gt table gt set_template table format Load the view and send the results Sthis gt load sview users view data 70 Chapter 4 The view file To complete the operation we will finish working on the view file lt DOCTYPE html gt lt meta http equiv Content type content text html charset utf 8 gt lt html gt lt head gt lt title gt Showing Users Table Using CI Build In table
73. cation and ask the controller to render a Google Map for it var latitude 0 var longitude 0 functionshow on map var DIRECTORY SEPARATOR Prepare the URL path of calling the Gmaps controller method user_location with latitude and longitude coordinates as parameters using the CI naming convention of ControllerName methodName Param1 Param2 Var url_to_show lt php echo base _url gt index php gmaps user location longitude DIRECTORY SEPARATOR latitude Use jQuery to issue the HTTP controller call and rendering request location attr href url_to_ show document ready function if user clicks on the lt li gt for getting its Geolocation getmylocation click checkLocation functioncheckLocation Check if the browser supports the HTML5 Geolocation Note that navigator geolocation will pop a request from the user to allow getting its location Privacy if navigator geolocation It does so let the user be notified notifications html fetching your location wait 159 Views notifications css color blue Try to fetch the latitude longitude of the browsing user and provides the callbacks Success getLocation Failure locationFail navigator geolocation getCurrentPosition getLocation locationFail else notifications html Sorry your browser settings does not e
74. ces The libraries can be loaded by any project controller model or helper in a helper we shall use the sget_instance method discussed several times before to reuse their code anywhere in the CI project More than that the controller rendered views can call those loaded library methods Hence great code reusability is achieved Separation Separation prevents accidental overlapping with same name to the parameters or functions elsewhere in the project The Library class methods and parameters have their own name space so that they can t be overridden by a mistake outside the library in case the developer is using the same parameters in the served module such as controllers views Simplicity This make the code text as minimal as possible and easy to understand and maintain The libraries methods called from the served resources such as controllers models and helpers make the code look much simpler and make it easy to maintain and navigate Hence this simplifies extending the code and maintaining it Libraries The libraries give us development power and efficiency with rich focused functionality on certain project aspects and also enable us to have simple and concise fashion code in the served controllers by calling the library method instead of having the service code locally in the controller The libraries should be initially instantiated by the code using them such as the controller model or helper or if used by almos
75. com my_ci_project will be routed by the CI routes to http mydomain com my_ci_project maincontroller For user navigation request to another page we shall have in the rendered view HTML anchors for navigating into another pages something like the following view code lt PHP echo anchor site _url pages_controller page b Navigate me to page B gt Controller serving the browser AJAX requests These controllers respond to the AJAX client requests and most commonly return the JSON data to the calling jQuery script as follows lt script type text javascript gt function autocomplete clue val varurl lt php echo site _url gt AJAX_controller autocomplete name SAJAX type POST url url data clue clue val dataType json success function data show the data of matching names return D 47 Controller Usage and Scope Controller serving Linux scheduled cron requests A very powerful CI usage we found is serving Linux cron schedule requests defined in the Linux cron for more information about Linux cron scheduling please refer to http en wikipedia org wiki Cron We can find within the DirectAdmin apache admin tool a UI editor to define the scheduled crontab repeatable actions we want the server to perform For each request we will define the PHP processor path for example user local bin php as well as the PHP script to be executed for example ho
76. contact forms By reviewing these examples we will get the basics of using CI resources We will begin by briefly reviewing the CI resources used Then we will review a web application code that loads a static view page Next we will use the model to retrieve data from a database and show it in a view Finally we ll add a view with a contact form to enter input and save it by calling a controller method into the database Getting Started Installing Codelgniter First of all we need to have a hosted PHP server Version 5 3 or later and a MySQL one of the latest versions server where we know the database credentials Local database access from the PHP is recommended for simplicity Note that the server will operate in a CGI Common Gateway Interface fashion in order to let CI operate We can have a local web development environment on our PC or a remote server hosted and dedicated Once we ve set up a local web development environment we ll need to download the latest version of CI which is Version 2 1 2 at the time of writing this book The link to download the latest version is http codeigniter com downloads Now if we look inside the CI folder we should see the following directory tree codeigniter index php application cache config controllers core errors helpers hooks language libraries logs models third_party views system core database fonts helpers language libraries
77. ct auth admin_main_menu else Issue regular user main menu controller redirect auth user_main_menu return else rendered with no submission let s destroy any previous session and challenge again the user Sthis gt session gt sess destroy We can get here due to login failure or referring to auth controller without any active submission Keep the msg return from the model into view view_setup msg msg render the login view to challenge the user Sthis gt load sview login view php view_setup 57 Controller Usage and Scope functioncheck login Extract the credentials from the submitted login viewform Suser name S this gt input gt post user_name Spassword S this gt input gt post password init an array to return Sret array Check if login is ok and get the row using the loaded users model model Suser record this susers model gt check login Suser_name password if user_record User passed credentials checkup successfully We have the user record Let s use it to extract info for the logged session buildup Sthis gt session gt set_userdata user_id Suser_record gt id Sthis gt session gt set_userdata user_name Suser_record gt user_name Sthis gt session gt set_userdata role Suser_record gt role Sret result ok Sret msg Logged in
78. d The CI controller method processes the browser request and sends back a rendered view that becomes a visualized web page to the browsing user Controller Usage and Scope The web page received from the CI controller is referred to as the rendered controller view it may include anchors and buttons for the user to continue the interaction with the controller When the user clicks on an anchor in the browser a call to a controller is made the anchor makes an HTTP request to activate a controller call In case the user issue a event such as Button clicking operate scroll bars and so on so that this user event will trigger an action using jQuery to activate for example a jQuery callback function The jQuery callback function may issue a call to another rendering controller such as location attr href controller_url_to_call Another option is to activate an asynchronous AJAX calls to an AJAX controller Such a controller function handles the AJAX request that we will discuss later on When AJAX calls returned from the server their returned data may be used by client side JavaScript to update certain page regions HTML selector s of the web page for example when typing a search string and issuing a search button the AJAX response will provide the search results to be visualized in the same web page or will move to another page More on controllers in general can be found at http en wikipedia org wiki Model view controll
79. d friend lists The model uses the Facebook FQL mechanism For more information about Facebook API usage and development please refer to the Facebook developer page available at http developers facebook com The code and inline explanations are as follows lt php class fbmodel extends CI Model The Facebook app s token private token public function __construct Call the model constructor parent construct This method sets the model class s private token value public function set_token token Sthis gt token token This method returns an array which contains the Facebook user profile public function get_user profile Getting the CI main class to get access to the Facebook library ci amp get_instance Getting the Facebook user s profile Suser profile ci gt facebook gt api me 139 Models return Suser profile This method returns an array which contains a Facebook user s details public function get_me by fql Suid Getting the CI main class to get access to the Facebook library Sci amp get_instance The SQL query to send to Facebook fql SELECT uid name pic_big FROM user WHERE uid Suid Sparam array method gt fql query query gt fql callback gt Getting the Facebook user s details SfqlResult ci gt facebook gt api param Returning an array which
80. e Sa_values product_name maxlength gt 100 size gt 30 class gt input product sku details Sview_params form product_sku label array text gt Product SKU for gt product_sku j 126 Chapter 6 Sview_params form product_sku field array name gt product_sku id gt product_sku value gt isset a_values product_sku a_values product_sku maxlength gt 100 size gt 30 class gt input product quantity details Sview_params form product_quantity label array text gt Product Quantity for gt product_quantity Sview_params form product_quantity field array name gt product_quantity id gt product_quantity value gt isset a_values product_quantity Sa_values product_quantity maxlength gt 100 size gt 30 class gt input Form attributes validation rules Sconfig_form_rules array array field gt product_name label gt Product Name rules gt trim required array field gt product_sku label gt Product SKU rules gt trim required array field gt product_quantity label gt Product Quantity rules gt trim required integer Sthis gt form_validation gt set_rules config_ form_rules return view_params This controller method retr
81. e lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt My Connections lt title gt lt head gt lt body gt lt hl gt My Total connections lt php echo connections_ count gt lt h1 gt lt div gt lt p gt lt a href lt php echo site_url lt gt gt gt Back to Menu lt a gt lt p gt lt div gt lt table gt lt tr gt lt td gt Picture lt tds gt lt td gt Name lt td gt 102 Chapter 4 lt td gt Headline lt td gt lt td gt Industry lt td gt lt tr gt lt php foreach connections as connection gt Ca She lt td gt lt img src lt php echo connection lt picture_url gt gt gt lt td gt lt td gt lt a href lt php echo S connection lt url gt gt target _blank gt lt php echo connection lt name gt gt lt a gt lt td gt lt td gt lt php echo connection lt headline gt gt lt td gt lt td gt lt php echo S connection lt industry gt gt lt td gt lt tr gt lt php endforeach gt lt table gt lt body gt lt html gt The view file linked company php This view file displays the LinkedIn company s details The following is the view code lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Company lt title gt lt head gt lt body gt lt div gt lt p gt lt a href lt php echo s
82. e minimal footprint and fast learning curve Codelgniter seems to be the best choice Furthermore it is part of a proactive improvement process thanks to the growing developer s community worldwide What this book covers Chapter 1 Getting Started introduces the Codelgniter framework while initially getting started with web based applications Chapter 2 Configurations and Naming Conventions reviews the CI naming convention rules style guide and spirit as well as the mandatory and optional configurations and usage within a CI project with several examples The practice of user defined configurations will be reviewed as well Chapter 3 Controllers reviews the CI controller and the user defined controllers extending the CI controller The CI controller class services role definition usage and scope will be reviewed with several examples to clarify 2 Preface Chapter 4 Libraries reviews the user defined libraries in a CI framework Their services role definition usage and scope will be reviewed with several examples to clarify Several examples for defining libraries and using them will be provided Chapter 5 Helpers introduces you to the CI helpers and user defined helpers reusability value definition rules scope and usage Several examples for defining helpers and using them will be provided Chapter 6 Models covers CI models and user defined models reusability value definition rules scope and
83. e using the PHP REST access method This example will be constructed from the following library controller and view application libraries flickr_wrapper php The is the CI wrapper library that enables smooth Flickr API access via CI This basic services library can be expanded to support the entire Flickr App Garden application controllers flickr recent php This is the controller that uses the lickr_wrapper library that we wrote and pulls the recent public photos uploaded with the EXIF photo info and the photographer related information application views flickr_ recent _view php This is the view that shows the collected information of recent photos and photographers 76 Chapter 4 Let us assume the URI to the project root is http mydomain com myproject Hence the URI to execute the auto controller for logging in will be http mydomain com myproject flickr_ recent The flickr_wrapper php library file The application libraries flickr_ wrapper php library file contains the library flickr _wrapper class library that we are building and using to access the Flickr App Garden API It is mandatory to load this library with a valid Flickr api_key that you can get by following the Flickr App Garden documentation The library will use the PHP REST API access so that we can later expand any of the Flickr API services to be supported with our library Each of the library methods returns a multidimensional keyed a
84. e String Helper e Text Helper e Typography Helper e URL Helper e XML Helper 109 Helpers Cl third party helpers e ssl_helper php e html_manipulator_helper php Example 1 using built in helpers In this example we will see how to use CI build in helpers For this example we will use the URL helper for generating links The URL helper file contains functions that assist in working with URLs We will use the URL helper function site_url which returns the site URL as specified in the config file This example will be constructed from either of the following controllers e application controllers helperexamplel php This controller loads the built in CI helper URL this gt load gt helper url The controller renders a view named helper example1 view e application views helper examplel view php This view will use the URL helper to generate links in the view file Let us assume the URLs to the project root are as follows http mydomain com myproject http mydomain com myproject helperexamplel1 Q The source code is provided with this book via URLs The controller file Now we will see how the controller loads the built in CI URL helper so the view file will be able to use the URL helper function site_ur1 which generates the links class Helperexamplel extends CI_Controller Index Page for this controller Maps to the following URL http example com index php welcome or
85. e are also encouraged to build our own libraries that can be used by others and share them with the community such as e The Git community at https github com e ClISparks at http getsparks org e Cl Forums at http codeigniter com forums e Packagist at https packagist org To call a built in library we shall call for example the built in library named CI_Xxxx as follows this gt load gt library xxxx So that CI_ prefix is not used and instead of the capitalized library name Xxxx we use the lowercase library name xxxx For calling a library function yyyy within the library CI_Xxxx we shall write this gt xxxx gt yyyy The following is a list of built in and commonly useful CI libraries As of version 2 1 4 68 Chapter 4 CI_Benchmark CI_Cache CI_Cache_apce CI_Cache_dummy CI_Cache_file CI_Cache_memcached CI_Calendar CI Cart CI_Config CI_Controller CI_Driver CI_Driver Library CI_ Encrypt CI_Exceptions CI_Form_validation CTP EP CI_Hooks CI_Image_lib CI_ Input CI_Javascript CI_Jquery CI_Lang CI_Loader CI_Log CI_Migration CI_Model CI_ Output CI_ Pagination CI_ Parser CI_Profiler CI Router CI_Security CI_ Session CI_SHA1 CI_Table CI_Trackback CI_Unit_test CI_Upload CI_URI CI_User_ agent CI_Utf8 CI_Xmlrpc CI_Xmlrpcs CI_Zip In this chapter we will provide a usage example for Google Maps third party library wrapper avail
86. e toolkit for codeigniter developer CI Formlgniter easy form generator for Codelgniter http formigniter org CI Wiki libraries plugins for CodeIgniter https github com EllisLab CodeIgniter wiki pages Ajax library wrapper for CodeIgniter https github com EllisLab CodeIgniter wiki AJAX for CodeIgniter d articles supporting Codelgniter are as follows CI built in libraries navigator http apigen juzna cz doc EllisLab CodeIgniter index html PHP assist Codelgniter project hosting http phpassist com Simply Codelgniter http www simplycodeigniter com NetTuts Codelgniter http net tutsplus com s codeigniter The Codelgniter tutorials site http video derekallard com Using Codelgniter for PHP application development http www macronimous com resources using codeigniter for PHP_ application_development asp Codelgniter powered sites proactively updated list http poweredsites org Codelgniter site projects http seeroo com tag codeigniter Featured websites powered by Codelgniter are as follows Sprint Center http www sprintcenter com AT amp T Center http www attcenter com World Gold Council http www gold org Motortopia http www motortopia com Club 3D http www club 3d com Cyber Ears http www cyberears com For more information refer to the Build on CodeIgniter section at http ellislab com codeigniter 170 Symbols htaccess file 9 A admin use
87. egistration File folde a views r d reports File folder B admin r s J settings File fe P almin e J status File folder p chat a di templates File folder m compnents 7 a dJi uploads File folder B datot x J utils File folder J dmz_htmiform J headings p kosher pages m payments m photos_and_docs P prints p registration a reports B settings mY status templates B uploads m utils J assets D css J font J img n a media J modes d system 145 Views For example to render a template file located at Application views templates named home php we shall write the following code S this gt load gt view templates home The following load view call render a view using all its optional parameters Sthis gt load gt view view_file PHP view file to render Sview_params parameters array for view FALSE FALSE default to output TRUE back as string i In this example view_file is referring to the CI resource PHP view file application views view_file php view_params is the array of parameters scalar array in each entry for the view file as we have demonstrated at several places earlier so that each array key let s say name becomes the name PHP variable in the view to use In case we wish to get the processed view into a buffer for special processing caching or any other processing purpose for example you may call the following example Svie
88. eign key fields They can support one to many many to one and many to many relationships or even more complex relationships between multiple database tables The ORM plugins also provide validation and manipulation services on the handled database fields such as performing trimming on a string field before it is saved to the database Validation services include built in validations such as valid e mail fields or a field that must have the same value as another field such as fields with an account creation password retype requirement The full scope and usage of ORM is beyond the scope of this CI book However it is highly recommended that you learn more about ORM and try using the referred ORM plugins and consider using them in your CI projects Of course we do provide a simple usage example of adding a record to the database and retrieving the database records using ORM in the following section 123 Models ORM simple operations example For example let s say we have a user database table with the ID as the primary key auto increment User name e mail and password are the other fields and if we want to add a new user record to the database we could do so with the help of the following code lt PHP Su u u u if We shall define the database table named users with ID as auto increment username password and e mail as the other fields ORM will create an use
89. el returned user role If the logged in user issues the logout anchor auth logout will be called to destroy the session and redirect the user to the login form The following is the code class Users model extends CI Model function __ construct parent construct functioncheck login user Spass Important notice Since the model extends the base CI model it already got the instance However we can use the ci amp get_instance instead this gt anywhere in helpers libraries and so on convert the typed password in the login form to md5 same as we do when opening a user account Smd5_pass md5 Spass build up the query sql SELECT FROM users WHERE user name Suser AND password Smd5_pass q S this gt db gt query sql if q gt num_rows foreach q gt result as row return Srow In case no num_rows return NULL 59 Controller Usage and Scope The database file to upload for this example We shall upload this database file provided as part of the book resources into our database connected to CI The user table includes two users namely reg_user and admin_user Their passwords are stored as the md5 of the text passwords where 111111111 and 222222222 are the passwords of the reg_user and admin_user users The following is the code phpMyAdmin SQL Dump http www phpmyadmin net SET SQL MODE NO_AUT
90. elements and UI elements such as buttons scrollbars and navigation elements we see and operate via the browser to navigate to other page views or get specific information or media by issuing a page anchor button clicking on the icon and so on The described action causes another HTTP request s either synchronous mostly anchor or asynchronous AJAX request s handled by JavaScript embedded in the web page Views The CI view is a PHP file that may contain part or all of the following PHP statements HTML tags CSS JavaScript program Flash images and media sources In CI a view file may contain the PHP code that uses the controller provided parameters or even call the CI helpers libraries or model directly to generate the output that is part of the generated HTML file response The generated PHP output can be strings or numeric values incorporated in the HTML tags or even a whole HTML page This chapter will primarily focus on the following topics e The CI view scope The CI view resources path The rendering flow Client side flexibility Accessing libraries helpers within a view Forms Using AJAX View parser configuration issues Integrating jQuery or other client side libraries e View rendering plugins view template plugin example e Example 1 HTML5 location powered by Google Maps e Example 2 user feedback powered by AJAX and the jQuery UI We will begin by briefly reviewing the CI view scope a
91. enior web developer Jason has worked with over 100 online startups globally in addition to providing consulting services and mentorship to early stage startups and entrepreneurs For over 6 years Jason has taught the Building Database Driven Websites course at McMaster University Continuing Ed based in Hamilton Ontario Canada Muhammad Faisal Shabbir works as a senior software engineer at Strategic Systems International Faisal has more than 6 years of extensive experience in software architecture design agile development and deployment Faisal completed his BS Information Technology from Virtual University He can be reached at faisal215 gmail com Special thanks to my mother wife and kids who pushed me up to do such activities www PacktPub com Support files eBooks discount offers and more You might want to visit www Packt Pub com for support files and downloads related to your book Did you know that Packt offers eBook versions of every book published with PDF and ePub files available You can upgrade to the eBook version at www Packt Pub com and as a print book customer you are entitled to a discount on the eBook copy Get in touch with us at service packtpub com for more details At www Packt Pub com you can also read a collection of free technical articles sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks P PACKTLIB http PacktLib Pack
92. ep through practical and detailed examples which are all fully backed up with code illustrations and tips Responsive Web Design with HTML5 and CSS3 Responsive Web Design with HTML5 and CSS3 ISBN 978 1 84969 318 9 Paperback 324 pages Learn responsive design using HTML5 and CSS3 to adapt websites to any browser or screen size 1 Everything needed to code websites in HTML5 and CSS3 that are responsive to every device or screen size 2 Learn the main new features of HTML5 and use CSS3 s stunning new capabilities including animations transitions and transformations 3 Real world examples show how to progressively enhance a responsive design while providing fall backs for older browsers Please check www PacktPub com for information on our titles
93. er The following are the chapter topics and subtopics we will cover e Scope of the CI controller e Usage categories Rendering views Controllers serving browser AJAX requests Controllers serving Linux scheduled cron execution requests e Cl controller expansion and usage Refer to Chapter 2 Configurations and Naming Conventions for more Loading resources of models helpers and libraries Using loaded resources Calling CI controller methods e Example 1 the default home page controller e Example 2 sending e mails with attachments e Example 3 admin and regular user log in We will begin by briefly reviewing the scope and use cases of the application controller in the CI framework and how we can use them for our project requirement needs 42 Chapter 3 Scope of the Cl controller The CI controller is the hub and brain of the CI MVC that handles the HTTP requests such as those from the browser and operates with other CI resources to respond The CI controller scope is described in the following figure and operates with other CI resources to respond to the requests Codeigniter s Controller Scope SQL Database s lt gt cl Models d cI Libraries g Cl Helpers _ ae CI Routes can be many User Browser User The general flow with the CI controller is shown in the preceding figure The user s browser sends the HTTP request to the URL of the CI
94. es and initializes oauth consumer object param config array of the Linked configuration variables 89 Libraries public function _ construct config Setting the handler s variables foreach config as k gt v Sthis gt Sk Sv Setting all the class variables Sthis gt set_varaiables Initializing the oauth consumer object Sthis gt oauth_ consumer new oauth S this gt api_key Sthis gt secret_key Enabling Oauth debug Sthis gt oauth_consumer gt enableDebug Checking if returned from the LinkedIn UI permission conformation window if this gt CI gt input gt get oauth_verifier this gt CI gt input gt get oauth_problem Sthis gt on_success elseif this gt CI gt session suserdata oauth_token amp amp Sthis gt CI gt session gt userdata oauth_token_secret if session variables are not set oauth_token oauth_token_secret call auth to start the process of getting the tokens from LinkedIn via the oauth consumer object Sthis gt auth elseif S this gt CI gt session gt userdata oauth_token amp amp Sthis gt ClI gt session suserdata oauth_token_secret if session variables are set oauth_token oauth_token_secret initialize the oauth consumer with Soauth_token Soauth_token_secret Soauth_token this gt CI gt session suserdata oauth_token Soauth_token_ secret this gt CI gt se
95. est to the LinkedIn access_token_path to receive the array which it s keys are tokens oauth_token oauth_token_secret for accessing the LinkedIn resources Saccess_ token reponse this gt linkedin gt getAccessToken this gt input gt get oauth_verifier Setting the session variables with the tokens oauth_token oauth_token_secret for accessing the LinkedIn resources Sthis gt session gt set_userdata lt oauth_token gt Saccess_token_reponse lt oauth_token gt Sthis gt session gt set_userdata lt oauth_token_secret gt Saccess_token_reponse lt oauth_token_secret gt 98 Chapter 4 Redirecting to the main page redirect lt gt This function calls the library method me to get the LinkedIn user gt s details 2 public function me Get the LinkedIn user gt s details s_json this gt linkedin gt me o_my_details json_decode s_json prodile_url o_my details gt siteStandardProfileRequest gt url Sview_params lt my_details gt So my details Sview_params lt profile_url gt prodile_ url Load the view for displaying the LinkedIn user gt s details Sthis gt load sview lt linkedin me gt Sview_params T This function calls the library method me to get the LinkedIn user gt s connections public function connections Get the LinkedIn user gt s connections s_ json S this gt linkedin gt connections
96. et_user feedback _log ajax type POST url get_user feedbacks dataType json success function data feedback_log_ view show eedback_log view html data result D document ready function Set up the jQuery UI feedback dialog ideas form dialog autoOpen false height 270 width 700 modal true resizable false effect drop direction up show slide buttons Send Us Your Feedback function varuser feedback user feedback val ajax_save_user_ feedback user_feedback clean feedback entry for next one user feedback val Show user all its feedback so far ajax_get_user_ feedback _log this dialog close Cancel function this dialog close Fa When user clicks on for a popup feedback window user_ideas button click function 166 Chapter 7 ideas form dialog open p eedback_log button click function ajax_get_user_feedback_log ar Document ready section lt script gt lt head gt lt body gt lt Hl gt Welcome lt Suser_ name gt lt H1 gt lt H1l gt You are logged in lt H1 gt lt HR gt lt HR gt lt H3 gt Your User ID is lt Suid gt lt H3 gt lt H3 gt Your System Role is lt Srole gt lt H3 gt lt H3 gt Your Menu options lt menu gt lt H3 gt lt DI
97. ew file accessing CI resources such as libraries models helpers lt HTML gt lt PHP URI is a built in CI library if the rendering controller for this view was http mysite myproject mycontroller test3 the segment 1 mycontroller the controller name the segment 2 test3 the controller method Sthe controller this gt uri gt segment 1 Sthe method S this suri gt segment 2 2m lt H1l gt This View Rendered by Controller lt Sthe controller gt lt H1 gt lt Hl gt Using its method named lt the_ method gt lt H1 gt Forms The CI PHP view file can contain any number of HTML data entry forms to accept the input data from the browsing user We can use the CI form helper service to simplify the data entry buildup powered validation services The CI form helper provides a useful and comprehensive set of PHP functions for many data entry and input fashions Among them we can find the data entry of the text field the area text field the radio button checkbox combo box and menu option The following is a list of the most common CI form helper functions e form_open e form_input e form dropdown 148 Chapter 7 e form_password e form upload e form textarea e form_multiselect e form checkbox e form radio e form close The CI form helper generates an HTML portion that is rendered as part of the HTML file that is retu
98. f they are set then it initializes the oauth consumer else it will call the method auth to start the process of setting the token It also loads the LinkedIn library public function _ construct parent construct Slinked_ config array Application keys registered in the LinkedIn developer app 97 Libraries lt api_key gt gt lt esq76cosbm9x gt lt secret_key gt gt lt TyUQ2FzZRRzZWz9bHk gt The url to return from the LinkedIn confirmation URL lt callback_url gt gt base_url lt linkedinfo on_success gt The URL when the failure occurs lt on_failure_url gt gt lt linkedinfo on_failure gt Load the LinkedIn library Sthis gt load gt library lt linkedin_handler gt Slinked_ config Load the main menu of the application ay public function index Sthis gt load gt view lt linkedin menu gt This is the method called after returning from the LinkedIn authorization URL The returned values from the LinkedIn authorization URL are oauth_token oauth_token_secret oauth_verifier Those values are used to retrieve oauth_token oauth_token_secret for accessing the LinkedIn resources ef public function onSucess Set the oauth consumer tokens Sthis gt linkedin gt setToken this gt input gt get lt oauth_token gt Sthis gt session gt userdata lt oauth_token_secret gt j Sending the requ
99. flickr_recent php controller file The application controllers flickr recent php controller file will load the flickr_wrapper API call its services for newly uploaded public photos and photographers and render a view to show the results In order to execute the controller you should point your browser to the following URI http mydomain com myproject flickr recent 82 Chapter 4 The following is the controller code lt php Flickr Recent Controller Provide recent uploaded public photos in flickr community Enable to apply several settings and filtering Enable to get photographer user profile for each photo author Eli Orr class Flickr recent extends CI_Controller function __ construct parent construct Standard Libraries database amp helper url are included in the configs autoload php This lines are only for debugging needs we may drop them if things are going good error reporting E ALL ini_set display_errors 1 Loading User Defined Library S this gt load gt library flickr_wrapper array api_key gt lt YOUR_FLICKR_API gt DEFAULT RES gt 3000 filter 3000 pix GPS ENABLED gt FALSE The class code continues as follows function index S settings array DEFAULT RES gt 4000 Only 4000 pix and better GPS ENABLED gt FALSE GPS Info is not mandatory RECEN
100. googlemaps gt initialize config Add visual flag this gt add_ visual flag config center data this gt load map setting Load our view passing the map data that has just been created this gt load gt view google map view data NEW CODE functionis user location defined return this gt user lat 0 this gt user_lon 0 NEW CODE functionget user location return this gt user lat this gt user_ lon functionlondon as before functionbombai as before functionrehovot as before functionload_map_setting as before functionadd visual flag place as before End class Gmaps 158 Chapter 7 The view file The view PHP file is located at application views google map view php The following is the view file code based on Chapter 4 Libraries for the extended Google Maps API usage example view where the new parts of code are highlighted Here we add an HTML5 service in JavaScript to collect the user s geolocation and call the controller method user location lat 0 lon 0 lt DOCTYPE html gt lt meta http equiv Content type content text html charset utf 8 gt lt html gt lt head gt lt script src http code jquery com jquery latest js type text javascript gt lt script gt lt script gt New Code to get the user Geolo
101. h 3 marked places London UK Bombai India Rehovot Israel Initialize our map for this use case of show 3 places altogether let the zoom be automatically decided by Google for showing the several places in one view Sconfig zoom auto Sthis gt googlemaps gt initialize config Define the places we want to see marked on Google Map Sthis gt add_visual_flag London UK Sthis gt add_visual_ flag Bombai India Sthis gt add_visual_flag Rehovot Israel NEW CODE optional user location if user allow it and was fetched successfully if this gt is user location defined this gt add_ visual flag this gt get_user location data this gt load_map_setting Load our view passing the map data that has just been created Sthis gt load sview google map _view data NEW CODE function user location lat 0 lon 0 This is a new code we add for showing the Geolocation fetched from the view base HTML5 Geolocation service Initialize our map with it if it is set if lat amp lon this gt index They are ok let s keep them this gt user lat Slat 157 Views this gt user_ lon lon config center this gt get_user location Define the address we want to be on the map center config zoom 5 since its approximate location is country level this gt
102. hapter we have reviewed the CI model scope business logic and ORM We have made the following examples in this chapter e Example 1 a CRUD example e Example 2 a business logic example e Example 3 retrieving data from Facebook 141 Views This chapter covers the process flow to render views the process flow within the view file different type of views and their role and usage with several code examples of web applications The views are programmatic portions that provide content to the browser to be executed on the client side that is the user PC to make the user interface session on the local computer The PHP view file rendered output returns from the server as an HTTP response content to the requesting browser application that is requesting via submitting a URI in the browser navigation area Initially the browser sends a URI request that the user types in to a default or specific controller method such as http mysite com myapp helloworld The called controller method processes the request performs its decision making and may use the other CI resources such as helpers libraries models and eventually renders a view back as an HTTP response to the browser HTTP request that initiates the controller operation The HTML file returned to the browser includes HTML CSS and JavaScript The browser executes the received rendered view from the server and uses it to perform the user interface session visual
103. he Facebook user to the Facebook login page to confirm the Facebook app s permission The controller is also responsible for getting the Facebook user s details and friends via the model and rendering the view page accordingly For more information about Facebook API usage and development please refer to the Facebook developer page available at http developers facebook com The following are the code and inline explanations lt php class Fbpage extends CI Controller public function __construct parent construct Extremely important 137 Models Due to the fact that the CI handles classes for _GET _POST and COOKIE parse_str is called to copy the variables sent by Facebook to the REQUEST var so that the Facebook SDK can do its checks This is done in order to avoid infinite redirect loop parse_str SERVER QUERY STRING REQUEST This method retrieves Facebook data of a Facebook user and displays personal details and some of his friends It checks if a Facebook token is valid if it s valid then it displays his details otherwise it produces the token public function index Sa_config array appId gt fb API secret gt fb secret cookie gt true Sthis gt load gt library facebook Sa_config Checking if the user is logged in and confirms the app s permissions if user this gt facebook gt getUser
104. he model lt php class Usermodel extends CI Model function __ construct This method retrieves the users list and returns an array of objects each containing user details Call the Model constructor parent construct function get_users Calling CI s database object s method for generating SQL queries query this gt db gt get users returns an array of users objects return S query gt result In this example the CI object database s method is called for generating and executing the SQL query Please refer to the CI database s library at http ellislab com codeigniter user guide database index html For more information about models refer to Chapter 6 Models The view file The view in this example shows the table content received from the controller containing the users list as defined in the database The following is the corresponding rendered view lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt lt php echo mega_title gt lt title gt lt head gt lt body gt lt table gt lt tr gt 16 Chapter 1 lt tdsID lt td gt lt td gt Name lt td gt lt td gt Email lt td gt lt tr gt lt php foreach Susers as Suser gt lt tr gt lt td gt lt php echo user gt user_id gt lt td gt lt td gt lt php echo S user gt user_ fname Suser gt user_l
105. his gt is filtered photo Sphoto filtered_photos photo return filtered_ photos function flickrUserInfo Suid UID e g 72095130 N00 find info for this User build the API URL to call Sparams array api_key gt S this gt api_key method gt flickr people getInfo user id gt Suid extras gt contact friend family format gt php serial 80 Chapter 4 Srsp_ obj this gt flickrRestAPI Sparams Check if response is OK if rsp_obj stat ok Yes We have a good result let s load it to the keyed array structure Sreal_name urlencode rsp_obj person realname _content Slocation urlencode strtolower S rsp_obj person location content Sphotos rsp_obj person photos count content more can be added The class code continues as follows Sparams array name gt Sreal_name uid gt Suid photos gt photos location gt S location full_info gt rsp_obj return S params else Response failed return NULL return NULL PRIVATE SECTION OF ALL PRIVATE LIBRARY METHODS THAT CANNOT BE CALLED DIRECTLY FROM THE LIBRARY USER This is the heart of our wrapper library that makes it easy to get The Flickr API access via simple keyed array based calls and response private function flickrRestAPI
106. hod_name param1 param2 paramN The controller named controller_name will be instanced by CI with the controller constructor and then the method method_name will be called with the first parameter param second parameter param2 and so on On the PHP controller side the controller_name method prototype will look like the following example Publicfunction method_name Suser name email Sphone So that user param name param2 and so on This is one possible way to get the parameters through a URL or get an array In Codelgniter we don t have to get an array so we can use the URI class to get the parameters For reference see http ellislab com codeigniter user guide libraries uri html If we provide lt PHP base _url gt controller_name The CI will execute only the controller constructor and the index method if the controller has such a method If we provide lt PHP base_url gt controller_name method_name 35 Configurations and Naming Conventions The preceding code will be executed without calling the index method following the call of the specific method method_name Remember that we shall not use in our parameters for such a call and may wish to provide them using URL encode or other reversible encoding methods We can also call our controller method using POST GET so that we can retrieve the parameter value posted in the class method in the following way Sparaml_ v
107. http code jquery com jquery latest js gt lt script gt lt Render all the map JS provided by rendering controller gt lt php echo map js gt lt head gt lt body gt lt H3 gt CodeIgniter Powered CI Google Maps Library lt H3 gt lt HR gt lt ul gt lt Let the User Always Get Back to the default Zoom out gt lt li gt lt php echo anchor index php gmaps lt B gt See All Locations lt B gt gt lt li gt lt PHP i 0 foreach S locations as location 75 Libraries Show to the user all the possible Zoom Ins defined places by the controller so that user may zoom in by issuing the anchor Scontroller controllers i i gt lt li gt lt php echo anchor index php gmaps S controller Zoom In to Slocation lt li gt lt PHP gt lt ul gt lt HR gt lt HR gt lt php echo Smap html gt lt body gt lt html gt Example 3 building a library such as the Flickr API wrapper The flickr com website by Yahoo provides API access to the Flickr repository of public photos uploaded to the community The API is extremely rich and its documentation is available at http www flickr com services api and is called App Garden gt The API is enabled for various programming languages and access methods We will build a solution of a wrapper that can be expanded to get any Flickr API servic
108. ibraries loading 37 38 models loading 37 38 Motortopia URL 170 MySQL database USERS_DB sql table 55 O ORM Object Relational Mapping about 122 simple operations example 124 125 ORM plugins for CI DataMapper Codelgniter ORM library 123 Doctrine ORM 123 P Packagist URL 68 parser configuration issues 152 R regular user login 55 resources path CI view scope 145 146 routes php file 29 S site_url function 110 Sprint Center URL 170 SSL helper example about 112 controller file 113 helper file 112 view file 114 SSN Social Security Number 150 system folder 10 174 T V third party libraries example view about 71 about 144 controller file 72 75 complex parameters passing to 12 view file 75 view file linked company updates php 101 view_params 146 U view template plugin rendering 153 user defined CI controller 44 users_model resource services W get_logged_in_user 162 keep_user_feedback 162 World Gold Council UX User Experience 154 URL 170 175 PAC KT Thank you for buying susti suino 2 Programming with Codelgniter MVC About Packt Publishing Packt pronounced packed published its first book Mastering phpMyAdmin for Effective MySQL Management in April 2004 and subsequently continued to specialize in publishing highly focused books on specific technologies and solutions Our books and publications share the experiences of your fellow IT professionals in adapting
109. ies models and helper capabilities in our controller we may also want to load libraries and helpers to our controller or model class to reuse them for our needs In case we decide that certain helpers libraries or models are useful we will have them loaded automatically We can do so in the autoload configuration file named autoload php located at application config autoload php in our project The following is an autoload configuration example Sautoload libraries array template database session Sautoload helper array url utilities Note url helper provide base _url service Remember that if we want to load our helpers or libraries within a certain controller or model we can enable it as per the following example classMy handler extends CI Controller function _ construct see previous explanation on this parent call parent construct Loadingspecific helperto enable calling its functions in all this controller class methods as well as in all rendered views Note how the full name and path is abbreviated Sthis gt load gt helper ssl_helper Loading and instantiatinga library s application librarues smart_handler php to enable calling all its class methods as from this controller as well from all the rendered by this controller Sthis gt load slibrary smart_handler j function enforce ssl 37 Configuratio
110. ieves the products list calling the model productmodel s method get _products renders the results in the view productsview public function index Loading the url helper this gt load shelper url Manually loading the database S Sthis gt load gt sdatabase Loading the model class this gt load gt model productmodel Calling the model productmodel s method get_products to retrieve the products from the database Sview_params products this gt productmodel gt get_products Rendering the products list in the view productsview Sthis gt load gt view productsview view_params This method handles the operation of adding a product to the database 127 Models public function add Loading the url helper Sthis gt load gt helper url Manually loading the database Sthis gt load gt database Loading the model class Sthis gt load gt model productmodel Sa_post_values this gt input gt post S view_ params this gt load_form product add a_post_ values Validating the form if this gt form_validation gt run FALSE Validation failed Sthis gt load sview productform Sview_params else data a_post_values array_pop data Sthis gt productmodel gt addProduct data redirect product This method handles the operation of editing a product
111. iew path under views After we provide the root path via the base tag using the built in CI URL helper lt base href lt php echo base url gt gt both will load myjs js as follows lt script type text javascript src javascript myjs js gt lt script gt They load as if they were located at the project root This is due to the fact that CI processes the requests and rendering views as part of the root directory index php Hence all the directory paths for SRC or INCLUDE from view PHP portions are considered as if they occurred from the project root directory This is due to the fact that all URIs to the project are executed by index php So for all the project code the directory path is as though your code was in the same directory of index php or at the CI project root directory Plugins for rendering view As mentioned at the beginning of this chapter we can use third party libraries to enable us to create more advanced rendering services in a template layout fashion For example the CodeIgniter template class by Colin Williams available at http www williamsconcepts com ci libraries template index html This plugin enables us to define the rendered page as a Lego fashioned layout with predefined page regions so that we can have a different PHP CI view to render each of them 153 Views This way we can have great reusability and a unified look and fill in the entire application page which in many ca
112. ile and social environments integration Among the environments Yehuda integrated with Facebook API LinkedIn API and various others Yehuda is an enthusiastic CodeIgniter developer who has been developing rich functionality and heavy traffic web based applications over the last two years Among Yehuda s clients are several major academic institutes Yehuda can be contacted through his website http yudazdk co il Writing this book has been a challenging experience for me My purpose was to write a practical book for developers that includes many examples First of all I would like to thank my family members my wife Elana and my son Avishay for their understanding and support during the writing of this book I would like to thank Eli Orr a lot for assisting me in writing the book and his helpful and fruitful feedback I would like to thank my clients Omer Weissbein CEO of Ontxt for his advice and support and Merav Babai CEO of Pro Man and a LinkedIn expert for her LinkedIn tips Finally I would like to thank the Packt Publishing team for advising and assisting me throughout the book writing process About the Reviewers Jason Hamilton Mascioli leads the growth of 77robots com the Canadian based web development company he founded in 2005 Jason s role is to find and work with entrepreneurs to produce sustainable online businesses including solutions that aid early stage startups With over 15 years as a s
113. include rendering of all the defined template regions as follows lt html gt lt body gt lt div gt lt Sheader gt lt div gt lt div gt lt Supper navigation gt lt div gt lt div gt lt main content area gt lt Scontent gt lt div gt 154 Chapter 7 lt footer gt lt Sfooter gt lt body gt lt html gt In order to use the preceding template plugin we will do the following First load the template library in the CI controller constructor s where we want to use the template Remember that the template library is located at lt Project_root gt application libraries Template php We shall load the template library as follows Sthis gt load gt library template lt Project_root gt application libraries Template php Sthis gt load gt library template Then we will load the specific template file we have configured we can define many to choose from let s say in the controller constructor we will also assume that all the controller methods use the same template set the selected template from the template config we want to use Sthis gt template gt set_template default Now for rendering the template regions into a rendered view using the predefined view We will do as follows For each template region at let s say lt Project_root gt application views we Shall load it to the corresponding region as follows Sthis gt
114. index php my_class mymethod This will define url as http example com mydev index php my_class To set the index page as a part of the URI path to CI controllers methods we use Sconfig index page index php The index php file is the CI root PHP service that handles all the URI requests It is used as part of a path URI to a resource such as http mysite com fci index php tables_management show However we can hide the index php file by setting CI to hide the index php file in the URI path for calling the CI resources such as http mysite com fci tables_management show To do so we need to perform the following configuration steps 1 Inthe project root directory where the CI index php file resides an HTACCESS type file named htaccess is added with the following configuration lines which reroutes a none index php URI referring to the CI project controllers path without index php RewriteEngine On RewriteCond REQUEST FILENAME f RewriteCond REQUEST FILENAME d A RewriteRule index php 1 L lt Files index php gt AcceptPathInfo On lt Files gt 23 Configurations and Naming Conventions For more on this refer to http en wikipedia org wiki Htaccess 2 We should make the change to the config config php file so that index_page will be empty in the URI path string instead of the default index php config index_page The htaccess file does the trick here by
115. iondoc_root path 54 Chapter 3 An auxiliary method for calculating attachment file path in our server return doc_root SERVER DOCUMENT_ROOT Example 3 admin and regular user log in In this example we will see how the controller can coordinate using models and views a login session for a regular user as well as an admin super user so that each will have a distinct menu In order to use the provided database file and successfully log in use the following steps For regular user login User reg_user Password 111111111 9x 1s For admin user login User admin_user Password 111111111 9x 1s This example will be constructed from the following controller models and views application controller auth php This controller is used to control authentication checkup and redirect each user category to its view or notify of a login failure Regular users and admin users will have different view menu message and logout options application models users model php This is the model to validate the submitted user name and password stored in the database via MD5 against the predefined database table of users application views login view php This is the view shown to users that are not logged in in order to log in application views logged_in view php This is the view shown to users that were successfully logged in and performed their roles as reg_user admin users
116. is localhost the connection to the database fails Setting the hostname to 127 0 0 1 solves the problem 14 Chapter 1 db default username dbUser S db default password dbPassword db default database dbDataAbse Sdb default port dbPort The model class will retrieve all the user details from the table users For more information on configurations refer to Chapter 2 Configuration and Naming Conventions We will build the following three scripts e The controller class application controllers user php e The model file application model usermodel php e The view script application views userview php The controller file The controller retrieves the users list from the database via the model and renders the view with it The following is the code sample of the controller lt php class User extends CI Controller function users Manually loading the database this gt load gt database Loading the model class this gt load gt model Usermodel Sview_params mega_title Model Example Calling the model to retrieve the users from the database Sview_params users S this gt Usermodel sget_users this gt load gt view userview S view params End of file welcome php Location application controllers welcome php 15 Getting Started The model file The following is the code sample of t
117. is example we will use a model that performs these operations on the database SELECT INSERT and UPDATE The example displays all the products that are retrieved by the model productmodel at the main page in the database Let us assume the URI to the project root is http mydomain com myproject and http mydomain com myproject product Ge The source code is provided with this book via URLs The main page has links for adding and editing a product These links generate a form for editing and adding a product Let us assume the URI to the project root is http mydomain com myproject and http mydomain com myproject product add Suppose we want to edit and update the product with product_id 1 the link will be http mydomain com myproject product edit 1 This example will be constructed from the following controller model and views e application controllers product php This controller loads the model product Sthis gt load gt model productmodel1 This controller renders the following views productsview This view displays all the products with links to editing and adding a product product form This view contains the form for adding and editing a product e application models productmodel php This model contains functions that perform these operations on the database SELECT INSERT and UPDATE 125 Models e application views productsview php The view that displays the p
118. is the amount of usage in our controller Let s say for example that we have 40 controllers and 39 of them need the same library We shall add that library into the auto load list config autoload php If we did add a resource such as a library model or helper into the project auto load we can eliminate loading it in the class that needs the resource services as follows class Email extends CI_Controller The controller email php file will contain this class function __ construct call the parent constructor to inherit all its services parent construct Loads the CI e mail library so that it will be instantiated and its methods will be accessed as this gt mail gt METHOD NAME Sthis gt load gt library email Define the controller methodindex the default method so that referring to the URI mydomain com myproject email will execute the index method call function index Configure the library to work with UTF 8 strings multi language support as well as enable HTML content body Sconfig charset utf 8 Sconfig mailtype html Loads the configuration settings by initialize method Sthis gt email gt initialize config Since the mail body is HTML define CR LF to be replaced with HTML lt BR gt Sthis gt email gt set_ newline lt BR gt Define the From Email address Sthis gt email gt from eliorr phpmyqsl com Eli Orr
119. ite _url lt gt gt gt Back to Menu lt a gt lt p gt lt div gt lt table gt lt tr gt lt td gt Name lt td gt lt td gt lt php echo name gt lt td gt lt tr gt lt tr gt lt td gt Founded lt td gt lt td gt lt php echo foundedYear gt lt td gt lt tr gt lt tr gt lt td gt employeeCountRange lt td gt 103 Libraries lt td gt lt php echo SemployeeCountRange gt lt td gt lt tr gt lt tr gt lt td gt Specialties lt td gt lt td gt lt ul gt lt php foreach specialties as specialty gt lt li gt lt php echo specialty gt lt li gt lt php endforeach gt lt ul gt lt td gt lt tr gt lt tr gt lt tdsWebsite lt tds gt lt td gt lt a href lt php echo SwebsiteUrl gt gt Website lt a gt lt td gt lt tr gt lt tr gt lt tdsnumFollowers lt td gt lt td gt lt php echo numFollowers gt lt td gt lt tr gt lt table gt lt div style margin top 10px gt lt a href lt php echo site url lt linkedinfo company_updates 7919 gt gt gt Updates lt a gt lt div gt lt body gt lt html gt The view file linked company updates php This view file displays the three updates of the LinkedIn company The following is the view code lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Company lt title gt lt head gt lt body g
120. its as a product manager with Radvision Ltd During that period he was an active member with the ITU T Signaling Group 16 for defining VoIP protocol standardization mainly the H 323 Prior to that Eli led development teams with IAI Israel Aircraft Industries ELTA based C and C programming languages Eli can be contacted through his website http E1liOrr com Wring a book about the Codelgniter Framework that I use daily and love was a great thrilling challenge for me I would like to thank Yehuda Zadik who assisted me in writing this book as well as the Packt Publishing team for advising and assisting me through the entire book writing process In addition I would like to thank Asher Efrati who is a strong Codelgniter supporter who assisted me by reviewing the book drafts and commenting on them Finally I would like to thank my daughter Hila Orr who supported me in my effort of writing this book Yehuda Zadik has over 20 years of experience in the IT industry where he mainly specialized in software development based object oriented programming OOP technologies Yehuda has over 8 years of experience developing with PHP OOP and open source Linux environments for developing web based applications Yehuda used state of the art technologies for building dynamic web based applications that were e commerce enabled as well as social network integrated Yehuda has a vast knowledge for integrating third party plugins for network mob
121. kers It is recommended to set it to TRUE even if we reduce a bit of our application performance Sconfig csrf protection FALSE If set to TRUE the CI will prevent Cross Site Request Forgery CSRF XSRF attacks The risk is when the fraud form is submitted If we are accepting user data it is strongly recommended that CSRF protection should be enabled Note that when using AJAX additional code may be required to enable CSRF protection with AJAX database php The database configuration enables to define one or more database connections that can be used by the application The database configuration is a two dimensional array in the following form Sdb db_ entry db_ connection_param By setting the parameters for database default entry we shall define the following parameters S db default hostname localhost note in some cases 127 0 0 1 must be used instead localhost if the database server is in another server use URI such as domain db NNNNNNN hostedresource com or similar advise our system admin service provider Optional configuration of DB server connection port Sdb default port 4009 In case our DB server operates on another port otherwise we may drop the port config line Sdb default username mydefaultdb 27 Configurations and Naming Conventions db db db db db db db db db db default tn
122. ktpub com Cover Image by Abhishek Pandey abhishek pandey1210 gmail com Credits Authors Eli Orr Yehuda Zadik Reviewers Jason Hamilton Mascioli Muhammad Faisal Shabbir Acquisition Editor James Jones Commissioning Editor Shreerang Deshpande Technical Editors Dylan Fernandes Dipika Gaonkar Kapil Hemnani Copy Editor Brandt D Mello Aditya Nair Alfida Paiva Laxmi Subramanian Project Coordinator Deenar Satam Proofreader Maria Gould Indexer Tejal Soni Graphics Ronak Dhruv Production Coordinator Melwyn D sa Cover Work Melwyn D sa About the Authors Eli Orr brings over 30 years of experience in the high tech industry mainly in software product development Eli published articles in several magazines such as Wireless Systems Design Telephony Online CommsDesign EE Times and for various emerging technologies Currently Eli is a PHP Advanced Web Application Developer focused on Codelgniter based projects for the last two years for rich functionality heavy duty web applications Prior to that as an entrepreneur Eli established LogoDial Zappix Ltd and had the role of CTO and server side developer Zappix enables dynamic visualization of call center voice menus which are currently available in USA and Israel on iOS and Android enabled smartphones Prior to that Eli developed telecom solutions for Unified Communications with AT amp T Prior to that Eli developed VoIP developer toolk
123. l check if there s a login user session and if so will fetch its record 163 Views ci amp get_instance get the login in user ID if any Suid Sthis gt session gt userdata user_id if Suid return NULL sql SELECT FROMusers WHERE id Suid q ci gt db gt query sql if q gt num_rows foreach q gt result as row return row return NULL Function get_user_rec uid Will check if there s a login user session and if so will fetch its record Sci amp get_instance get the login in user ID if any if Suid return NULL sql SELECT FROM users WHERE id Suid q ci gt db gt query sql if q gt num_rows foreach q gt result as row return row return NULL Function keep user feedback feedback Sci amp get_instance Suid_rec Sthis gt get_logged_in_user Suid Suid_rec Suid_rec gt id 0 id email uid feedback timestamp Stable user_feedback data array feedback gt urldecode feedback uid gt uid ci gt db gt insert Stable data Function get_user feedbacks uid Sci amp get_instance if Suid return NULL feedbacks array 164 Chapter 7 Stable user_ feedback sql SELECT FROM StableWHERE uid Suid ORDER BY timestamp DESC q ci gt db gt query sql if q gt num_ro
124. le hooks FALSE The preceding configuration if set to TRUE will enable us to define hooks into CI events where the hooks are defined in the application hooks directory Do not touch these settings unless you have a specific plan for CI event hooks Note that the concept of adding hooks to the CI core activity is out of this book s scope Sconfig subclass prefix MY_ 24 Chapter 2 The preceding configuration will enable us to define naming roles to our library class name s prefix in order to distinguish with other default libraries Sconfig permitted_uri_chars a z 0 9 _ The preceding code defines the allowed chars within a URI calling CI resources mainly controllers It is recommended to not touch this setting Sconfig allow_get_array TRUE This will enable us to call the controller class methods with parameters such as in the example provided earlier lt php echo base _url gt index php my_handler calc 5 7 The preceding code will provide the same results as the class method within the my_handler class itself in the following format Val Sthis gt calc 5 7 The following configuration defines if a GET URL query string will be used Sconfig enable query strings FALSE This configuration if set to TRUE will enable us to call controller class methods with parameters in the GET URL query form lt php echo base_url gt index php my_handler calc php a 5 amp b 7
125. library which enables access to the LinkedIn resources such as the LinkedIn user s details and connections and the companies details application views linkedin me php The view which displays the LinkedIn user s details application views linked connections php The view which displays the LinkedIn user s connections application views linked company php The view which displays the company s details application views linked company updates php The view which displays a company s updates Let us assume the URI to the project root is http mydomain com myproject Hence the URI to execute the auth controller for login will be http mydomain com myproject linkedinfo 87 Libraries The linkedin_handler php library file The library file application libraries linkedin_handler php contains the class library linkedin_handler The library contains the function for authenticating the app and accessing the LinkedIn resources The following is the code lt php if defined BASEPATH exit No direct script access allowed The php oauth extension is required For more information refer to http ill php net manual en book oauth php if extension_loaded oauth throw new Exception Simple LinkedIn library not compatible with installed PECL oauth extension Please disable this extension to use the Simple LinkedIn library CodeIgniter LinkedIn API package CodeIgniter auth
126. ll be constructed from the following library controller and view e application libraries This is the CI wrapper library for Google Maps that we downloaded Refer to the CI library contributor website at http biostall com e application controllers gmaps php This controller loads the googlemaps library and builds up several views for several places shown together on the Google Map and zooms in to each of the places S this gt load gt library googlemaps The controllers prepare vectors of map settings and the list of places and possible controllers to zoom into each of the places and render a view named google map view e application views google map view php This is the rendered view that initially shows all the places on the Google Map and lets the user zoom in using a menu option to a listed zoom in location or go back to the view of all the places together on a zoom out map Let us assume the URI to the project root is http mydomain com myproject http mydomain com myproject gmaps M Q The source code is provided with this book via URLs The controller file The controller file controllers gmaps php will initially load the CI Google Maps library then set up the maps settings and the places to be marked and shown in different views the same view file is rendered with different data settings each time The controller will have the __construct and index methods in addition to set the zoom in on
127. login view php This view uses many of the CI form helper functions loaded by the auth controller When a user issues a submission the input is initially checked at the client side before issuing a submission call to application auth The following is the code lt DOCTYPE html gt lt meta http equiv Content type content text html charset utf 8 gt lt html gt lt head gt lt script src http code jquery com jquery latest js type text javascript gt lt script gt lt head gt lt body gt lt H1 gt Login Here lt H1 gt lt Building the login form using the form helper gt lt php Define the form attributes We will use the form helper auth will be called on submission only if check_if_valid will return true Ssubmit_attributes array onsubmit gt return check_if valid id gt auth echoform_open auth S submit attributes echo lt table gt s lt tr gt lt td gt The attributes of the lt input tag gt echoform_ label User Name echo lt td gt lt tds echoform_input array name gt user_name value gt echo lt td gt lt td gt The error message holders hidden by default echo lt label id user_name_err style color red display none gt name is too short lt label gt echo lt td gt lt tr gt lt tr gt lt td gt echoform_label Password echo lt td gt lt td gt echoform_password pa
128. lor blue decoration underline gt Show Me My Location lt 1li gt lt As Before gt Example 2 user feedback powered by AJAX and the jQuery UI In this example we will show how we can use the jQuery UI with AJAX to call a CI AJAX controller method to collect the user feedback and submit it without refreshing rendering a page We will reuse and expand the login example from Chapter 3 Usage and Scope of Controllers so if a user is logged in we will log the feedback with the user ID kept in the session while if not we will log it as anonymous user feedback Remember the following things e Username reg_user e Password 111111111 9 by 1s for regular user login The reused and extended resources are as follows e auth php No change here e ajax _handler php This is the new AJAX handler controller e users model php This is the extended user model e logged_in_view php This is the extended view for regular user login We expand the code to include the new Ajax_handler to keep the jQuery UI dialog submission of the browsing user feedback as well as get the user logged message via the AJAX asynchronous interface Note that we check in Ajax_handler to see whether the request is AJAX or not And if it s not we issue the following URL in the browser http photographersnav com ci_utils index php ajax_handler We will get a notification in the browser that this is a bad request 161 Views The
129. lt in CI controller that is something like an abstract class in the development scope so that in order to use the controller for our needs we must build our controller extending the base class We can extend the CI controller in several ways Loading resources of helpers models and libraries O Those can be from the CI built in repository third party developed or self developed For more information on how to self develop models refer to Chapter 6 Models on how to self develop libraries refer to Chapter 4 Libraries and on how to self develop helpers refer to Chapter 5 Helpers The controller can load any of the external resources in the following fashion in any of its methods commonly at the contractor and in case the resource is required in all the controllers via the autoload php configuration file refer to Chapter 3 Configurations and Naming Conventions for more information However for the best resource optimization to minimize the footprint and overhead even better the resources will be loaded only on those controller methods that need their services to operate The following are a few examples of how to load the mentioned resources Sthis gt load gt model some_model Sthis gt load gt library some_library keyed_array this gt load gt helper some_helper Adding public and private methods o This approach is the common guideline of PHP OOP that you are expected to be familiar with
130. me mysite com public_ html crontabs ci_crontab php ci_crontab php can execute the CI controller method http myCIproject mycontroller mymethod will for example scan the database and update a table named sums_table which contains the number of rows in all the tables added together after every execution Let us see an example of how to make that CI controller call from the PHP script In order to call a CI controller via an HTTP request ci_crontab php will use the cURL service that will call the CI controller similar to the way we issue it from a browser cURL Client URLs http php net manual en ref curl php Let s build home mysite com public html crontabs ci_crontab php Linux crontab will call every defined action repeatedly The code of ci_crontab php will be something like the following code lt PHP function file get contents curl url Sch curl init curl_setopt ch CURLOPT HEADER 0 curl_setopt ch CURLOPT RETURNTRANSFER 1 curl_setopt ch CURLOPT_URL url data curl_exec ch curl_close ch return data Shttp_to_execut http myCIproject mycontroller mymethod result file _get_contents_ curl S http_to_ execute The result will be the rendered output from the controller mostly simple echoed messages such as Processed 127 entries Sure we can log the result every time and append it to a log file of the action logs performed We just saw how we can use the CI cont
131. myproject mail We shall remember that in Codelgniter if you refer only to the controller URI path the CI will operate the function controller class index function if any In case the controller class does have index function And actually in any case the controller constructor will be called to create the class instance The controller file The controller file controller email php will initially load the CI mail library then it will configure the mail service properties such as from to e mail address subject HTML body and the attachment files Finally the controller will issue the e mail send service of the library getting back the operation completion status to report to the web user In case of a failure the controller will render a report for the reason of the failure with debugging information provided by the CI mail library 3 The helpers used are provided with the sample 3 source code provided with this book 52 Chapter 3 Since this controller example has several functions we shall review their usage initially before we review the code as follows __construct This contractor loads the CI e mail library to be used by other functions index This builds the e mail message and sends it to its destination doc_root_path This provides the directory path to find the e mail attachments to send Regarding the need to load resources such as libraries helpers and models the best practice
132. nable fetching your Geolocation ends checkLocation this is what happens if getCurrentPosition is successful functiongetLocation position latitude position coords latitude longitude position coords longitude Notify user for its location notifications html Your approx position latitude longitude notifications css color green Two seconds after the notification to user we have the location issue call to the controller to show it on the Google Map setTimeout show_on map 2000 this is what happens if getCurrentPosition is unsuccessful getCurrentPositionerrorCallback functionlocationFail notifications html Sorry your browser could not fetch your location notifications css color red lt script gt lt As Before gt lt Notification selector gt lt HR gt lt HR gt lt DIV style background lightgreen width 300px gt lt span id notifications gt lt span gt lt DIV gt lt HR gt lt HR gt 160 Chapter 7 lt ul gt lt Let the User Always Get Back to the default Zoom out with all places marked gt lt li gt lt php echo anchor index php gmaps lt B gt See All Locations lt B gt gt lt li gt lt If user clicks this one the Geo Location service will start gt lt li id getmylocation style cursor pointer co
133. name gt lt td gt lt td gt lt php echo S user gt user_email gt lt td gt lt tr gt lt php endforeach gt lt body gt lt html gt Example 4 interactive contact forms This example shows how to write a contact form using the CI form helper and the form_validation library For more information about libraries refer to Chapter 4 Libraries and for information about helpers refer to Chapter 5 Helpers The CI controller defines a form validation setup using the form_validation library and renders a form view that uses the form_validation library setup to apply a desired validation on the submitted data by the user If it s a success the CI controller will render a view page displaying a success message otherwise it will render the view page with the form and the error messages will be displayed The URI for this example is http ourdomain com index php contact In order to perform this example we shall build the following three scripts e The contact form controller class application controllers contact php e The view form script application views contactview php e The view success page script application views contactsuccess php 17 Getting Started The controller file The controller creates a form for adding and editing a product For more information refer to Chapter 7 Views The following is the code sample of the controller lt php class Contact extends CI Controller
134. nd will proceed with several usage examples covering different use cases that can be combined in a real project Scope of the Cl view The CI view is enabled with great flexibility to integrate the client side third party resources as well as accessing the CI resources of the CI libraries helpers and models This section will focus on the CI view syntax and usage guidelines as a preface to the coming usage code examples We can extend the CI library using the third party libraries from the CI echo system or develop our own libraries 144 Chapter 7 The Cl view resources path In a CI project the view files are located under the application views directory or any subdirectory to this path For example we can build under application views subdirectories for let s say two different view categories to improve the clarity of the file structure in our project The following screenshot shows the views location in a CI project directory tree JE CLPROJECT Name i Date modified Type Size licati d application BB admin p cache i f d almin d config I chat J controllers P compnents J core A J datot File folde Ji errors P dmz_htmlform File folder J helpers d headings File folde ab hooks a J kosher File folder a language S d pages File folder ad libraries J payments File folder di logs d photos_and_docs File fc J models i E k d prints File folder d third_party ae ee Ma d r
135. ne specific task with no dependence on other functions The chapter will elaborate on the CI helper s concept definition and usage with several examples The folder system helpers contains the CI system s built in helpers The folder application helpers contains all the additional helper files of CI helpers They can be third party helpers or created by the developer This chapter will primarily focus on e CI helpers scope and usage Usage categories Using a helper Adding a helper to the project Loading a helper Using helper methods e The available CI helpers Helpers e Examples Example 1 using a built in helper Example 2 using third party helpers SSL helper Example 3 building our own helper the my_download helper We will begin by briefly reviewing what a helper is in a CI framework and how we can use it for our needs across the project code resources Cl helpers scope and usage The CI helper does not have access to the controller resources by default unless CI and get_instance are called and used to access CI resources We can extend the CI helper using third party helpers from the CI system or we can develop our own helper Any application helper should be located under application helpers in the project directory The helper file must be in the following format application helpers lt HELPER_NAME gt helper php For example the SSL helper file should appear as applic
136. nfig Sconfig Sconfig Sconfig Sconfig dbprefix pconnect db debug cache_on cachedir char_set dbcollat swap_pre autoinit stricton vr 1 TRUE TRUE FALSE vi 1 utf8 utf 8 general _ci ri 1 TRUE FALSE Loading the database with the configuration manually this gt load gt database config Business logic Business logic is a set of validation rules and decision criteria defined for a certain information object topic or database object The model can apply business logic to the database and information objects that it handles In the case of a social network the model layer would take care of tasks such as saving user data saving friend associations storing and retrieving user photos finding new friends for suggestions and so on Object Relational Mapping ORM While CI provides the model class for the developer to expand for object oriented database CRUD Create Read Update and Delete validation and business logic for the defined project database there s another option that enables automatic model services In this section we will discuss Object Relational Mapping ORM ORM is anew concept of converting the database scheme definition into an object oriented database class API It provides database CRUD services on a given database so that the minimal code is required instead of the full model development More th
137. nload gt gt Download large file lt a gt lt div gt lt body gt lt html gt Summary In this chapter we have reviewed the CI helpers scope the different types of built in CI system helpers third party helpers and how to build our own helpers We also reviewed the steps to load and use helpers in our project Finally we saw several relevant usage examples as follows e Example 1 using build in helpers e Example 2 using third party helpers SSL helper e Example 3 building our own helper my_download helper 118 Models This chapter covers the CI models their role and their usage with several code examples of web applications The model is responsible for handling the database it stores and retrieves database objects used by the application from a database and contains the logic implemented by the application Much of the data that is part of the persistent state of the application whether that persistent state is stored in files or databases should reside in the model objects after the data is loaded into the application Because the model objects represent knowledge and expertise related to a specific topic they can be reused in the application The model represents the application data services and can serve the application logic commonly referred to as business logic as well Usually the model is responsible for the following operations e Adding modifying deleting and searching the applic
138. ns and Naming Conventions force _ssl A function in the ssl_helper for more see helpers chapter function smarty call a method smart_service in the loaded smart_handler library for more see Libraries chapter Sthis gt smart_handler ssmart_service paraml Sparam2 End Controller My_ handler We shall call the method that uses the helper as follows lt php echo base_url gt index php my_handler enforce_ssl For smarty method calls in the loaded library we use the following code lt php echo base_url gt index php my_handler smarty Passing parameters within a controller into a view application controllers my _controller php as follows Sarray array a gt 100 b gt 200 Sview_params array paraml gt hello world param2 gt Sarray i Sthis gt load_view my_view Sview_params In the view file at application views my_view php the view can use the provided parameters in the following method lt PHP echo param1 will echo hello world Note that within the controller it is defined as the param key array element where the array is sent to the view To get the param2 values we shall perform foreach S params2 as param echo param will echo 100 and 200 as params2 a and Sparams2 b values P gt 38 Chapter 2 Miscellaneous naming conventions CI guidelines have some general naming conventions such
139. o have helped with performance fclose file handle else die File Not Found End of file my_download_helper php Location application helpers my_ download_helper php The controller file The controller loads the helper my_download and calls its function download_ large files in order to enable the user to download large files that originally could not be downloaded using the my_download helper lt php class Classg2 extends CI_ Controller public function index Sthis gt load gt helper url Sthis gt load gt view classg2view function download Loading the helpers url my_download Sthis gt load gt helper array url my download FCPATH is a constant that Codeigniter sets which contains the absolute path to index php fullPath FCPATH files movie classg2 wmv Using the helper my_download function to download a very large file download_large files fullPath End of file classg2 php Location application controllers classg2 php 117 Helpers The view file The view file displays the data that contains a link for downloading the very large file lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Download large file lt title gt lt head gt lt body gt lt div id container gt lt a href lt php echo base_url classg2 dow
140. oglemaps php 71 Google Maps V3 API 71 H hello world example about 10 controller file 11 view file 12 helper building example about 114 controller file 117 helper file 115 view file 118 helper file 107 index function 52 index php file 9 installation Codelgniter 8 173 interactive contact forms example about 17 controller file 18 view file 19 J jQuery integration performing 153 jQuery UI using with AJAX 161 Jsmin php 71 L library file linkedin_handler php 88 license txt file 9 LinkedIn API wrapper about 85 authentication flowchart 86 controller file linkedinfo php 96 library file linkedin_handler php 88 requirements 86 view file linked company php 103 view file linked company updates php 104 view file linked connections php 102 view file linkedin me php 101 logged_in_view resource 162 login example about 55 56 controller file 56 controller file ajax_handler php 162 database file uploading 60 login_in_view view 63 model file 59 model file users_model php 163 view file logged_in_view php 165 login_in_view view 63 mail attachments example about 52 controller file 52 Main Resource Types Naming Rules about 30 Ajax call to an AJAX oriented method with arguments 36 37 controller and calc method calling with arguments 34 36 controller and calc method calling without arguments 34 controller definition naming rules 31 32 controller index method calling 33 helpers loading 37 38 l
141. on the get id selector to call the AJAX search service with the user typed SSN function get click function get_person_info SSN val name Phone err_sel pD lt script gt 151 Views The following is the portion of the HTML form itself lt form gt lt labels gt Enter SSN lt label gt lt input type text name SSN id SSN gt lt button id get gt Get Info lt button gt lt BR gt lt label gt Name lt label gt lt B id name gt lt B gt lt BR gt lt label gt phone lt label gt lt B id phone gt lt B gt lt form gt lt B id err_sel gt lt B gt Parser configuration issues The view is parsed by the CI parser before it is rendered back to the requesting browser The default syntax to echo a PHP parameter calculated expression value within the HTML tags is lt PHP echo trim param gt However CI provides automatic PHP short tag parsing support configuration at application config config php and at the configuration parameter Sconfig rewrite_short_tags TRUE If rewrite _short_tags is set to TRUE we can use the short tag of lt trim Sparam gt An important note on this is that in terms of debugging the non short regular PHP echo format is preferred as the short form errors might be more difficult to trace in this fashion However since this fashion is used in many code projects we ve seen we are mentioning it
142. or Yehuda Zadik Enable Simple LinkedIn API usage ESA class Linkedin_handler const LINKEDIN API URL https api linkedin com private Sapi_key private Ssecret_key private Son_failure_ url Oauth class public Soauth_consumer The url to return to from LinkedIn authorize url in our case is http mydomain com return_from_ provider 88 Chapter 4 private callback_ url The request token URL private Srequest_token_url LinkedIn authorize URL for getting the LinkedIn user confirmation for required permissions private Sauthorize path LinkedIn URL for getting the tokens to access the LinkedIn URL resources private Saccess_token_path accessory variable for accessing the LinkedIn resources private Sapi_url CI instance private cCI Set the class variables private function set _varaiables Sthis gt request_token_url self LINKEDIN API URL uas oauth requestToken Sthis gt authorize path self LINKEDIN API URL uas oauth authorize Sthis gt access token _path self LINKEDIN API URL uas oauth accessToken Sthis gt api_url array people gt http api linkedin com v1 people connections gt http api linkedin com v1 people connections companies gt http api linkedin com v1 companies Sthis gt CI amp get_instance Library constructor Initializes the library variabl
143. ote that in order to refer to a specific My handler class method named calc we concatenated calc after the class name Issuing this view from a browser we will get a result as follows 2 2 4 Why This is simply because we define default values in the receiving controller method So that if no parameters are sent as in this example the default ones will be used which are both set to 2 and hence the class calc method will output 4 Example 3 calling the controller and calc method with arguments In this example we enable the end user to call a specific class method with its parameters via the browser lt PHP echo anchor base_url index php my_handler calc 5 7 Press Me Cha hG gt Issuing this from a browser we will get 5 7 35 Why 34 Chapter 2 Since we provided 5 as the first parameter and 7 as the second using the CI URI naming convention of spectated to pass parameter values to a called controller class method Since we use the parameters as integers for multiplication PHP casts them as integers so we have 5 7 which is 35 Note that in order to call a specific controller method with parameters we add the separator after the method name followed by the parameters and each parameter is also separated by To understand this better see the following use cases and their meanings CI uses the URI as follows When issuing the URI lt PHP base _url gt controller_name met
144. per to integrate with the LinkedIn API in order to query the LinkedIn information from it There are several challenges in doing so one of which is to get the token to access the LinkedIn resources and access the data objects such as the following The LinkedIn user s details The LinkedIn user s connections 85 Libraries e The LinkedIn company s details e The LinkedIn company s updates Requirements e The PHP extension oauth library must be installed from http il1 php net manual en book oauth php e We shall register the application at LinkedIn Developers Network Homepage to receive the API key from http developer linkedin com This unique API key is required to identify our application in order to grant access from LinkedIn for responding to our API calls to their API Once we ve registered our LinkedIn app we will be provided with an API key and a secret key For the safety of our application we do not share our secret key For more information please refer to http developer 1linkedin com Authentication flowchart The following steps are required to authenticate our LinkedIn application to grant access We will refer to this project as LinkedIn app 1 The LinkedIn API client sends a request to LinkedIn The client sends the request to the LinkedIn request token URL at https api 1linkedin com uas oauth requestToken via the oauth object with a callback URL as a parameter to the LinkedIn API The
145. professional developers During November 2010 the Codelgniter development project was added to the well known GitHub community projects and got increasing interest and usage by developers worldwide as well as more and more third parties providing more add ons with a better maturity and functionality set There is a rising trend of web applications based on OOP object oriented programming frameworks using MVC Model View Controller development patterns described in the next section for developing advanced web applications Codelgniter is such a framework It seems that Codelgniter is continuously increasing its popularity as it has a simple yet high quality OOP core that enables great creativity reusability and code clarity naming conventions which are easy to expand user class extends CI class while more third party application plugins including views controllers models libraries helpers providing application oriented solutions such as CMS shopping carts or table grid navigators and add ons of libraries helpers are becoming available Preface The MVC concept is a development pattern or an application framework for a computer user interface that separates the representation of information from the user interacting with it MVC has been adopted as a successful architecture for web application developments The model consists of application data and provides services to manipulate them The controller handles business rules and
146. r function __ construct parent construct Standard libraries database amp helper URL loaded via the auto load if Sthis sinput gt is_ajax_request exit Bad Request ignored Your info has been logged for further investigation of attacking the site Chapter 7 Sthis gt load gt model users model functionsave user feedback Get the feedback content feedback this gt input gt post feedback Get if the user is logged in keep the user id Sthis gt users_model gt keep user feedback feedback functionget_user feedback log user Sthis gt users_model gt get_logged_in_user if user Suid Suser gt id Suser feedback rows Sthis gt users_model gt get_user_feedbacks Suid html foreach Suser feedback rows as row Shtml Srow gt timestamp lt B gt Srow gt feedback lt B gt lt BR gt result array result gt html echojson_encode result return End Ajax_handler The users_model php model file The model PHP file is located at application models users model php The code and inline explanations are as follows lt php if defined BASEPATH exit No direct script access allowed class Users model extends CI Model function __ construct parent construct functioncheck login user Spass No change here functionget_logged_in_user Wil
147. r login 55 AJAX 150 Ajax_handler 161 App Garden 76 application config folder 9 application controller auth php controller 55 application controllers folder 9 application helpers folder 10 application libraries folder 10 application models folder 9 application models users_model php 55 application views folder 9 application views logged_in_view php 55 application views login_view php 55 Asynchronous JavaScript and XML See AJAX attachments e mail sending with 52 authentication flowchart 86 B benefits CI libraries simplicity 65 built in helpers using 110 built in helpers example controller file 110 view file 111 Index built in libraries using 69 built in libraries example about 69 controller file 70 view file 71 business logic 122 business logic example about 132 controller file 132 model file 133 139 view file 136 140 C called controller method 143 CGI Common Gateway Interface 8 CI configurations defining 29 configurations using 29 miscellaneous naming conventions 39 CI controller extending 45 46 scope 43 use cases 46 48 CI formal resources 169 CI form helper about 148 functions 148 CI forums URL 109 CI helpers about 107 109 accessing 148 CI system helpers 109 CI third party helpers 109 scope 108 usage 108 CI libraries accessing 148 benefits 65 66 resources 68 reviewing 68 scope 67 usage 67 CI load library 146 CI load view library 147 CI model about 119 da
148. r objet based on the users table scheme We can set the variable to this object and use the operational services provided by ORM for actions such as save delete update and add new User gt username A new User gt password shhnewl1 gt email user mail com To add a new user record u gt save if saved we have a new echo New User Id Opened having u gt id User Id lt br gt else Show why we failed to save echo Su gt error gt string Getting the first three users from the database su u new User gt limit 3 gt get Showing the fetched users foreach u as Suser_rec echo User Id Suser_rec gt id lt br gt echo User Name Suser_rec gt username lt br gt echo User Email S user_rec gt email lt br gt Get the user with Uid 10 if any su new user Sseek uid 10 Su gt where id seek uid gt get Check if found if exist u echo User Id Su gt id Name is Su gt username lt br gt else echo No user found for user ID seek_uid lt br gt 124 Chapter 6 This is only a very simple usage example while ORM today provides a rich set of CRUD and validation services Please refer to the provided links to the featured ORM plugins for more information Example 1 a CRUD example In this example we will see how to use a CI model For th
149. r other library assets to their locations as mentioned before e Instantiating the library class via config autoload or instantiating it via the controller Automatically load a library my_1ib for the entire CI project Sautoload libraries array database my lib Specifically in certain controller s constructor s or method s Sthis gt load gt library my_lib e Using the library methods Sresult this gt smy_lib gt called_method Sparaml Sparam2 e Wecan see the library scope as the ultimate OOP reusability enabler for the entire project code resources models views helpers and libraries which govern all to address the execution requests from the user or a scheduled request 67 Libraries As mentioned the CI libraries enables us with great Separation and Simplicity For example the following code Library class class my handler private Smy_lib param Can t be accessed outside the class directly but we can provide the read only service as follows public function get_my lib param return this gt my_ lib param The following is a library function that can t be called from outside the class private function my private function Available Cl libraries CI and the CI echo system of developers provide many libraries covering a rich set of topics We will review the CI libraries as well as known resources for the third party CI libraries W
150. rapper 85 Requirements 86 Authentication flowchart 86 The linkedin_handler php library file 88 The linkedinfo php controller file 96 The linkedin me php view file 101 Summary 105 Chapter 5 Helpers 107 Cl helpers scope and usage 108 Available Cl helpers 109 Cl system helpers 109 Cl third party helpers 110 Example 1 using built in helpers 110 The controller file 110 The view file 111 Example 2 SSL helper 112 The helper file 112 The controller file 113 The view file 114 Example 3 building your own helper 114 The helper file 115 The controller file 117 The view file 118 Summary 118 Chapter 6 Models 119 Scope of the Cl model 120 The model resource path 120 Loading a model 120 Using model methods 121 Connecting to a database 121 Business logic 122 Object Relational Mapping ORM 122 ORM simple operations example 124 Example 1 a CRUD example 125 The controller file 126 The model file 129 The view file 131 ii Table of Contents Example 2 a business logic example 132 The controller file 132 The model file 133 The view file 136 Example 3 retrieving data from Facebook 136 The controller file 137 The model file 139 The view file 140 Summary 141 Chapter 7 Views 143 Scope of the Cl view 144 The Cl view resources path 145 The rendering flow 146 View flexibility 147 Accessing the libraries helpers 148 Forms 148 AJAX 149 Parser configuration issues 152 Integrating jQue
151. re gt lt br gt lt br gt echo Response SE gt lastResponse exit a This function returns a LinkedIn company three updates It returns a JSON string containing these values 95 Libraries param Integer company_id company id return json string String containing company s three updates public function company_updates company_id Sparams array Sheaders array method OAUTH HTTP METHOD GET Sapi_url this gt api_url companies Scompany_id updates start 0 amp count 3 amp format json try Request for a LinkedIn company s three updates Sthis gt oauth_consumer gt fetch Sapi_url S params method headers Receiving the last response with json containing company s three updates s_json this soauth_consumer gt sgetLastResponse return s_json catch OAuthException E echo lt pre gt var_dump this gt soauth_consumer echo lt pre gt lt br gt lt br gt echo Response SE gt lastResponse exit Class closing tags End of file linkedin php Location application libraries linkedin_handler php The linkedinfo php controller file The controller file application controllers linkedinfo php will load the LinkedIn API call its services and render a view to show the results The following is the controller code lt php if defined BASEPATH exit No direct scrip
152. rl http cnn com data url_ valid isValidURL url data url_exist isURLExists url Sthis gt load sview home_page view data publicfunction page b data array Smyqsl date 1970 01 01 dates_time_helper calls 49 Controller Usage and Scope data since ui_date Smyqsl_ date data past getAgeAccurate Smyqsl_ date Spercision 2 Sthis gt load sview page_b view data End controller The view file The controller file will prepare the current date and time to be shown in the home page views home_page_view php view lt DOCTYPE html gt lt meta content text html charset utf 8 gt lt PHP data from controller email email_valid url url_valid url_exist Svalidation_text email_valid Is Valid Is Not Valid Svalidation_url Surl_valid Is Valid Is Not Valid Sexist_url Surl_exist Exist Not exist gt lt body style text align left color blue gt lt H1 gt Main Page lt H1 gt lt HR gt lt HR gt lt div style float left gt The Email lt email gt lt validation text gt lt div gt lt div style clear both gt lt div gt lt HR gt lt HR gt lt div gt The url lt url gt lt Svalidation_url gt and lt Sexist_url gt lt anchor url visit the url array target gt _ blank title gt
153. rned to the browser For example let s take a look at a drop down selection example of a color pickup lt HTML gt lt PHP Sattr class nice field Soptions array Soptions 0 Blue Soptions 1 Green Soptions 2 Yellow default 1 echo form_dropdown color options default Sattr The form_dropdown helper will generate the following code lt select class nice field name color gt lt option value 0 gt Blue lt option gt lt option selected selected value 1 gt Green lt option gt lt option value 2 gt Yellow lt option gt lt select gt For more information refer to the CI form help user manual AJAX Asynchronous JavaScript and XML services AJAX http en wikipedia org wiki Ajax of JavaScript jQuery integration within a view is critical today in almost any web application It provides an advanced user experience by operating asynchronously in parallel to the user operations and updates only certain HTML selector portions and not the entire page as anon AJAX updates operate 149 Views AJAX has many use case examples to enhance the user experience The following are few common usage examples e Autocomplete while the user typing into a field all the matches found shown in a pop up list for the user to choose Without AJAX autocomplete the UI service is almost impossible e When submitting a form data entry using AJAX enables to issue the
154. roducts e application views product form php The view that contains the form The controller file The controller PHP file is located at application controllers product php The controller handles the product s operations such as adding editing updating and displaying the product s table The controller creates a form for adding and editing a product For more information refer to Chapter 7 Views The following are the code and inline explanations lt php if defined BASEPATH exit No direct script access allowed class Product extends CI_ Controller Accessory method for generating forms called by the methods add and edit private function load_form form_action S a_values array Loading the form helper Sthis gt load gt helper form Loading the form_validation library Sthis gt load gt library form_validation Sview_params form attributes array id gt productform Sview_params form action form action Sproduct_id isset a_values product_id Sa_values product_id 0 Sview_params form hidden_ fields array product_id gt Sproduct_id product name details Sview_params form product_name label array text gt Product name for gt product_name Sview_params form product_name field array name gt product_name id gt product_name value gt isset a_values product_nam
155. roject order GA The source code is provided with this book via URLs The controller file The controller PHP file is located at application controllers order php This controller is responsible for displaying the products and updates each product If the product s quantity reaches a limit it generates an error message The code and inline explanations are as follows lt php if defined BASEPATH exit No direct script access allowed class Order extends CI_Controller This method retrieves the products list and returns an array of objects each containing product details public function index Loading the url helper 132 Chapter 6 Sthis gt load gt helper url Manually loading the database Sthis gt load gt database Loading the model class Sthis gt load gt model productmodel1 view_params products Sthis gt productmodel sget_ products Sthis gt load sview orderview Sview_params This method checks the product s quantity It updates the product row in the database or generates an error message public function product product_id Loading the url helper Sthis gt load gt helper url Manually loading the database Sthis gt load gt database Loading the model class Sthis gt load gt model productmodel1 if this gt productmodel supdate quantity product_id mail Suser_
156. roller to serve Linux cron services which has a very powerful capability in many business cases 48 Chapter 3 Example 1 default homepage controller Initially we will start with a simple controller example that opens a home page with the navigation option back and forth to another page B and similarly to the home page We will do so while rendering some controller calculated data at the view This example doesn t use the database This example will be built from the following CI framework component configuration controller and view files Let us define the default controller filename as controller home_page php and the home page view as views home_page_view php Let us assume the URI to the project root is http mydomain com myproject The controller file The controller file home_page php will prepare some data to be shown in the view and will let the user navigate to page B and similarly back to the home page The helpers used are provided with the sample source code provided with this book The following is the code lt php class Home_page extends CI_ Controller function __construct parent construct Sthis gt load gt helper validators helper Sthis gt load gt helper dates time_helper public function index data array data email email the email com validators _helpercalls data email valid isValidEmail email data url u
157. rray of the resultant data The following is the code CodeIgniter Flickr API wrapper Library Class Enable Simple Flickr API usage package CodeIgniter category Libraries author Eli Orr Usage Via CI controller Sthis gt load slibrary flickr wrapper array api_key gt lt YOUR_FLICKR API _KEY gt DEFAULT RES gt 3000 filter 3000 pix GPS ENABLED gt FALSE j Sthis gt flickr wrapper gt set_ params keyed_array Srecent_photos Sthis gt flickr wrapper gt flickrPhotosGetRecent filter_ photos Sthis gt flickr_handler gt filter photos Sphotos_to_filter Suser_info this gt flickr_wrapper gt flickrUserInfo uid Suid e g 72095130 N00 PRIVATE We will use the following private functions private function _file_get_contents_curl url private function _flickrRestAPI Sparams private function _is_filtered_photo photo_rec EN N A A 77 Libraries The following is the Flickr_wrapper class that we are building class Flickr wrapper parameters as part of the library instance private SDEFAULT_ RES 2000 Width in Pixels private GPS ENABLED total shown photos private SRECENT PHOTOS 500 how many photos in each poll TRUE CI instance private CI Flickr api_key to use private Sapi_key function construct S params array Make sure we got the api
158. rs These are called by project resources such as controllers views libraries and models 2 J CLPROJECT_ROOT Al Name Date modified Type 4 B application N z p h htaccess 23 05 2013 03 46 HTACCESS File cache B 5 activate_user php 24 05 2013 20 52 PHP File Eau index html 23 05 2013 03 46 Firefox HTML Doc W controllers si v E test php 24 05 2013 21 37 PHP File aaa E welcome php 23 05 2013 03 46 PHP File I errors B helpers 0 hooks B language B libraries B logs B models B third_party B views B system B user_guide M logistics E m Controller definition naming rules Let s define the initial project controller to handle some basic services Note that the controller class name is My_handler and must reside in a file named my_handler php all lower case at application controllers in our CI project directory Here s the code sample with which to review the naming conventions class My handler extends CI Controller function _ construct Must Call Patent to get all its services inherited parent construct function index executed when referring to My _handlercontroller via URLecho Hello World function calc a 2 b 2 executed when referring to My _handler calc via URL echo Sa Sb a b functionAJAX calc I the request is not an AJAX we shall abort This is done by the 31 Configurations and Naming Conventions
159. ry or other client side libraries 152 Plugins for rendering view 153 Example 1 HTMLS5 location powered by Google Maps 156 The controller file 156 The view file 159 Example 2 user feedback powered by AJAX and the jQuery UI 161 The ajax_handler php controller file 162 The users_model php model file 163 The logged_in_view php view file 165 Summary 168 Appendix 169 Index 171 iv Preface This book aims to teach you how to develop web applications efficiently with the Ellis Labs Codelgniter platform The Codelgniter platform is an object oriented Model View Controller development platform For more on MVC please refer to http en wikipedia org wiki Model view controller The reader of this book is expected to be familiar with at least the PHP programming language specifically with PHP OOP object oriented programming and its usage as well as with MySQL Codelgniter referred to as Cl in this book is an Application Development Framework a toolkit for people who build websites and web applications using PHP Codelgniter is a smart application development skeleton framework with flexible and expandable core powered high performance and low footprint The Codelgniter framework OSL 3 0 open source license developed and maintained by Ellis Labs powers an echo system of developers across the globe The first public version of Codelgniter was released on February 28 2006 It got very good feedback from web application
160. s techniques and extended libraries 1 Learn expert CodeIgniter techniques and move beyond the realms of the User Guide 2 Create mini applications that teach you a technique and allow you to easily build extras on top of them 3 Create Codelgniter Libraries to minimize code bloat and allow for easy transitions across multiple projects Codelgniter 1 7 ISBN 978 1 84719 948 5 Paperback 300 pages Improve your PHP coding productivity with the free compact open source MVC Codelgniter framework 1 Clear structured tutorial on working with Codelgniter for rapid PHP application development 2 Careful explanation of the basic concepts of Codelgniter and its MVC architecture 3 Use Codelgniter with databases HTML forms files images sessions and email Please check www PacktPub com for information on our titles PACKT PUBLISHING Ext JS 4 Web Application Development Cookbook Ext JS 4 Web Application Development Cookbook ISBN 978 1 84951 686 0 Paperback 488 pages Over 110 easy to follow recipes backed up with real life examples walking you through the basic Ext JS features to advanced application design using Sencha s Ext JS 1 Learn how to build Rich Internet Applications with the latest version of the Ext JS framework in a cookbook style 2 From creating forms to theming your interface you will learn the building blocks for developing the perfect web application 3 Easy to follow recipes st
161. s For example a library can provide Facebook library API services to simplify the application code for Facebook integration Chapter 4 Libraries will elaborate on the CI libraries concept definition and usage with several usage examples system This is the root of the CodeIgniter core directory The system folder contains important system components in the subfolders such as core database helpers built in system helpers and libraries built in system libraries easier if we don t Q Do not edit any of these files Upgrading is much Example 1 hello world Initially we will start with a simple example that displays Hello World on the rendered web page This is an example that doesn t use a database The URI will be http ourdomain com index php hello We can eliminate the index php file from the path to enable a shorter URI that is http ourdomain com index php hello In order to enable these shorter URIs we will make configuration changes as described in Chapter 2 Configurations and Naming Conventions regarding the config php index_page setting in config php We will build the following two scripts Controller class application controllers hello php View script application views helloview php 10 Chapter 1 In this example we use the default configuration For more information about configurations refer to Chapter 2 Configurations and Naming Conventions The controller in this e
162. s configuration The session key Sconfig encryption_key This encryption_key must be set with a key in order to use the session class services For example Sconfig encryption_key cMGy4DSwGUvxYSar4279626Hg0n2342efrwerr2TE2RF4G3reg4tF3et An example of the session library usage within a controller and setting a session variable is as follows Suid 119 where uid is the id of the loggeing user Sthis gt session gt set_userdata this_user_id Suid Getting the session variable in another controller is as follows Suid Sthis gt session gt userdata this user _id The session data storage mechanism is as follows Sconfig sess use database FALSE If the recommended configuration is set to TRUE we would use many session parameters of a large size stored in the associated default database 26 Chapter 2 Session expiration timeout in seconds Sconfig sess expiration 7200 The number of seconds the session will be kept Additional session configuration parameters can be found in the CI user manual Cross site scripting XSS filtering activation deactivation Sconfig global_xss filtering FALSE This will enable XSS filtering on URI requests sent to the application Note that all URI requests are processed initially by the root index php to analyze the URI request and issue the proper CI calls If set to TRUE it will protect URI requests from XSS type malicious attac
163. s com myciapp showme class mybutton gt Press Me lt a gt lt where Suri myapps com myciapp showme text Press Me Shtml_attributes class mybutton gt Back to our example the view code part that enables the user to call the defined controller will look like the following the PHP portions are with other HTML tags in a view file lt PHP echo anchor ase_url index php my_handler Press Me A 2 gt 33 Configurations and Naming Conventions Since we only referred to the class name its constructor and index method if defined will be executed In case we did not define an amp index method for this my_handler controller the preceding calls GA will only instantiate the class using its constructor definition and if the index method was defined it will be called as well In our case the index method was defined so it will be called as well Example 2 calling the controller and calc method without arguments In this example we enable the end user to call a specific class method but without parameters so that the default method parameters must be used via the browser Note that in order to use any CI helper function we need to make sure that it is either autoloaded or specifically loaded in the controller for the controller method s usage or rendered views library or model lt PHP echo anchor base_url index php my_handler calc Press Me B gt N
164. s example will be constructed from the following helpers e application helpers ssl_helper php The CI helper for SSL that implements SSL on the links e application controllers helpersslexamp1le php This controller loads the helper and implements SSL on the links The helper is loaded in the constructor this gt load gt helper ssl e application views helper ssl view php This is the rendered view that SSL is implemented on Let us assume the URI to the project root is http mydomain com myproject http mydomain com myproject helpersslexample Q The source code is provided with this book via URLs The helper file This CI helper file implements the services described in the preceding section This helper uses the built in CI URL library and URL helper using the redirect CI URL helper function lt php if defined BASEPATH exit No direct script access allowed if function_exists force_ssl function force_ssl get the CI instance to access the CI resources SCI amp get_instance Change the base_url to have https prefix CI gt config gt config base_url str_replace http https CI sconfig gt config base_url if _SERVER SERVER_PORT 443 redirect CI to use https URI so that CI suri gt uri_string return the current URI with https prefix redirect CI suri suri_string 112 Chapter 5 if function_exists remo
165. server submission to store or process the date and show the result only in a specific selector notification massage instead of refreshing the whole page as form with the action submission requires Format Bullet e When browsing many information pages called pagination and clicking on a certain page number to view AJAX enables rendering the selected page to view within a selection DIV in the whole HTML page without refreshing the whole page Format Bullet End Currently AJAX is becoming an essential view component mostly enabled via the popular jQuery library which makes it easy to use AJAX is an extremely valuable Ul asset for building smart and interactive views For example the following is an example of the AJAX service that for a given SSN Social Security Number provides the person s name and phone number in the defined selectors if found or alerts if else Whenever the user is clicking on the Get Info button an AJAX call is triggered and an asynchronous AJAX call to an AJAX controller is sent with the SSN to get the person s record When the response is returned if the SSN was found the phone and name of the person will be updated Otherwise a notification will be provided that the SSN person s record was not found The following is the code implementing the process described previously where the AJAX call is the heart of the operation lt script type text javascript gt function get person info SSN val n
166. ses is a good UX User Experience practice In this case the user let s say will know that on the top they will have a certain main navigation area on the right certain status info and operational shortcuts and so on We can define one or more layouts so that each page layout template will have its region s organization Each region is commonly defined within a DIV Having several template layouts we can initially choose the proper layout we want to use and then we will load its region content using the CI views defined for each region For example let s say we want to have a certain layout named default The default template s main layout will be named template for example using the view file main_template php so that main_template php will include the following regions e header upper navigation e content e footer We shall perform the following configurations at application config template php This is not the CI built in plugin but an additional plugin with the GA library and configuration file and other assets we have installed to our CI project The default template shall be defined as follow Note more templates can be defined in the same fashion Stemplate default template main_template Stemplate default regions array header upper navigation content footer The main_template refers to application views main_ template php The content of main_template php will
167. ssion gt userdata oauth_token_secret initialize oauth consumer with Soauth_token Soauth_token_secret Sthis gt setToken Soauth_token Soauth_token_secret Start the process of getting oauth token amp oauth token secret so that the user redirects to a LinkedIn UI permission conformation window 90 Chapter 4 public function auth Start communication with the LinkedIn server Srequest_token_ response S this gt getRequestToken Sthis gt CI gt session gt set_userdata oauth_token_secret Srequest_token_response oauth_token_secret Get the token for the LinkedIn authorization url Soauth_token request_token_response oauth_token Slog message yuda auth getRequestToken oauth_ token Soauth_token Slog_message oauth_token_ secret Srequest_token_response oauth_token_secret n log_message debug Slog message Redirect to the LinkedIn authorization url for getting permissions for the app header Location Sthis gt generateAuthorizeUrl Soauth_token This is the method called after returning from the LinkedIn authorization URL The returned values from the LinkedIn authorization URL are oauth_token oauth_token_secret oauth_verifier Those values are used to retrieve oauth_token oauth_token_secret for accessing the LinkedIn resources public function on_success if this gt CI
168. ssword echo lt td gt lt td gt The error message holders hidden by default echo lt label id password_err style color red display none gt password is too short lt label gt 61 Controller Usage and Scope echo lt td gt lt tr gt echo lt table gt The submit button echo form_input array type gt submit value gt Login echoform_close gt lt HR gt lt HR gt lt Server Credentials failure message gt lt p style color red gt lt php echo msg gt lt p gt lt body gt lt Local JavaScript service gt lt script type text javascript gt functioncheck if valid var submit true varuser name name user_name val var password name password val if user_name length lt 2 user name_err show submit false else user name_err hide if password length lt 6 password_err show submit false else password_err hide return submit lt script gt lt html gt The login_in_view view file The login_in_view view is rendered following a successful login by either application auth admin_main_menu controller method or application auth user_main_menu method base on the user category with the info of the logged in user Both the controllers uses the users_model model to validate the login attempt and fetch the logged in user The
169. t lt tr gt lt tr gt lt td gt lt a href lt php echo site url example2 more 1 2 3 gt gt Example2 lt a gt lt td gt lt tr gt lt table gt lt body gt lt html gt Example 3 building your own helper This example uses a helper to download a very large file of 200 MB which can t be downloaded in one file reading This example will be constructed from the following helpers application helpers my_download_helper php This is the CI helper that is used to download a very large file application controllers classg2 php This is the controller that uses the my_download helper application views classg2view php This is the view that has a link for the file download Let us assume that the URI to the project root is http mydomain com myproject Hence the URI to execute the auth controller for login will be http mydomain com myproject classg2 114 Chapter 5 al The source code is provided with this book via URLs The helper file This helper is used to download very large files which can t be downloaded in one file reading Its function download_large_files reads 1 MB in each loop until it downloads the whole file lt php if defined BASEPATH exit No direct script access allowed CodeIgniter Download Helpers package CodeIgniter subpackage Helpers category Helpers author Yehuda Zadik 17 Download large files
170. t form attributes gt lt table gt lt tr gt A td gt lt php echo form label form contact_name label text form contact_name label for gt lt td gt td gt lt php echo form _input form contact_name field gt lt td gt lt tr gt lt tr gt A 19 Getting Started lt td gt lt php echo form_label form contact_email label text Sform contact_email label for gt lt td gt lt td gt lt php echo form_input form contact_email field gt lt td gt lt tr gt lt tr gt lt td gt lt php echo form label form contact_message label text Sform contact_message label for gt lt td gt lt td gt lt php echo form _textarea form contact_message field gt lt td gt lt tr gt lt tr gt lt td colspan 3 gt lt php echo form submit mysubmit Send gt lt td gt lt tr gt lt table gt lt php echo form_close gt lt body gt lt html gt The following is the corresponding rendered success view lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt titlesContact sent lt title gt lt head gt lt body gt lt div id container gt lt div id body gt lt p gt lt php echo message gt lt p gt lt div gt lt div gt lt body gt lt html gt Summary In this chapter we have reviewed the CI
171. t lt div gt lt p gt lt a href lt php echo site_url gt gt Back to Menu lt a gt lt p gt lt div gt lt table gt 104 Chapter 4 lt php foreach Supdates as Supdate gt lt tr gt lt td gt lt ul gt lt php foreach Supdate as key gt S val gt lt li gt lt php echo key gt lt php echo val gt lt li gt lt php endforeach gt lt ul gt lt td gt lt tr gt lt php endforeach gt lt table gt lt body gt lt html gt Summary In this chapter we have reviewed the CI libraries scope the different types of built in CI echo system third party libraries and how to build our own libraries We also reviewed the steps to load and use the library resources in our project Eventually we created several usage examples 105 Helpers This chapter covers the CI helpers topic the different types of helpers and their different usage categories with several code examples of web applications CI provides us with built in helpers enables us to integrate third party helpers and enables us to develop new helpers and share them with the community if we wish to The CI helpers are powering CI efficiency and code reusability by enabling all other CI controllers using the same code instead of defining a helper function locally A helper file is a collection of independent procedural functions in a particular category Each helper function performs o
172. t PHP echo Smyval gt lt H1 gt Later the PHP view file will be executed so that the HTML generated code will be as follows lt H1 gt Hello lt H1 gt The entire PHP view file that is rendered including the PHP executions will generate the view HTML file that will be returned to the browser via HTTP to be executed locally View flexibility CI provides the flexibility for the PHP view file code to use any client side JavaScript CSS HTML or other JavaScript libraries in the view files without any requirement to declare them at the server side controller as it occurs in some other platforms Furthermore the CI view can access any other CI resources such as the CI libraries the CI models or the CI helpers as if it were the rendering controller of the view for example accessing a session parameter directly Sparam S this gt session gt userdata paraml p 147 Views Also the CI view can call a CI library method directly in the same fashion as the rendering controller does assuming the rendering controller loads this library Scalc this gt my_lib gt my_lib calc Sparam lt H1l gt lt PHP echo calc gt lt H1 gt Accessing the libraries helpers As mentioned earlier the CI PHP view file can access any of the CI resources such as calling the CI helpers libraries or models in the same way the controller does The following is a more elaborated and complete example of a PHP CI vi
173. t F F FF http example com index php welcome index 110 Chapter 5 or Since this controller is set as the default controller in config routes php it s displayed at http example com F FF So any other public methods not prefixed with an underscore will map to index php welcome lt method_name gt see http codeigniter com user_ guide general urls html public function index Loading the url helper Sthis gt load gt helper url this gt load gt view helper examplel view End of file helperexamplel php Location application controllers helperexamplel The view file The view file calls the URL helper function site_url Since the controller loaded the URL helper it s recognized by the view lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Menu lt title gt lt head gt lt body gt lt table gt lt tr gt lt td gt lt a href lt php echo site url welcome gt gt Welcome lt a gt lt td gt lt tr gt lt tr gt lt td gt lt a href lt php echo site url example2 more 1 2 3 gt gt Example2 lt a gt lt td gt lt tr gt lt table gt lt body gt lt html gt 111 Helpers Example 2 SSL helper In this example we will use the CI third party SSL helper to enforce an https or HTTP URI request and response between CI and the browser Thi
174. t access allowed k k The controller is loading our developed library 96 Chapter 4 LinkedIn wrapper Next the following process will occur in the loaded library 1 get oauth token amp oauth token secret so that the user will be redirected to a LinkedIn UI permission conformation window to approve our requested permission 2 If user confirms the permissions we requested the method onSuccess is called with the oauth token amp oauth token secret as GET parameters The tokens will be stored as session parameters Else we cannot proceed querying LinkedIn and the onFailure FF FF Now we can access the LinkedIn resources using the retrieved tokens Here are the methods that query LinkedIn resources me Get the Info of the User who confirmed the permissions connections Get the preceding user connection records JSON formatted company We just gave an example how to retrieve any company by company id we got from the results or query company id by company id or search criteria company updates Let us get the latest updates of this company class Linkedinfo extends CI_Controller array of LinkedIn configuration variables private S linkedin_config callback url from the LinkedIn authorization URL private callback url Controller constructor Checks if session variables are set oauth_token oauth_token_secret I
175. t all controllers models can be loaded using the autoload mechanism Chapter 2 Configurations and Naming Conventions discusses how to autoload the libraries Once instantiated by the autoload or controller constructor the libraries can be used by the controller methods or by rendered views In addition any model helper or another library may use our project installed libraries using sget_instance as described in the previous chapters The libraries power the code of the CI model view controller instantiated components for more information visit the website http en wikipedia org wiki Model view controller regarding the functionality expansion and reusability across the project controllers models helpers and views This chapter will primarily focus on the following topics e The Cl libraries scope and usage Usage categories Using a library Adding a library to the project Instantiating a library Using library method s e Available CI libraries e Examples Example 1 using the built in libraries Example 2 using third party libraries such as the Google Maps CI library wrapper Example 3 building our own library such as the Flickr API wrapper Example 4 building our own library such as the LinkedIn API wrapper We will begin by briefly reviewing what a library in a CI framework is and how we can use it for our needs across the project code resources 66 Chapter 4
176. t td gt GPS Filter lt td gt lt td gt lt settings GPS_ENABLED With GPS With Without GPS gt lt td gt lt tr gt lt p gt lt For each photo show the User name how many photos they took 84 Chapter 4 till now the original size in MP Mega Pixels of the photos and the Time stamp when the photo was taken by the camera mostly loaded days or even weeks months later lt table border 1 style background color 009900 gt lt tr gt lt th gt User Uploaded lt th gt lt thsUser photos Count lt th gt lt th gt Photo ID lt th gt lt th gt Original Size MP lt th gt lt th gt Was Taken lt th gt lt tr gt The class code continues as follows lt PHP foreach photos as photo get the owner id Suid S photo owner Get User Info Suser_ info this gt flickr_wrapper gt flickrUserInfo Suid Sphotos number format Suser_info photos Smp_res int photo o width S photo o height 1000000 1 gt lt tr gt lt td gt lt Sphoto ownername gt lt td gt lt td gt lt Sphotos gt lt td gt lt td gt lt Sphoto id gt lt td gt lt td gt lt Smp_res gt lt td gt lt td gt lt Sphoto datetaken gt lt td gt lt tr gt lt PHP gt lt table gt lt div gt lt body gt lt html gt Example 4 the LinkedIn API wrapper In this example we will build the CI Library wrap
177. tPub com Do you need instant solutions to your IT questions PacktLib is Packt s online digital book library Here you can access read and search across Packt s entire library of books Why Subscribe e Fully searchable across every book published by Packt e Copy and paste print and bookmark content e On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www Packt Pub com you can use this to access PacktLib today and view nine entirely free books Simply use your login credentials for immediate access Table of Contents Preface 1 Chapter 1 Getting Started 7 Installing Codelgniter 8 Folders overview 9 Mandatory components 9 Example 1 hello world 10 The controller file 11 The view file 12 Example 2 passing the complex parameters to a view 12 The controller file 13 The view file 14 Example 3 the database query by a model rendering results to a view 14 The controller file 15 The model file 16 The view file 16 Example 4 interactive contact forms 17 The controller file 18 The view file 19 Summary 20 Chapter 2 Configurations and Naming Conventions 21 Cl directory tree 22 config php 22 database php 27 routes php 29 Defining and using your own configurations 29 Understanding and using Cl naming conventions 30 The main resource type naming rules 30 Controller definition naming rules 31 Table of Contents Example 1 calling
178. tabase connection 121 loading 120 121 operations 119 CI model scope about 120 business logic 122 model loading 120 121 model methods using 121 model resource path 120 CI naming conventions about 30 Main Resource Types Naming Rules 30 CI parser 152 CI PHP style guide URL 39 CI plugins Agile toolkit for Codelgniter developers 170 Ajax library wrapper for Codelgniter 170 Cl based CMS PyroCMS 169 CI Formlgniter 170 CI GoCart 169 CI grid plugin 169 CI PayPal Library 169 CI sparks 169 CI Wiki 170 Codelgniter Google Maps V3 API Library 169 Facebook PHP SDK and CodeIgniter 169 CI resource PHP view file 146 CI Sparks URL 68 CI system helpers 109 CI third party helpers 109 CI view scope about 144 AJAX 149 client side libraries integrating 152 flexibility 147 flow rendering 146 forms 148 libraries helpers accessing 148 parser configuration issues 152 resources path 145 146 classic directory tree structure CodeIgniter about 22 config php 22 27 database php 27 28 routes php 29 client side integration performing 152 153 Club 3D URL 170 Codelgniter articles 170 classic directory tree structure 22 installing 8 sites 170 CodeIgniter template integrating with third party plugins 153 154 Codelgniter template class URL 153 complex parameters passing to view 12 complex parameters example controller file 13 view file 14 config php file 22 27 controller definition naming rules 31 32 controller file linkedinfo php
179. ter Hello World title gt Welcome to Codegniter message gt Hello World ig this gt load gt view helloview view params 11 Getting Started closing the class definition End of file welcome php Location application controllers welcome php You can download the example code files for all Packt books you xy have purchased from your account at http www packtpub com Q If you purchased this book elsewhere you can visit http www packtpub com support and register to have the files e mailed directly to you The view file The following is the corresponding rendered view that uses the parameters provided by the controller to render the view to the web page and return it to the user lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt lt php echo mega_title gt lt title gt lt head gt lt body gt lt div id container gt lt hil gt lt php echo title gt lt h1 gt lt div id body gt lt p gt lt php echo message gt lt p gt lt div gt lt div gt lt body gt lt html gt Example 2 passing the complex parameters to a view In this example we will show you how to pass and use complex parameters such as arrays and object arrays from the CI controller to the rendered CI view to be used in the view You can pass any number of arrays as parameters to a view you can also pass objects
180. th or without providing parameters that the view code may use for its operation e Using existing general reusable resources can be loaded from any controller or model and reused by rendered views as well of CI helpers and libraries and defining new CI helpers and libraries e How tos dos and don ts for locating files and naming are categorized based on the defined controllers models libraries and helpers e Relations between the defined class resource name containing file name loading a defined class a helper or a model instantiating and calling a calls method via the URI and calling a class method with parameters The main resource type naming rules CI defines one class one file standard so that every class of a CI controller extension and CI model extension of a library class resides in one file This also applies to helpers that are a set of functions Each resource category controller library model and view will be located in a specific directory or its subdirectory The most commonly used resource categories are as follows e Controllers These get the client side for example browser to operate e Views These are rendered by the controller and returned to the browser via HTTP 30 Chapter 2 Libraries These are called by project resources such as controllers views models and helpers e Models These are called by project resources such as controllers views libraries and helpers e Helpe
181. that you autoload it in application config autoload php In that case the scope of the model is in all the CI project and will refer to application models mymodel php Sautoload model array users mymodel Using model methods Once the CI model is loaded we will access the model functions using an object with the model name as our class The model s method is called for performing database operations such as retrieving inserting and updating data from the database Loading the model mymodel in the controller s method Sthis gt load gt model mymodel1 Calling the model s method my_function Sthis gt mymodel gt my_function For example let s load the model users and access its function get_users Loading the model class Sthis gt load gt model usermodel Calling the model to retrieve the users from the database Sview_params users Sthis susermodel gt get_users Connecting to a database For more information refer to Chapter 2 Configurations and Naming Conventions In this example we will connect manually to a database The following settings are done in application config database php Sconfig hostname 127 0 0 1 Sconfig username db username Sconfig password db password Sconfig database db database Sconfig port db port Sconfig dbdriver mysql 121 Models Sconfig Sconfig Sconfig Sconfig Sconfig Sco
182. the CI model The CI model provides services for all the application modules to access the application database s or external information resources in an OOP fashion Typically the model classes will contain functions that help us retrieve insert and update information in the database This section will focus on the CI model syntax and usage guidelines as a preface to the following usage code examples The model resource path The model files are located in the folder application mode1s in the pattern application models lt MODEL NAME gt php Loading a model Loading a model can be done automatically or via the controller More specifically it can be done in a certain controller s constructor or any controller s method e If the model is used in a few of the controller s methods it s recommended that you load the model in those methods The scope of the model in that case is only in those methods project and will refer to application models mymodel php 120 Chapter 6 e If the model is used in most of the controller s methods it s recommended that you load the model in the controller s constructor In that case the scope of the model is in all the controller s methods project and will refer to application models mymodel php Sthis gt load gt model mymodel1 It automatically loads a model mymode1 for all the CI projects e If the model is used in most of CI s project controllers it is recommended
183. the defined places lt php Use The Google Maps CI Library Wrapper for several marked places altogether and zoom in class Gmaps extends CI_Controller function __ construct 72 Chapter 4 parent construct Sthis gt load gt library googlemaps Set the map window sizes Sconfig map_width 1000px map window width Sconfig map_height 1000px map window height Sthis gt googlemaps gt initialize config function index Initialize and setup Google Maps for our App starting with 3 marked places London UK Bombai India Rehovot Israel Initialize our map for this use case of show 3 places altogether let the zoom be automatically decided by Google for showing the several places on one view Sconfig zoom auto Sthis gt googlemaps gt initialize config Define the places we want to see marked on Google Map Sthis gt add_visual_flag London UK Sthis gt add_visual_ flag Bombai India Sthis gt add_visual_flag Rehovot Israel j data this gt load_map_ setting Load our view passing the map data that has just been created Sthis gt load sview google map _view data The class Gmaps continued with several more functions as follows function london Initialize our map Here you can also pass in additional parameters for customizing the map see the following
184. tion address or website name immediately so that we can pursue a remedy Please contact us at copyright packtpub com with a link to the suspected pirated material We appreciate your help in protecting our authors and our ability to bring you valuable content 5 Preface Questions You can contact us at quest ions packtpub comif you are having a problem with any aspect of the book and we will do our best to address it 6 Getting Started This chapter covers the basics of the CI development framework and its usage by reviewing some fundamental web application examples We will start with a basic hello world example and move to an interactive contact form integration with a database We will construct the CI applications by following a step by step method Throughout this chapter we need to remember that the CI development framework is an MVC based development architecture for more information refer to the Wikipedia definition at http en wikipedia org wiki Model view controller This chapter will primarily focus on the following topics e The CI project directory tree framework e Configurations routing and autoloading are covered in this chapter while the other issues are covered in Chapter 2 Configurations and Naming Conventions e Example 1 hello world e Example 2 passing parameters to a view e Example 3 the database query by a model rendering results to a view e Example 4 interactive
185. tion and usage will be covered in Chapter 2 Configurations and Naming Conventions e application views This folder contains all the view files A view is the HTML content executed by the user browser that presents and interacts with the user A view can be a webpage or an RSS page The following components are not mandatory but are highly recommended e application model1s This folder contains all the project model files A model is the component of the MVC design architecture which handles the data stored in the database A model in CI is a PHP class that is designed to work with the information in the database Chapter 6 Models will elaborate on the CI models concept definition and usage with several usage examples 9 Getting Started application helpers This folder contains all the additional helper files to the CI helpers They can be third party or created by the developer A helper file is a collection of independent procedural functions in a particular category Each helper function performs one specific task with no dependence on other functions Chapter 5 Helpers will elaborate on the CI helpers concept definition and usage with several usage examples application libraries This folder contains all the libraries of the CI application project created by the developer A CI library is technically a PHP class The scope of the library can be any project resource such as helpers models controllers and view
186. ules Sconfig rules array array field gt contact_name label gt Contact Name rules gt trim required array field gt contact_email label gt Contact Email rules gt trim required valid_email Sthis gt form_validation gt set_rules config rules Sthis gt form_validation gt set_rules contact_message Contact Message trim required Validating the form 18 Chapter 1 if this gt form_validation srun FALSE failed for Sindex 0 Sindex lt count a_fields index s_ field a_fields index if form_error s_ field Sview_params form s field field class error Sthis gt load sview contactview Sview_params else Validation succeeded Ssuccess params array message gt Success Sthis gt load gt view contactsuccess success params End of file welcome php Location application controllers welcome php The view file The view file displays the contact form for receiving data from the user The following is the corresponding rendered form view lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Form Example lt title gt lt head gt lt body gt lt php if validation_errors gt lt php echo validation_errors gt lt php endif gt lt php echo form_open contac
187. updateContent gt companyJobUpdate gt job gt siteJobRequest gt surl Load the view for displaying the LinkedIn company gt s updates Sview_params lt updates gt a_updates Sthis gt load gt view lt linked company updates gt Sview_params class closing tags End of the file linkedinfo php Location application controllers linkedinfo php The linkedin me php view file This view file displays the LinkedIn user s details The following is the view code lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt My Details lt title gt lt head gt 101 Libraries lt body gt lt table gt lt tr gt lt td gt Full Name lt td gt lt td gt lt php echo my details gt firstName Smy_details gt lastName gt lt td gt lt tr gt lt tr gt lt tdsTitle lt td gt lt td gt lt php echo my_details gt headline gt lt td gt lt tr gt lt tr gt lt td gt My LinkedIn profile lt td gt lt td gt lt a href lt php echo profile url gt target _blank gt Link lt a gt lt td gt lt tr gt lt table gt lt div gt lt p gt lt a href lt php echo site_url lt gt gt gt Back to Menu lt a gt lt p gt lt div gt lt body gt lt html gt The view file linked connections php This view file displays the LinkedIn user s connections The following is the view cod
188. users_model resource is expanded to provide a few more services which are as follows get_logged_in_user This function is used to return the logged in user record if logged in or NULL otherwise get_user_rec uid to get a specific user record based on his her ID keep_user_feedback feedback This function is used to keep the user feedback in the database with its user ID if logged in get_user_ feedbacks Suid This function get all the user feedback messages save till now in the database as an array of the database objects Each feedback database row returned have the feedback message and its timestamp formatted as HTML and returned via the JSON format back to the AJAX caller to be shown to the end user via the jQuery selector based HTML rendering for example selector html The_html_item_returned_ from_server The logged_in_view resource is expanded to provide the user with the new services as follows Add a new feedback button which when clicked pops up a jQuery UI dialog for this purpose Show the feedback log button which when clicked shows a scrollable list of the user feedback Now let us review the source code itself The ajax_handler php controller file The controller PHP file is located at application controllers ajax handler php The code and inline explanations are as follows lt php if defined BASEPATH exit No direct script access allowed class Ajax handler extends CI Controlle
189. ustomer support Now that you are the proud owner of a Packt book we have a number of things to help you to get the most from your purchase Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http www packtpub com If you purchased this book elsewhere you can visit http www packtpub com support and register to have the files e mailed directly to you Errata Although we have taken every care to ensure the accuracy of our content mistakes do happen If you find a mistake in one of our books maybe a mistake in the text or the code we would be grateful if you would report this to us By doing so you can save other readers from frustration and help us improve subsequent versions of this book If you find any errata please report them by visiting http www packtpub com support selecting your book clicking on the errata submission form link and entering the details of your errata Once your errata are verified your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title Piracy Piracy of copyright material on the Internet is an ongoing problem across all media At Packt we take the protection of our copyright and licenses very seriously If you come across any illegal copies of our works in any form on the Internet please provide us with the loca
190. ve_ssl function remove_ssl SCI amp get_instance Change the base_url to have http prefix CI sconfig gt config base_url str_replace https http CI sconfig gt config base_url If _SERVER SERVER_PORT 80 redirect CI to use http URI so that CI suri gt uri_string return the current URI with http prefix redirect CI suri suri_string The controller file Now we will see how the controller loads the SSL helper and calls its function force_ssl to enforce the HTTPS URI request and response with the browser class Helpersslexample extends CI Controller public function _construct parent construct Loading the ssl helper Sthis gt load gt helper ssl Enforce URI request of https force _ssl Index Page for this controller public function index Sthis gt load gt helper url Sthis gt load gt view helper ssl view End of file helpersslexample php Location application controllers helpersslexample 113 Helpers The view file The view file code is as follows lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset utf 8 gt lt title gt Menu lt title gt lt head gt lt body gt lt table gt lt tr gt lt td gt lt a href lt php echo site _url welcome gt gt Welcome You are using https lt a gt lt td g
191. w_buffer Sthis gt load sview sectionA view_ file Sparams TRUE Note that the third parameter s value is set to TRUE the default value is set to FALSE and echoes the view to the standard output in the case of the controller rendering this means it will be returned as an HTTP response to the browser issuing the request from the controller The preceding example refers to the following view file application views sectionA view_file php The rendering flow The view is rendered by the controller The controller provides the parameters to the rendered PHP view file to use them The controller uses the following built in CI load library this gt load gt view my_ view data 146 Chapter 7 Otherwise the controller uses the third party rendering service library In this chapter we will use such a library The CI controller rendering is done by the CI load view library and that optionally accepts the data of parameters and objects that the rendered PHP view file can use See the following code for example Sdata myval Hello Sthis gt load gt view my view S data The rendered PHP view file application views my view php uses the data parameters provided via the load library as follows lt H1 gt lt PHP echo myval gt lt H1 gt Note that the controller defines the data as follows Sdata myval Hello While the usage at the PHP view file rendered will be as follows lt H1 gt l
192. we will collect the geolocation and call a controller method to prepare the Google Map of that area to render a Google Map view with the option navigator We will use the HTML5 navigator geolocation service as follows navigator geolocation getCurrentPosition getLocation locationFail Here getLocation is called if the location was successfully fetched and locationFail if it was failed We will start with the controller first The controller file The controller PHP file is located at application controllers gmaps php The following is the controller code based on Chapter 4 Libraries for the extended Google Maps API integration example where the new parts of code are highlighted class Gmaps extends CI_Controller Extends the CI controller to be our Gmaps controller powered by the Google API wrapper library Setting the initialization parameters of Google Maps Library Mapper for the window size where the user interaction with Google Maps created window will occur private user lon 0 private Suser lat 0 function __ construct O parent construct 156 Chapter 7 Sthis gt load gt library googlemaps Set the map window sizes S config map_width 1000px map window width Sconfig map_height 1000px map window height Sthis gt googlemaps gt initialize config function index Initialize and setup Google Maps for our App starting wit
193. will be located under application controller in the project directory The controller can load other CI project code resources of libraries models and helpers so that they can be accessed directly by the rendered views This means that if a controller loaded a library the rendered view PHP file can call the library function in the exact same way as the controller does The following is the code resources that the controller can load e application helpers The helper s are built in CI third party or user defined e application models The models are most commonly user defined for the specific database s and tables of the specific project extending the built in CI model Wrapping with CRUD service for specific defined database s table s but also can be third party for example data mapper extensions that can be used generically with any database e application libraries The library can be built in third party or user defined The library is an Object Oriented PHP class based service that can provide some reusable services related to a specific project or across many projects For example as Flickr Facebook or LinkedIn wrapper API libraries A good practice is to define in addition to the third party libraries we may decide to use our project oriented libraries to enhance our project simplicity and maintainability 44 Chapter 3 Extending the Cl controller As we said our application controller extends the bui
194. ws foreach q gt result as row Sfeedbacks row i return S feedbacks End Users_model The logged_in_view php view file The PHP view file is located at application views logged_in view php This file was extended with several more services as described in the previous examples The code and inline explanations are as follows lt DOCTYPE html gt lt meta http equiv Content type content text html charset utf 8 gt lt html gt lt head gt lt script src http code jquery com jquery latest js type text javascript gt lt script gt lt scriptsrce http code jquery com jquery 1 8 2 js gt lt script gt lt script src http code jquery com ui 1 9 0 jquery ui js gt lt script gt lt link rel stylesheet type text css href lt base _url gt css my_style css media screen gt lt script type text javascript gt The AJAX handler controller method URLs varsave_user feedback submitter lt site_url gt index php ajax_handler save_user_feedback varget_user feedbacks lt site_ url gt index php ajax_handler get_user_ feedback _log functionajax_save_ user feedback feedback ajax type POST url save_user_ feedback submitter data feedback feedback dataType json success function data When AJAX return back alert Your feedback Updated Thanks 165 Views functionajax_g
195. xample passes the parameters that are displayed in the view al E Passing the parameters from the controller to the view is optional The controller file Here s the code sample of the controller The controller is responsible for rendering the view with the parameters such as mega title and message For naming the controller classes refer to Chapter 2 Configurations and Naming Conventions lt php class Hello extends CI_Controller Index Page for this controller Maps to the following URL http example com index php hello or http example com index php hello index 2 6 r lt since this controller is set as the default controller in config routes php it s displayed at http example com So any other public methods not prefixed with an underscore will map to index php welcome lt method_name gt see http codeigniter com user_guide general urls html public function index Note that view_ params is optional we can use this gt load gt view helloview as well if the view doesn t use php variables The view_params is extracted in the view script to php variables key value In this example three variables will be generated by CI in the view page helloview php variable mega_title value Codeigniter Hello World variable title value Welcome to Codegniter variable message value Hello World Sview_params array mega_title gt Codeigni

Download Pdf Manuals

image

Related Search

Related Contents

COAL INDIA LIMITED  Understanding the CY2291, CY2292, and CY2295    MANUAL DE USUARIO TRi  UR44 Operation Manual  Spectracide HG-96017-1 Instructions / Assembly    User`s Manual for SHAPE - Light Reflector Design Software  

Copyright © All rights reserved.
Failed to retrieve file