Greg Aker

Entries Tagged with: codeigniter

Know what ORMs and Query Builders do!

Filed in: ExpressionEngine, CodeIgniter, Django

September 2, 2011

ORMs and Query Builders such as the Django ORM and CodeIgniter Active Record classes are great time savers. But you can get yourself into performance trouble if you're not paying attention to what these are doing behind the scenes. I think the biggest mistake people make is they use tools like this as crutches for not understanding how to write simple SQL statements. Don't fall into this group! If you're in it, work hard to get out!

I'll show a couple of examples from both to illustrate how you can give your applications a performance boost ...

Read More

CodeIgniter Conference 2011 Roundup

Filed in: CodeIgniter, Blobbing

August 22, 2011

It's 3:30am, and I'm waiting to take a car to the airport after my weekend in New York for the CICon 2011 Conference. To help trick my body into not wanting to sleep, I decided to do a write up about my weekend experiences.

First, hats off to Adam Fairholm, Kenny Katzgrau and Phil Sturgeon with the the great job they did organizing the conference. I'm extremely pleased and impressed with the amazing venue, accommodations, speakers and overall experience they provided for us.

I was honored to announce to the community two things I think are ...

Read More

var_dump($this) in EE/CI with no recursion

Filed in: PHP, ExpressionEngine, CodeIgniter

August 18, 2011

Here's a quick little tip for all you EE addon developers out there. I've seen some people moaning on Twitter about how a var_dump() can be hard on the browser due to the recursion in the CodeIgniter singleton. So check how I like to roll in my CI libraries/EE addons.

<?php

class Foo {

    protected $_foo;

    public function __construct()
    {
        $this->_foo = $this->config->item('something');
    }
}

Look maw, no $this->CI

Did you know you you can do this in a CodeIgniter library or an ExpressionEngine mcp/mod/extension/plugin/accessory file? It's pretty easy to accomplish, and ...

Read More

DB Results in ExpressionEngine and CodeIgniter

Filed in: ExpressionEngine, CodeIgniter

June 9, 2011

I learned PHP by forcing myself to try to create ExpressionEngine addons. I studied the 1.x code. I poured over the CodeIgniter core, trying to figure out how the heck it worked. One thing that confused me was how the heck do I get a single result from an active record query! If you're in this boat, I hope this might help clear it up for you a bit.

In ExpressionEngine 2.x, unless you're doing something extremely complex, using the CodeIgniter Active Record class is the way to go.

For instance, if we want to do ...

Read More

What is xss_clean in CodeIgniter, and why should I use it?

and just as importantly, when should I not use it

Filed in: PHP, CodeIgniter, Security

March 30, 2011

If you're new to CodeIgniter, you've probably seen xss_clean() mentioned in the docs. When I was first starting with PHP and CodeIgniter, every time I saw xss_clean() mentioned, I'd think to myself "what the hell is that?!" So I hope I can show you what it is, why it's important to use and when you shouldn't use it.

What's XSS?

If you haven't heard the term XSS before, read over at Wikipedia about Cross Site Scripting.

A really quick example of an XSS attack would be to build a script like:

<?php

echo ...

Read More

Automagic CodeIgniter base_url

Filed in: CodeIgniter, php

March 29, 2011

I posted this on Forrst a while back, but I thought I'd post it here. Forrst seems cool, but I honestly rarely use that service as it's yet another login to remember, and finding things if you're not logged in seems hard...but I digress.

In CodeIgniter Core projects, I like a 'dynamic' base_url, and it's easy to accomplish in your config.php file with the following code.

<?php
// Chop of the DOCUMENT_ROOT part of the $path.  This will
// Give us everything after http://example.com/
$path = ltrim(str_replace(rtrim($_SERVER['DOCUMENT_ROOT'], '/'), '',
                    rtrim(FCPATH, '/')) . '/', '/');

// Running HTTPS ...

Read More

Extending CodeIgniter's Controller

Filed in: PHP, CodeIgniter

March 18, 2011

I think it's safe to assume that I'm not in the minority when I say that I learned to program with CodeIgniter. I started in mid-2007 learning HTML/CSS while learning ExpressionEngine. Within a year or so, CodeIgniter seemed to be the logic step on what to learn, so I dove in head first. Lisa Wess gives me grief to this day for my forum post asking for someone to explain to me how the exp:weblog:entries tag worked. Her answer was correctly, "go look at the code." :)

When I was new to programming and CodeIgniter, one ...

Read More

Constructor-structor, what's your function?

Filed in: ExpressionEngine, CodeIgniter, php

March 10, 2011

