Codeigniter2学习笔记

0、控制器类必须继承CI_Controller,类名首字母必须大写
   (默认)example.com/index.php/控制器名/方法名/其它附加的   
    (控制器名必须与对应控制器文件名大小写一样,方法名也要大小写一样)
 
1、URL向方法传递参数,example.com/index.php/products/shoes/a/b
<?php
class Products extends CI_Controller {
    public function shoes($a, $b)
    {
        echo $a;
        echo $b;
    }
}
?>
结果是ab

2、修改默认的“URL调用方法”的行为
在Controller中定义_remap方法,那么it will always get called regardless of what your URI contains.
public function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->default_method();
    }
}
 _remap()有第二个可选的参数.
public function _remap($method, $params = array())
{
    $method = 'process_'.$method;
    if (method_exists($this, $method))
    {
        return call_user_func_array(array($this, $method), $params);
    }
    show_404();
}

3、控制页面输出,
   在控制器中定义_output方法,最后页面输出数据不会发送到浏览器会传递给_output处理
public function _output($output)
{
    echo $output;
}

4、在页面最下面显示调试信息
   在控制器方法中$this->output->enable_profiler(true);//禁用是false默认是禁用
   在application/config/profiler.php中可以配置是否显示调试信息中的每块内容
   $config['config']= FALSE;
   $config['queries']= FALSE;
   也可以在控制器中:
   $sections = array(
    'config'  => TRUE,
    'queries' => TRUE
    );
   $this->output->set_profiler_sections($sections);
   到达动态控制调试信息的显示。
   $sections所有可用的key
          Key                         描述                                                默认值
       benchmarks            Elapsed time of Benchmark points and total execution time     TRUE
       config                系统配置变量                                                  TRUE
       controller_info       访问的控制器名和方法名                                        TRUE
       get                   GET的数据                                                  TRUE
       http_headers          http请求头信息                                                TRUE
       memory_usage          内存使用单位byte                                              TRUE
       post                  POST的数据                                                    TRUE
       queries               所有执行的SQL语句包括执行时间                                 TRUE
       uri_string            当前请求的URL                                                 TRUE
       query_toggle_count 超过多少条查询隐藏查询信息块                                  25
   
    CodeIgniter will parse the pseudo-variables {elapsed_time} and {memory_usage} in y
    our output by default. To disable this, set the $parse_exec_vars class property to FALSE in your controller.
    $this->output->parse_exec_vars = FALSE;

5、控制器中不想被URL访问的方法
   方法用private修饰并且方法名以下划线开始,那么这个方法就不能通过URL访问
    private function _utility()
   {
   // some code
   }

6、控制器controllers文件夹中建立子文件夹功能,
   访问时变为example.com/index.php/子文件夹名/控制器名/方法名/其它附加的    

7、子类控制器有构造方法时必须先调用父类构造方法parent::__construct();

8、系统保留名称,避免在自己的代码中出现。
  控制器名
       CI_Base
       _ci_initialize
       Default
       index

  方法名
      is_really_writable()
      load_class()
      get_config()
      config_item()
      show_error()
      show_404()
      log_message()
      _exception_handler()
      get_instance()

   变量
      $config
      $mimes
      $lang

   常量
      ENVIRONMENT
      EXT
      FCPATH
      SELF
      BASEPATH
      APPPATH
      CI_VERSION
      FILE_READ_MODE
      FILE_WRITE_MODE
      DIR_READ_MODE
      DIR_WRITE_MODE
      FOPEN_READ
      FOPEN_READ_WRITE
      FOPEN_WRITE_CREATE_DESTRUCTIVE
      FOPEN_READ_WRITE_CREATE_DESTRUCTIVE
      FOPEN_WRITE_CREATE
      FOPEN_READ_WRITE_CREATE
      FOPEN_WRITE_CREATE_STRICT
      FOPEN_READ_WRITE_CREATE_STRICT
9、视图永远不会被直接调用,必须被控制加载
  加载视图文件name.php $this->load->view('name');如果是name.html那么就写为 $this->load->view('name.html');
  可以同时加载多个视图
  <?php
  class Page extends CI_Controller {
     function index()
     {
        $data['page_title'] = 'Your title';
        $this->load->view('header');
        $this->load->view('menu');
        $this->load->view('content', $data);
        $this->load->view('footer');
     }

  }
  ?>
  视图文件夹可以建立子文件夹,那么加载视图时变为:
  $this->load->view('folder_name/file_name');
10、向视图传递数据:

   数据是数组:
   $data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );
   $this->load->view('blogview', $data);
  视图中输出:
  <?=$title?>

  数据也可以是对象:
  $data = new Someclass();
  $this->load->view('blogview', $data);
  视图中输出:
  <?=对象属性名?>
11、循环数据
 <?php
