best practices and whatnot by calvin froedge nashville codeigniter
Post on 18-Jan-2017
216 Views
Preview:
TRANSCRIPT
CodeIgniterbest practices and whatnot
by Calvin Froedge
Nashville CodeIgniter / Expression Engine Meetup Group
Thursday, March 8, 2012
Who Be I?
Web developer, 8 years
CodeIgniter user, 2 years
Likes running
Polyglot at heart (always learning new languages)
Still learning
Thursday, March 8, 2012
General Architecture
The CI core is a mix of procedural functions, static variables and the Singleton pattern.
The first thing CI does on a request is loads Common.php. This contains function that the rest of the core files depend on.
Thursday, March 8, 2012
General Architecture
Hooks are super useful, perhaps underused feature. They are one of the first things CI checks for (also one of the last), with several checks in between. Use them to “wire” your CI app in cool ways (one example is performance tuning).
The URI class mainly servers to operate on the $_SERVER array.
Thursday, March 8, 2012
General Architecture
CodeIgniter’s &load_class function adds Singleton instances to a static array, $_classes. These instances persist for the duration of a given request.
References are created to these classes in CI_Controller. This is how CodeIgniter has implemented $this->whatever which you are familiar with.
Thursday, March 8, 2012
General Architecture
Thursday, March 8, 2012
General Architecture
When you use $var = &get_instance() you’re making a reference to CI_Controller.
The database class extends either the active record class or the db driver class. The DB function returns that class instance.
Thursday, March 8, 2012
General Architecture
Thursday, March 8, 2012
General Architecture
A security check is done and if everything is cool and there is no reason for a 404, the controller gets loaded.
Finally, the controller is called.
The output class sets headers, helps with caching and sends final output to the browser.
Thursday, March 8, 2012
General Architecture
Thursday, March 8, 2012
General Architecture
Thursday, March 8, 2012
App Design Philosophy
Don’t repeat yourself! (ie, Keep it DRY)
Keep controllers lean.
Write as little HTML as possible.
Thursday, March 8, 2012
ControllerHierarchy
An intelligent way to reduce code is to share functionality among controllers using base controllers. You can create as many of these as you want. To the right is a typical implementation
Thursday, March 8, 2012
Tables
Write your own views to generate tables or use CodeIgniter’s table class. DON’T write them manually!
Thursday, March 8, 2012
Pagination, Calendars,
other Common
Stuff
Make a custom view or use the helpers / libraries (or both!). Reuse, reuse, reuse!
Thursday, March 8, 2012
Nav
Works for two level navs, could be improved upon to do things in a more flexible way, and possibly handled via a library.
Thursday, March 8, 2012
Message Helpers (Needs, Work, You Get the Idea)
Thursday, March 8, 2012
App Design Philosophy
Don’t reinvent the CI wheel unless there’s a good reason to. When you do, don’t hack the core.
You can put more in your models than you may think.
Make sure you have a good understanding of all the tools at your disposal.
Thursday, March 8, 2012
Custom Base
Model
My_Model can do some awesome things to help you reduce the amount of code you need to put in your other models.
Thursday, March 8, 2012
Thursday, March 8, 2012
Thursday, March 8, 2012
Extending Core Classes
MY_Model can save you lots of time.
Base Controllers are AWESOME.
You can nearly totally rewrite core classes if you want to.
Thursday, March 8, 2012
Make it Work How You Want
I changed the routing up in one app just a bit to let me load different versions of the app based on URI segments.
Thursday, March 8, 2012
Databases!
CodeIgniter has really great database support these days. Around a dozen supported.
MongoDB, Redis, CouchDB, others not in core but there are good libraries for them (Joel Cox, Alex Bilbie, Others)
Thursday, March 8, 2012
Forms
Let’s face it, form helpers kind of suck.
One approach I like is creating your own views for forms. This may not be a good appraoch for really large forms (then, you may want to write a helper) simply because of all the extra includes.
Thursday, March 8, 2012
Thursday, March 8, 2012
Thursday, March 8, 2012
HMVCHierarchal MVC.
Creates separate MVC instances which have endpoints. You can use them like widgets or in place of controllers.
Modular CI from WanWizard
Modular Extensions from wiredesignz
Warning: can be buggy.
Thursday, March 8, 2012
What HMVC Does
Thursday, March 8, 2012
What HMVC Does
Thursday, March 8, 2012
What HMVC Might
Look Like
Thursday, March 8, 2012
Sparks, Third Party
Sparks is the package manager for CI. Use it. Runs from the command line. Created by Kenny Katzgrau and John Crepezzi.
Third party packages are an easy way to drop complete resource sets into CodeIgniter, including code that isn’t meant for CodeIgniter (then you can just write a quick library on top of it).
Thursday, March 8, 2012
Thursday, March 8, 2012
You Haz the Ajax?Try to do things with progressive enhancement - make Javascript improve your CodeIgniter app, not the other way around.
The best way I’ve found to work with JavaScript is to accept and return JSON from controllers (or pass it to models) using the same endpoints you use without JavaScript. You can use is_ajax_request() from the Input class to check for ajax requests.
Thursday, March 8, 2012
Handling AJAX Errors
Thursday, March 8, 2012
DRY JS as well!
Thursday, March 8, 2012
You Don’t Have to Couple
Your JS and PHP!
Thursday, March 8, 2012
Version Control
Still using SVN? Stop.
Git is easy as hell to use. I keep all of my open source stuff on Github and host everything else in my own repositories via Gitolite.
Branching, submodules super important in large projects.
Thursday, March 8, 2012
Miscellaneous
Try to follow the style guide. No camel casing in CI apps!
Documentation is cool, but well written code is even cooler.
Throw code away often to learn as much as you can. Rewriting / refactoring is a good thing.
Thursday, March 8, 2012
Testing
Unit testing - one piece at a time
Writing a test suite
Behavior driven development
You can do them all
The main thing is killing bugs
Thursday, March 8, 2012
Questions?
Whatever you can think of.
Let’s look at some more code?
Thursday, March 8, 2012
top related