Last Updated: 14 Oct 2023
|
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
backend-tech:codeigniter-basics [Jul 21, 2011 07:18 AM] dordal created |
backend-tech:codeigniter-basics [Oct 14, 2023 04:43 AM] dordal old revision restored (Nov 21, 2020 10:39 PM) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | = CodeIgniter Basics = | ||
+ | [[http:// | ||
+ | |||
+ | == CodeIgniter Structure == | ||
+ | |||
+ | When you download CI, you'll find a couple folders & files: | ||
+ | |||
+ | * **application**, | ||
+ | * **system**, which contains all of CI's core libraries and scaffolding code. | ||
+ | * **user_guide** and **license.txt**, | ||
+ | * **index.php**, | ||
+ | |||
+ | Inside the application folder, there are several sub-folders. Key items include: | ||
+ | |||
+ | * **config**, which has site-specific configuration variables | ||
+ | * **controllers**, | ||
+ | * **errors**, with common error pages | ||
+ | * **models**, which contain your application specific data models (user, product, etc.) | ||
+ | * **views**, which contains the presentation logic for your app | ||
+ | |||
+ | == Writing Models, Views & Controllers | ||
+ | |||
+ | The controllers contain your business logic, and in the MVC world generally pull data from models and send it to views. | ||
+ | |||
+ | <code language=php> | ||
+ | <?php if ( ! defined(' | ||
+ | |||
+ | /** | ||
+ | * MyApp: default controller for my application | ||
+ | * | ||
+ | * @author David Ordal [david -at- ordal.com] | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | class MyApp extends CI_Controller { | ||
+ | |||
+ | /* | ||
+ | * index(): primary index function; default action for CI | ||
+ | * | ||
+ | */ | ||
+ | public function index() | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * displayHello(): | ||
+ | * | ||
+ | */ | ||
+ | public function displayHello() | ||
+ | { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | |||
+ | $data[' | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The views are your presentation layer, and contain HTML code for display. | ||
+ | |||
+ | <code language=php> | ||
+ | <!-- file name: / | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | |||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | The models represent your data, and generally interact with your database. | ||
+ | |||
+ | <code language=php> | ||
+ | <?php if ( ! defined(' | ||
+ | |||
+ | /** | ||
+ | * UserModel: basic model class | ||
+ | * | ||
+ | * @author David Ordal [david -at- ordal.com] | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | class UserModel extends CI_Model { | ||
+ | |||
+ | private $_name; | ||
+ | |||
+ | function __construct() | ||
+ | { | ||
+ | // Call the Model constructor | ||
+ | parent:: | ||
+ | } | ||
+ | | ||
+ | function getName() | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | function setName($name) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == Accessing the Database == | ||
+ | |||
+ | Models like the above aren't terribly useful. What you really want to do is access your database to load and save data from your models. CodeIgniter allows you to do this yourself, or via its (somewhat loose) [[http:// | ||
+ | |||
+ | Best practice seems to be to use the active record class; most CI developers I interviewed were doing that. | ||
+ | |||
+ | == Best Practices for Models == | ||
+ | |||
+ | There' | ||
+ | < | ||
+ | function AddUser($insertData) | ||
+ | function UpdateUser($userId, | ||
+ | function DeleteUser($userId) | ||
+ | function GetUsers($limit, | ||
+ | </ | ||
+ | |||
+ | The CodeIgniter docs aren't really clear on the point; their [[http:// | ||
+ | |||
+ | ==== Model Validation ==== | ||
+ | |||
+ | The question of data validation comes up: in a traditional model, you'd validate incoming data via the setters. With CI, best practice actually seems to be to not check it at all, because you [[http:// | ||
+ | |||
+ | Shawn McCool has an interesting take on this, [[http:// | ||
+ | |||
+ | ==== Validating Result Sets ==== | ||
+ | |||
+ | The next big question is how to validate result sets coming back from the database. If you make a query and don't get anything back, you probably need to do something about that. In the real world, you'd probably throw an exception and then optionally catch that exception if its OK that you didn't get any results back. With CI, you can't really do that, so you have three options: | ||
+ | |||
+ | - Return a set of default values if you don't find a record in the database. | ||
+ | - Use CI's show_error function to display a ' | ||
+ | - Return the empty set, and make your controller logic handle the error. This is what you'll probably have to do in most cases, and it just means a lot of extra code in your controllers. | ||
+ | |||
+ | == Best Practices for Controllers == | ||
+ | |||
+ | CI generally recommends that all data validation be done initially in the controller, which makes some sense. I recommend you do it when you're passing in user generated data (e.g. on a reg form) and need to display a pretty error message. I don't think its as critical if you're passing in computer generated data (e.g. the ID number of something in your database) because that should be validated at the model level. | ||
+ | |||
+ | |||
+ | == Best Practices for Views == | ||
+ | |||
+ | Your views will probably contain some standard stuff, such as a default header & footer. CodeIgniter allows you to queue a series of views up to be loaded, so you can do something like this: | ||
+ | <code language=php> | ||
+ | function index() { | ||
+ | $data[' | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Using Helpers == | ||
+ | |||
+ | Helpers are pretty much as they sound; a set of functions that help you do something specific. CodeIgniter comes with a bunch of built-in helpers in ''/ | ||
+ | < | ||
+ | $this-> | ||
+ | </ | ||
+ | |||
+ | Helpers do all sorts of things; some popular ones: | ||
+ | |||
+ | * **[[http:// | ||
+ | * **[[http:// | ||
+ | * **[[http:// | ||
+ | |||
+ | == Using Libraries == | ||
+ | |||
+ | Libraries are like helpers, but are a little bit more 'full featured' | ||
+ | |||
+ | Libraries are loaded in the standard way: | ||
+ | < | ||
+ | $this-> | ||
+ | </ | ||
+ | |||
+ | == Securing CodeIgniter == | ||
+ | |||
+ | One of the best practices from a security standpoint is to keep your PHP code outside of the 'web root', which is served by apache. I do this with CI: | ||
+ | |||
+ | * I move my '' | ||
+ | * I move all my images/ |