class Blog extends CI_Controller {
    function index()
    {
        $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        $this->load->view('blogview', $data);
    }
}
?>
视图中:
<ul>
 <?php foreach ($todo_list as $item):?>
    <li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
上面用到了替换语法:类似的还有 endif, endfor, endforeach, and endwhile
快捷输出<?=变量?>

官方提供也有一个模板引擎,但是为了使页面运行更快不建议使用,建议使用纯php+语法替换

12、返回视图数据,而不是输出到浏览器
  $string = $this->load->view('myfile', '', true);
  第三参数This can be useful if you want to process the data in some way.
  If you set the parameter to true (boolean) it will return data.
  The default behavior is false, which sends it to your browser.
  Remember to assign it to a variable if you want the data returned:

13、URL安全,URL地址中只能包含这些字符: 数字 文本 ~ . : _ -
   魔法转义magic_quotes_runtime在程序初始化时就被禁用了。
  you don't have to remove slashes when retrieving data from your database.

14、错误信息控制
  By default, CodeIgniter comes with the environment constant set to 'development'.
  At the top of index.php, you will see:
  define('ENVIRONMENT', 'development');
  Setting CodeIgniter's ENVIRONMENT constant in index.php to a value of 'production' will
  turn off these errors. In development mode, it is recommended that a value of 'development' is used.

15、使用system/libraries 或者application/libraries里面的类库时
   首先要加载$this->load->library('email');
   然后使用:$this->someclass->some_function();  // Object instances will always be lower case
   带参初始化:
   $params = array('type' => 'large', 'color' => 'red');
   $this->load->library('Someclass', $params);
   $this->load之后创建一个类的实例$this->someclasss。

16、在自己的库中利用CodeIgniter资源
The Database classes can not be extended or replaced with your own classes.
All other classes are able to be replaced/extended.
    $this, however, only works directly within your controllers, your models, or your views.
If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.

17、模型
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
}
Your file will be this:
application/models/user_model.php

18、加载模型
 $this->load->model('Model_name');
 $this->load->model('blog/Model_name');//blog是models一个子文件夹
 使用:$this->Model_name->function();
 别名:
  $this->load->model('Model_name', 'fubar');
  $this->fubar->function();等价于$this->Model_name->function();

19、加载系统库system/libraries
    $this->load->library('email');
    $this->load->library(array('email', 'table'));

20、载入辅助函数
   例如,要载入文件名为url_helper.php的URL Helper,你将会用到下面的语句:
   $this->load->helper('url');
   url 是辅助函数文件的名字(不带.php后缀 和"helper" 部分)。
   载入多个辅助函数
   如果你想一次载入多个辅助函数,你可以这样做:
   $this->load->helper( array('helper1', 'helper2', 'helper3') );
   一旦你载入了想要用到辅助函数文件,你就可以用标准的函数调用方法来使用里面的函数。
   自动载入辅助函数
   如果你想要的话,CodeIgniter可以自动为你载入辅助函数。你可以通过打开 application/config/autoload.php ,
   并往自动载入数组(autoload array)中增加辅助函数来实现。
   "扩展" Helpers
   你如果想 "扩展"一个原有的 Helpers,,可以在你的 application/helpers/ 目录下创建一个新的helper,
   新的helper的名字是在被“扩展”的Helper的名字开头多加一个 MY_ (这是可以配置的. 见下.).
   如果你想做的只是在原有的helper中添加一些新的功能,比如,添加一两个新的方法,或者是修改一个方法;
   就不值得重写自己的helper。在这种情况下,最好是“扩展”已有的helper。“扩展”一词用在这里不是很恰当,
   因为Helper的方法是procedural 和 discrete的,在传统的语言环境中无法被“扩展”,不过在CodeIgniter中,
   你可以添加或修改helper的方法。
   例如,扩展一个本地已有的 Array Helper 你应该建立一个文件: application/helpers/MY_array_helper.php,
   并添加或重写(override)其中的一些方法:
   // any_in_array() is not in the Array Helper, so it defines a new function
   function any_in_array($needle, $haystack)
   {
       $needle = (is_array($needle)) ? $needle : array($needle);
       foreach ($needle as $item)
       {
           if (in_array($item, $haystack))
           {
               return TRUE;
           }
           }
       return FALSE;
   }
   // random_element() is included in Array Helper, so it overrides the native function
   function random_element($array)
   {
       shuffle($array);
       return array_pop($array);
   }
   设定你自己的前缀(Prefix)
   用于"扩展" helper 而加上前缀的文件同样也是对库和核心类的扩展.为了设置你自定义的前缀,
   请打开 application/config/config.php 文件,然后找到如下的条目:
   $config['subclass_prefix'] = 'MY_';
   请注意所以CodeIgniter自带的库都被冠以 CI_ 这样的前缀,所以请不使用这样的自定义前缀.







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值