Ci框架——搭建以及简单应用(以及Apache、Nginx配置)。

CodeIgniter框架,这里简称ci框架。 Ci框架几乎没有搭建可言,拿来就可以使用,不像Zend需要自己组建一下目录结构。但其实我总觉的这里面似乎有点问题,感觉还是把框架和应用分离开比较好。这个问题以后再说先,先去官网looklook。http://codeigniter.org.cn/ 我下载的版本为2.1.4 。目录结构如下。(.svn .DS_Store请忽略,一个是svn的文件,一个是mac系统文件) 目录结构 这里面主要有三个顶级文件夹,官方说明如下。 application—–应用程序 system—-框架系统 user_guide—-用户手册 先不关注system,先来直接配置一个vhosts让他跑起来。 我本地安装了Apache2 配置信息如下

<VirtualHost *:80> ServerName ci.local DocumentRoot /MySpace/CodaProject/ci <Directory /MySpace/CodaProject/ci> Options FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /private/var/log/apache2/ci_errors.log CustomLog “/private/var/log/apache2/ci_accesses.log” common RewriteLogLevel 0 </VirtualHost>

配置好hosts直接打开浏览器,就可以浏览了。 搭建好后浏览 尝试做一个controller,在application文件夹下发现了如下两个文件夹。controller和view。对于mvc模型熟悉的来说这两个单词简直太熟悉了,看来暂时不需要文档了,因为在里面找到了上面这个页面的代码。照葫芦画瓢来一个。 分别再controller和view文件夹下建立abel.php和abel_view.php abel.php

<?php
class Abel extends CI_Controller{

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

    }
}

浏览的时候发现一些问题。 当我键入ci.local/abel的时候出现了404错误,没有找到这个页面。后来将入口文件加入到url中就可以正常访问了。但这个很别扭。。。 如果你用的是apache的话可以在网站根目录,也就是与入口文件index.php中加入.htaccess文件。在其中写入如下文本。 > RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule !\.(js|ico|gif|jpg|jpeg|swf|png|css)$ /index.php [NC,L\]

这个文件的主要作用就是将全局的路由重定向到入口文件中,其中不包含js iso gif jpg等文件。前提得开启apache的重写模块,具体细节请网上查找。 此时再次浏览 ci.local/abel页面如下 自定义控制类 尝试使用变量显示在网页当中。 修改controller index方法中的代码如下

        $data['name']='Abel';
        $this->load->view('abel_view',$data);

在abel_view.php中就可以使用<?php echo $name;?>这种方式访问name了。从这种方式可以看出,这个变量加载近数组,这种写法实现方式应该为$this->$key = $v;形式。 可以在core中找到Loader类,在其中发现如下代码。

   /**
     * Load View
     *
     * This function is used to load a "view" file.  It has three parameters:
     *
     * 1. The name of the "view" file to be included.
     * 2. An associative array of data to be extracted for use in the view.
     * 3. TRUE/FALSE - whether to return the data or load it.  In
     * some cases it's advantageous to be able to return data so that
     * a developer can process it in some way.
     *
     * @param    string
     * @param    array
     * @param    bool
     * @return    void
     */
    public function view($view, $vars = array(), $return = FALSE)
    {
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
    }

在_ci_load方法中找到了如下实现方式。

        $_ci_CI =& get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
        {
            if ( ! isset($this->$_ci_key))
            {
                $this->$_ci_key =& $_ci_CI->$_ci_key;
            }
        }

Nginx配置Ci框架同样会遇到index.php路由的问题。vhost文件当中需要加入如下代码,统一入口路由。 http://ci.abelzhou.com/abel > location / { if ( !-f $request_filename ) { rewrite ^(.*)$ /index.php?$1 last; break; } } location ~ .*\.php?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }