Posts tagged “codeigniter

I went running today and now I can’t walk.

WHAT HAPPENED, LEGS? WE WERE WORKING SO WELL TOGETHER.

In other news, VALVE MAKE TF2 WORK AGAIN. JUST ROLL BACK THE PATCHES FOR GOD’S SAKES. I am willing to relinquish my Manniversary hat, even if it did look rather fetching on my Demoknight. On SPUF (steam user forums) half the people there are defending Valve. Why? They pushed an update and it’s broken TF2 for everyone. Don’t they test these things before they release them? Don’t tell me Valve is “doing their best”; they evidently are not. If they were taking the care you’d expect of a big name software house, they’d run updates on their test system for 24 hours before pushing it out. The lack of any kind of official announcement that they’ve fucked everything up is only adding to the image of unprofessionalism they are currently cultivating.

IN OTHER OTHER NEWS, for lack of anything better to do, I have been writing a blogging system using CodeIgniter. Mostly the reason for this is that I have a news announcement feed which currently runs off WordPress but WordPress is extremely heavy and difficult to customise. The WordPress you see here powering my blog is not very similar to the WordPress you get when you download it and deploy it yourself. The WordPress I have here is very nice, it provides me with much useful functionality, it is well configured and I have no complaints. The WordPress you download is less impressive and 3rd party plugins are a horrible minefield, and even the good ones, well, I can’t be bothered to review them for security and stuff. WordPress from a developer’s point of view is utterly horrible.

So for fun I’ve been putting together my own simple blogging app over the last two days. It is horribly incomplete but it does the following things in some form or another:

User login and registration,
Post authoring/editing,
Tags (and tag browsing),
Comment authoring and administration and classification into approved/pending/spam/trash,
RSS post feed (works for tags as well),
a loose plugin system using a signals/slots idea
A plugin which throttles comments by IP by setting them to ‘pending’

hurrah?

The biggest problem I’ve had is that CodeIgniter has not scaled to a non-trivial SQL schema as well as I’d hoped. I mean, I’m still writing a lot of SQL and I hate writing SQL. I also hate dealing with the results of SQL and trying to put it into a form useful for my program. Next time I will look at using Cake.


making a custom fully working 404 page with codeigniter

[update: I changed this to use exceptions, it's better this way]

CodeIgniter has a really annoying bug with its 404_override setting which makes it pretty much useless as a complete solution. The bug is that it won’t load the correct libraries and helpers of your 404 controller if the controller which threw the 404 also loaded those modules for itself. Fear not, a workaround is at hand. The advantage of properly handling a 404 in the context of your CodeIgniter application is that you get access to your standard stuff like your site layout (views) so you can load all your menus and stuff and load the error message where the content would normally go – this way the visitor feels like they’re on the site and they can get onto a working page easily, if they so desire. You can also hook into your database and perhaps do some logging.

Firstly, if you’re not using MY_Controller then, err, well you really should be. It makes life so much easier. MY_Controller is an easy to define superclass for all of your controllers. Implementing it lets you centralise all your site-wide constants and routines, and it’s very useful here.

Define it in application/core/MY_Controller.php and make all your ‘real’ controllers extend it. Also define individual exception classes that you want to handle. The _remap method wraps the controller’s method invocation, so we override that and use it to catch exceptions. This means that at any point in your controllers, you can just throw an Exception404 and you don’t have to worry about anything else; the exception bubbles up to _remap which unloads any existing output and then loads the 404 view(s).

You could extend this to also define 403 exceptions, and any other error codes your application may deal with, and you could use the exception message (i.e. throw new Exception404(‘some message’);) to set some descriptive text for your 404 view. [update: actually I tried this and it's a bit cumbersome, you're probably best off having just a HTTPException class and set the status code as a constructor argument)]

This is sort of how mine looks:

// defined for neater syntax only
class Exception404 extends Exception {
}

public class MY_Controller extends CI_Controller {
  public function _remap($method, $params = array()) {
    try {
      if (!method_exists($this, $method))
        throw new Exception404();
      return call_user_func_array(array($this, $method), $params);
    } catch(Exception404 $e) {
      $this->show_404();
    }
  }
  protected function _load_header($data=array()) {
    $this->load->view('headerview.php', $data);
  }
  protected function _load_footer() {
    $this->load->view('footerview.php');
  }
  protected function show_404() {
    // clear any views that have already been loaded
    $this->output->set_output('');
    $this->output->set_status_header('404');
    $this->_load_header();
    $this->load->view('404.php');
    $this->_load_footer();
  }
}

CodeIgniter URLs work like this:

index.php/CONTROLLER/METHOD/ARG1/ARG2/ARG3/…/ARGN

So we can see here that when we request a method of a controller, the remap function catches it. If the URL is okay and the method exists, it is executed. If it doesn’t, the 404 is executed.

To make it so it also catches non-existent controllers, change application/config/routes.php:

$route['404_override'] = 'CONTROLLER/show_404';

CONTROLLER can be any controller which inherits MY_Controller. I use my default, you can use whatever most appeals or, if you want, define a new controller.

You may (probably) have your application and server set up so it’s possible to request URLs which aren’t handled by CodeIgniter’s index.php. In this case, you probably also want to redirect when Apache catches a 404 to your new custom 404. Put this in your htaccess:

ErrorDocument 404 /index.php/CONTROLLER/show_404

Where again, CONTROLLER is some controller that implements show404.

you should now have site-wide redirection of file-not-found URLs to your exciting and sexy new 404 page. I thoroughly recommend using the opportunity to display to your baffled visitor a picture of a lolcat. I recommend this one:

or (better),


Follow

Get every new post delivered to your Inbox.