codeigniter - calvin froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf ·...

41
CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression Engine Meetup Group Thursday, March 8, 2012

Upload: truongque

Post on 01-Nov-2018

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

CodeIgniterbest practices and whatnot

by Calvin Froedge

Nashville CodeIgniter / Expression Engine Meetup Group

Thursday, March 8, 2012

Page 2: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 3: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 4: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 5: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 6: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

General Architecture

Thursday, March 8, 2012

Page 7: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 8: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

General Architecture

Thursday, March 8, 2012

Page 9: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 10: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

General Architecture

Thursday, March 8, 2012

Page 11: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

General Architecture

Thursday, March 8, 2012

Page 12: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

App Design Philosophy

Don’t repeat yourself! (ie, Keep it DRY)

Keep controllers lean.

Write as little HTML as possible.

Thursday, March 8, 2012

Page 13: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 14: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Tables

Write your own views to generate tables or use CodeIgniter’s table class. DON’T write them manually!

Thursday, March 8, 2012

Page 15: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Pagination, Calendars,

other Common

Stuff

Make a custom view or use the helpers / libraries (or both!). Reuse, reuse, reuse!

Thursday, March 8, 2012

Page 16: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 17: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Message Helpers (Needs, Work, You Get the Idea)

Thursday, March 8, 2012

Page 18: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 19: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 20: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Thursday, March 8, 2012

Page 21: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Thursday, March 8, 2012

Page 22: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 23: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 24: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 25: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 26: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Thursday, March 8, 2012

Page 27: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Thursday, March 8, 2012

Page 28: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 29: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

What HMVC Does

Thursday, March 8, 2012

Page 30: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

What HMVC Does

Thursday, March 8, 2012

Page 31: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

What HMVC Might

Look Like

Thursday, March 8, 2012

Page 32: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 33: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Thursday, March 8, 2012

Page 34: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 35: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Handling AJAX Errors

Thursday, March 8, 2012

Page 36: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

DRY JS as well!

Thursday, March 8, 2012

Page 37: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

You Don’t Have to Couple

Your JS and PHP!

Thursday, March 8, 2012

Page 38: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 39: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 40: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

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

Page 41: CodeIgniter - Calvin Froedgecalvinfroedge.com/slide-decks/codeigniter-ee-nashville-03-12.pdf · CodeIgniter best practices and whatnot by Calvin Froedge Nashville CodeIgniter / Expression

Questions?

Whatever you can think of.

Let’s look at some more code?

Thursday, March 8, 2012