Introduction
This book is for you–the developer–who needs to get a quick answer to “How can I wire this up?”.
It’s also for you–the designer or entrepreneur–who has been tasked with building out the skeleton of a new application while you wait for funding to get a hired gun in to take care of the hard-core specifics of the app.
It’s not meant as a replacement for the detailed user guide. It won’t go into all of the nitty-gritty about each part of the framework. Instead, it is intended to be a practical “field guide” that you reference when you need ideas about how to do things, structure things, or where to put things. It tackles a number of questions that have shown up over and over again in the forums or on StackOverflow, and provides clear, actionable solutions you can start working with today.
If you’re an experienced software architect who revels in discovering the perfect way to structure your application, and follows all the best practices you learned in school, you might be disappointed. That’s not me. Personally, I find that many of the things that you’re taught in school are wonderful ways to organize code for maintainability and clarity. But I also think you need to take it all with a grain of salt and not simply jump on the latest bandwagon.
Many of the techniques that have come to be best practices in the PHP community over recent years were originally conceived in a software environment where the code was compiled. Once compiled, it performed very speedily and everyone was happy. PHP isn’t a compiled language, though, and those design decisions can, if not carefully used, result in quite slow code when run through interpreted languages like PHP. Yes, I know you can offset that in a server environment with proper caching, proxy caches, and yada yada. That comes with its own cost, though. The more layers you add on top of PHP, the more complex the overall system becomes, and the harder the system as a whole is to maintain. If ease of maintenance was the whole reason we started architecting our apps in that way, it becomes self-defeating at times.
The best answer, in my eyes, is to keep it simple wherever you can. Keep it practical. That doesn’t mean abandoning all hope of a structured, easy to maintain application, though. Far from it.
This book isn’t here to sell you on the gospel of “My Way”. It’s here to provide quick solutions and inspiration. You should take the knowledge you get here and bend it and reshape it in the way that works best for you and your organization.
The Book At A Glance
If you need to know a solution right now then this is a great place to start, since what follows is a very quick overview of what each chapter covers. Hopefully, it can point you in the right direction quickly.
Introduction
You are here. See the red ‘X’ on the CodeIgniter treasure map? Yup. That’s you.
Chapter 1: Where Does This Go?
While everyone seems to have a slightly different variation of MVC definitions, this chapter will start the book off by describing how I like to think about the different file types CodeIgniter provides: models, controllers, views, libraries, and helpers (Oh, My!), and why they work best that way. It also briefly touches on how to map your application into controllers for easiest maintenance.
Chapter 2: Environmental Protection Codes
This chapter will examine the all-too-underused capabilities of Environments. I’ll show you how to set them up for maximum flexibility and ease-of-use, and we’ll discuss why they are so necessary. Especially when working in teams and using version control.
Chapter 3: Controlling Traffic
You’ve already learned what should and should not live in a controller, so here I’ll show you how to create multiple controllers to keep your code DRY1 and separate different areas of your application. We’ll show you how to load them both with, and without, one of the common HMVC2 solutions.
Chapter 4: Showing Your Work
This chapter digs into working with views and view data. Once you understand the basics, I’ll show you the way that EllisLab intended the views to work. Next, I’ll show you a simplified version of my convention-based theme system I borrowed from Rails years ago. From there, we’ll explore the parser and how to integrate other template engines, particularly Twig.
Chapter 5: Working With Data
In this chapter, we’ll dig deep into using the database. Everything from basic setup to using multiple databases to separate read and write queries. We’ll look at setting up a basic MY_Model to provide utility functions to increase flexibility and productivity. We’ll take a look at how to validate data in the model, how to use the migration system, and even how to create our own Seeder.
Chapter 6: AJAX is Easy
I often see questions about working with AJAX in the forums, so this chapter provides the necessary knowledge to integrate AJAX into your applications with ease. We’ll use the Output class to ensure our data types are sent correctly, build some utility methods into our MY_Controller, and explore some simple jQuery setup and utilities.
Chapter 7: Working With Files
We’ll look at the fabled multiple-file upload problem that plagues the forums. We’ll also explore using FTP with some real-world examples, and working with remote files quickly and easily.
Chapter 8: Multiple Applications
In this chapter, I’ll show you how to use CodeIgniter’s multiple application setup to address several application needs, like separating out sections of the site for server needs or simple desires, and even how to share common code between different applications, or an API and Admin area.
Chapter 9: Security
In this chapter, we’ll scan through a number of topics that are crucial to understand if you wish to maximize the security of your application. We will make sure you understand when and why to use tools like the built-in CSRF and XSS protection, how and when to filter and sanitize your data, and more.
Chapter 10: Performance
Here we examine a number of things we can do to help increase the performance of our application, some obvious and some maybe not-so-obvious. We will cover several caching strategies and the types of caching available to you, database system tweaks for production environments, and even when and where to use CodeIgniter’s autoloading features for the best performance.
Chapter 11: Fun At The Terminal
While CodeIgniter has always seemed to shun the terminal, there are times you can’t get away from it. We’ll explore how to work with CodeIgniter from the command line, the basics of creating CLI scripts of your own, and even how to make a simple, but flexible, cronjob runner.
Chapter 12: Composing Your Application
You’ve probably heard of Composer and how it’s shaking up the way PHP is written and shared. This chapter starts by exploring how to use it within your application to simplify using some of the many high-quality packages available. We will look at codeigniter-installers and how to use it to share your CodeIgniter-specific code. Then we’ll take a look at how to use Composer to provide a completely different way of working with CodeIgniter than what you’re used to: a way that is more flexible, powerful, and more future-proof.