Book Image

Programming with CodeIgniter MVC

Book Image

Programming with CodeIgniter MVC

Overview of this book

The CodeIgniter Model-View-Controller framework provides genius simplicity, flexibility, and efficient resource usage, boosting performance and reusability. "Programming with CodeIgniter MVC" reviews the unique features of CodeIgniter using simple, easy-to-follow, and practical examples. Programming with CodeIgniter MVC provides a simple step-by-step guide to efficiently utilizingthe CodeIgniter MVC framework for developing web applications. This book is packed with engaging examples to understand the usage of controllers, libraries, and (Codeigniter) CI Models. This book commences with a quick discussion of the CodeIgniter Integration with  external plugins such as Flickr API, Google Maps and more will be reviewed with clear usage examples. It will then cover CI naming convention rules, mandatory and optional configurations, and usage within a CI project. It will also cover user defined configurations. By the end of this book, you will not only understand user-defined libraries in a CI framework, but also their services, role, usage, and scope with the help of an example-based approach. The book also covers helpers, models, and views, as well as their usage. Using this book, youwill soonbe able to develop feature-rich web applications using the CodeIgniter MVC framework. "Programming with CodeIgniter MVC" is a one-stop solution to developing solutions with CodeIgniter MVC.
Table of Contents (15 chapters)
Programming with CodeIgniter MVC
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Appendix
Index

Preface

This book aims to teach you how to develop web applications efficiently with the Ellis Labs CodeIgniter platform. The CodeIgniter 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.

CodeIgniter (referred to as CI in this book) is an Application Development Framework, a toolkit for people who build websites and web applications using PHP. CodeIgniter is a smart application development skeleton framework, with flexible and expandable core powered high performance and low footprint. The CodeIgniter 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 CodeIgniter was released on February 28, 2006. It got very good feedback from web application professional developers. During November 2010, the CodeIgniter 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. CodeIgniter is such a framework. It seems that CodeIgniter 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.

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 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, CodeIgniter is found to have excellent results.

To sum up in terms of flexibility, code reusability, light infrastructure, enabling developer creativity, code clarity, highest performance, minimal footprint, and fast learning curve, CodeIgniter 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 CodeIgniter 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.

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 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 CodeIgniter'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.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => 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 => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => 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".

Note

Warnings or important notes appear in a box like this.

Tip

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 , and mention the book title through the subject of your message.

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.

Customer 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 location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.