I'd like to set the record straight on the usage of class constructors in CodeIgniter and ExpressionEngine apps/addons. I have seen EE-addons and CI code examples using them where they are not necessary. To illustrate this point and make things a bit more clear, I removed the class constructor from the CodeIgniter 'welcome' controller last night. Since your controllers in CI extend CI_Controller, you automatically inherit all the code that is contained in CI_Controller::__construct().

So for instance, if you have a controller like:

<?php

class Foobar extends CI_Controller {

    public function index()
    {
        $this->load->view('foobar');
    }
}

or a ...

Read More

CodeIgniter Reactor's Caching Drivers

Filed in: PHP, CodeIgniter

February 12, 2011

Before Reactor dropped, I snuck in some caching drivers (Docs Link) that Pascal had been tossing back and forth for several months. I've seen a few people talk about them, but I think they are still generally unknown so I want to give a brief intro on how to use them.

At this time, there are drivers available for APC, Memcached, file-based caching, and a dummy cache. If you haven't already, head over to BitBucket and get cloning or forking.

Examples

Caching API Calls

We can cache API calls pretty easily within controllers. Let's say we want ...

Read More

Updating EE CodeIgniter version after the big PHP5 changes.

Filed in: ExpressionEngine, CodeIgniter

December 15, 2010

As I've mentioned before, we use a subrepo for CodeIgniter on our products at EllisLab. So, we occasionally update the sub repo as we want to pull changes in CodeIgniter into the project. So a few weeks ago starting with this revision, Pascal and I went hog wild ripping PHP4 dependencies from CodeIgniter. It was a joyous and liberating experience to say the least. So now the scary part. We need to merge these CI changes into ExpressionEngine and MojoMotor. Well, we took a deep breath, updated that subrepo to the latest version of CI and went to work ...

Read More

CodeIgniter Authentication - Part 3

Content Protection and Logging Out!

Filed in: PHP, CodeIgniter

December 9, 2010

The last article was a marathon, so let's keep it light for a Thursday afternoon. We'll cover restricting controllers to logged in members. We left off with redirecting a user to the logged_in controller, so let's start there. Drop the following into application/controllers/logged_in.php.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Logged_in extends CI_Controller {

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();

        $this->output->enable_profiler(TRUE);
        $this->load->library('simple_auth');
        $this->load->helper('url');

        if ( ! $this->simple_auth->is_logged_in())
        {
            show_404();
        }
    }

    // --------------------------------------------------------------------------

    /**
     * Index
     */
    public function index()
    {
        $this->load->view('logged_in');
    }
}

Okay awesome. Let's go over ...

Read More

Creating a Simple Auth Class for CodeIgniter - Part Two

Filed in: PHP, CodeIgniter

December 7, 2010

This post is a continuation of Creating a Simple Auth Class for CodeIgniter - Part One, and here's we will focus on member registration and logging in. The code for these tutorials can be found in a repository over at BitBucket.

In all of these controller examples, I have the CodeIgniter Output Profiler turned on. Obviously, on a production website, you'll want to make sure that and display errors are off. :)

First things first. In order to log in, we need to be a member of the website. Let's start with our Registration Controller. Drop register.php into ...

Read More

Creating a Simple Auth Class for CodeIgniter - Part One

Filed in: PHP, CodeIgniter

December 6, 2010

Authentication is pretty essential to many web apps, and was probably the hardest thing for me to figure out when I first started a few years ago. So, I'd like to do a series of posts, where we develop a simple auth class for CodeIgniter.

In our SimpleAuth Library, we're going to require log in, log out, testing to see if a user is authenticated, user registration, changing passwords and a forgot password feature. On a side note, at EllisLab, we write user documentation before we write a line of code. The first time I did this, I ...

Read More

Developing on the CodeIgniter Trunk

Filed in: PHP, Mercurial, CodeIgniter

November 11, 2010

Earlier this evening, Kenny Meyers posed a question on Twitter about how to develop with CodeIgniter while working off the trunk. With Mercurial, it's easy.

We'll use a mercurial sub-repository so CodeIgniter is running off the EllisLab CodeIgniter repository. In fact, I created a quick repository over at BitBucket to help me easily start a new project.

Here is my basic project directory structure.

> tree 
.
├── fabfile.py
└── src
    ├── application
    │   ├── config
    │   ├── controllers
    │   ├── core
    │   ├── errors
    │   ├── helpers
    │   ├── hooks
    │   ├── index.html
    │   ├── language
    │   ├── libraries
    │   ├── models
    │   ├── third_party
    │   └── views
    ├── codeigniter
    │   ├── application
    │   ├── index.php
    │   ├── license.txt
    │   ├── system
    │   └── user_guide
    └── public
        ├── example.htaccess
        ├── index.php
        └── static
            ├── css
            ├── images ...

Read More