开发者

file traversal type auto loading in codeigniter

In other php frameworks (kohana, zend, fuel) its possible to auto load files based on their directory path. Is it possible to do开发者_如何学Python this in codeigniter, so i could for instance load models, by typing something like

models_category_users

(where this would map to models/category/users.php) ?

Im using codeigniter reactor, version 2.0.2


You can autoload files system wide in the autoload.php file in the application/config folder. For extending the default model, you would create a file called MY_Model.php in the application/core folder. (you can change the prefix of MY_ in application/config/config.php)

otherwise you would use $this->load->model('category/users'); to load the users.php model in the models/category folder. You can also specify package paths... see the bottom of this page: http://codeigniter.com/user_guide/libraries/loader.html


CodeIgniter doesn't have PHP Autoloading build in, the only way to add it is by writing your own autoloader. I wrote a very extensive one a while back for CI: https://bitbucket.org/jschreuder/augmentedci/src/554e3d956a15/application/config/autoloader.php - though I'd probably do some things different now. If you'd search the forums you'd probably find some options as well.

One of the things you will run into is that CI uses the word "autoload" for something that should be "always-load", or maybe "autoload on each request". What you want is "autoload on demand" for classes, but that'll be a foreign concept to many CI users.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