开发者

codeigniter tips n tricks [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I have a php web project due for my college class in a couple days. It's worth 75% of my final grade. The way the teacher is marking us is as follows:

come the due date, we meet with him at a scheduled time and show hi开发者_JAVA百科m what we've learned. More or less a discussion about what our grade should be.

That brings me here, because he said the more things we do in a fancy codeigniter way, the more marks we get.

This is my question:

"What tips n tricks do you have regarding codeigniter? Is there anything you've ever learned about it that you wish you knew from the start?" Code snippets, guides from online somewhere, etc.

And my preference would be things that have a high ratio of impressiveness over length in time to learn.


might be of use ~

Extending base controllers to create an easy to use, extendable way of restricting access to your site

admin_controller.php

class Admin_Controller extends MY_Controller {

    function __construct()
    {
        if(!$this->auth->is_Admin()) /* imaginary auth library */
        {
            redirect('login/error');
        }
    }

 // bla bla
} 

template library

Using a good template library can be a godsend - google, there's plenty. Not for every project but can cut down on lots of unnecessary $this->load->view('foo); making your app easier to maintain.


The main things I do now that I wish I had done from the start?

Base Controllers
Template library
Migrations for your DB changes

Finally, while I used to be against ORM's in general, there are a few very lightweight ones around. The use of a very basic ORM (not these crazy heavy-weight bastards like Doctrine) can save you writing a LOT of code.

One to try is AwesomeRecord by Yorick Peterse, or DataMapper by WanWizard.

Also, use CI 2.0 now. Don't bother with 1.7.x or you'll be re-writing code in a few weeks.


$this->db->insert_id();

will return the id of last database entry...it can be really usefull and I use it all the time...


It's not directly related to the code itself, but CodeIgniter's User Guide really helped me understand what the MVC pattern is all about, with useful comments and examples such as:

Models are PHP classes that are designed to work with information in your database. For example, let's say you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and retrieve your blog data.

Following CI's example made my code 80% cleaner by having Models only do the work they were suppose to. I've brought this methodology to other frameworks/languages as well.


A couple of suggestions:

  • Implement a custom library (like say a phone number formatter or something)
  • Implement a controller hook, to gather usage statistics or for permission control.
  • Implement an error logging library that catches all your errors from try/catch blogs, logs it using the error logging function of codeigniter and also sends an email to a preset email ID with a stack trace.
  • Use an ORM (like Doctrine) to map your database tables into model classes and then use that to run all your queries.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