顯示具有 CodeIgniter 標籤的文章。 顯示所有文章
顯示具有 CodeIgniter 標籤的文章。 顯示所有文章

2010年11月2日 星期二

[Codeigniter] 使用URI Routing來做網址自訂及SEO

URI Routing
http://codeigniter.com/user_guide/general/routing.html


上周在思考製作Blog時, 如何讓網址可以一個name對應到一個post_id

假設我有一個網址

http://localhost/post/list/10

我想要他不要每篇post都只是跟隨著post_id

我可以利用route來做

在route.php中加入
$route['post/cool'] = "post/list/10";

這樣我就可以用
http://localhost/post/cool

來mapping到post/list/10

來達到SEO的效果。

2010年10月11日 星期一

[CodeIgniter] 解決 CI 下載函數 force_download 使用 IE 下載時檔名亂碼

$file = file_get_contents($this->config->item('upload_path').$row->fullname);
    
// 防止中文檔名IE會亂碼
if(mb_strlen($row->title,"Big5") != strlen($row->title)) $row->title = iconv('utf-8', 'big5', $row->title);
    
force_download($row->title.$row->ext, $file);

參考的文章是直接改download_helper.php的code
我是直接寫在Controller, 傳入前先判斷是否為中文, 若是就先轉換

Reference:

[PHP] 判斷字串是否為中文
小惡魔 - [CodeIgniter] 解決 CI 下載函數 force_download 在 IE 底下檔案標題亂碼

2010年9月28日 星期二

[CodeIgniter] 讓CI支援rar 檔案上傳 20101026修正

今天在用CodeIgniter作檔案上傳功能時, 才發現不管怎麼傳rar都會顯示

The filetype you are attempting to upload is not allowed.

後來發現原來CI的MIME(Multipurpose Internet Mail Extensions)檔根本沒有rar

所以手動加入rar 這個檔案類型

但因不同瀏覽器判斷rar的結果不一樣, 所以要三種都加上

IE8, Chrome, Safari:   application/octet-stream
Firefox: application/rar
Opera:  application/x-rar-compressed


打開 codeigniter\system\application\config\mimes.php


在mime陣列中加上


'rar'   =>  array('application/x-rar-compressed', 'application/rar', 'application/octet-stream')
 

2010-10-26 更新

今天發現Chrome跟Firefox瀏覽器更新之後, 都會發現mimes type抓不到的情形

Firefox是上傳rar時顯示document/unknown
Chrome則是zip和rar都完全抓不到

所以之後就用CI內建的file_helper

有個函數叫get_mime_by_extension, 直接透過副檔名來抓它的type, 即可解決

2010年9月16日 星期四

[CodeIgniter] Show all session userdata 列出所有session

使用CI 時, 在http://codeigniter.com/user_guide/libraries/sessions.html

找不到列出所有Session key & value的方法

後來直接到CI Library打開Session.php

發現裡面有個方法叫 all_userdata


/**
  * Fetch all session data
  *
  * @access public
  * @return mixed
  */
 function all_userdata()
 {
  return ( ! isset($this->userdata)) ? FALSE : $this->userdata;
 }

所以他有提供這方法, 只是Document沒有寫

使用方法
$this->session->all_userdata();

2010年8月11日 星期三

[CodeIgniter] 使用Model時, 檔案名稱要用小寫

避免在不同的作業系統(Linux, Windows) Server大小寫錯誤。
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends Model {

    function User_model()
    {
        parent::Model();
    }
}
Your file will be this:
application/models/user_model.php
都怪我沒把說明文件看清楚= =" 除錯除很久~

2010年7月30日 星期五

[CodeIgniter] 使用rewrite URLs

Reference


Step 1 : 

使用mod_rewrite : 開啟Apache mod_rewirte Module
1. 打開httpd.conf

Options FollowSymLinks
AllowOverride All

2. 找到 #LoadModule rewrite_module modules/mod_rewrite.so
3. # 字號拿掉後存檔, restart Apache Server即可開啟

Step 2 : 設定 .htaccess 檔

其中的
RewriteBase /
要改成自己的子目錄

Related Posts Plugin for WordPress, Blogger...