星期六, 7月 11, 2015

[CI] 記錄一下CI框架新手功能

記錄一些CI框架Seed常用的功能手記,讓日後可以快速回憶 :D
雖然已經是被很多人放棄的技術,不過還是有他方便的地方XD

測試版本

CI 2.x

設定起始的base url

Just overwrite the line in config/config.php with the following:
$config['base_url']    = 'http://'.$_SERVER['HTTP_HOST'].'/';
If you are using the sub folder you can use following code:
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = "$root";

設定資料庫


application/config/database.php:

設定啟動的控制器


打開application/config/routes.php:
$route['default_controller'] = 'index';

設定啟動時載入相關的helper


打開application/config/autoload.php:
// $autoload['helper'] = array();
$autoload['helper'] = array('form','url','assets','images','load_view_helper');


設定htaccess取代index.php

新增.htaccess檔案,新增下面的的東西

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

透過redirect方法轉頁


$redirectUrl = '/users/' .  $pool_user['username'] . '/albums';
 redirect(site_url($redirectUrl),'location', 301);
 // redirect('index', 'refresh');

如 果噴以下的錯誤,請去調整php.ini的output_buffering屬性要設定On

A PHP Error was encounteredSeverity: Warning
Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/flickrlinkr2/application/helpers/assets_helper.php:2)
Filename: helpers/url_helper.php

另外轉頁前不應該有其他output到頁面上的指令,不然也會爆錯

Form Post 表單處理

CI內部提供的form_open()的方法來產生form html的TAG,包含CRSF的處理

Router的規則設定


透過router規則的設定,能讓我們的url更具有可讀性

打開application/config/routes.php

//使用者頁面
//users/bigd/albums
$route['users/(:any)/albums'] = 'users/albums/$1';
$route['users/(:any)/albums/(:num)'] = 'users/album/$1/$2';

新增你自已的config.php

在/application/config之下建立新的<你的config>.php檔案

$config['flickrlinkr_version'] = '1.0';
//API常用函數
$config['flickrlinkr_albums_perpage'] = 18;
$config['flickrlinkr_frineds_perpage'] = 16;
$config['flickrlinkr_photos_perpage'] = 500;
$config['flickrlinkr_search_photos_perpage'] = 100;

在控制器裡面透過 $this->load->config('設定檔名')可以載入這個設定檔

         $this->load->config('flickrlinkr');

接著透過$this->config->item('變數名稱')
$this->config->item('flickrlinkr_albums_perpage');

整合第三方函式庫

待補

設定response的輸出JSON


$this->output ->set_content_type('application/json') ->set_output(json_encode($response));

自定404控制器


$route['404_override'] = 'error/myerror';


取得GET參數


  $pageIndex = $this->input->get('page',TRUE);
        if($pageIndex == false){
            $pageIndex = 1;
        }



修改URI字元保護規則

預設的CI的url處理是不允許中文字符與ASCII字元,可以新增模組或修改config.php來處理


$config['permitted_uri_chars'] ='a-z 0-9~%.:_-u4e00-u9fa5';

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails