这里是新生需要在 CodeIgniter 中处理的所有信息 :)
首先通过.htaccess从CodeIgniter url中删除'index.php'。在 CodeIgniter 'index.php' 所在的位置创建 .htaccess 文件。
.htaccess(使用 windows,只需将任何空白文本文件重命名为 .htaccess。创建一个 .htaccess 文件,如此简单...)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
如何在 WAMP 中启用 mode_rewrite
如何在 WAMP 中手动启用 mode_rewrite
1 - 使用您喜欢的文本编辑器打开 apache 的配置文件。
2 - 配置文件一般位于:{apache_dir}/conf/httpd.conf
3 - 如果您使用的是 XAMPP 或 WAMP 包,那么您将找到该文件
在:{xampp_dir}/bin/apache/apache-xxx/conf/httpd.conf 或 {wamp_dir}/bin/apache/apache-xxx/conf/httpd.conf
4 - 搜索以下字符串:#LoadModule rewrite_module modules/mod_rewrite.so 并取消注释(删除“#”号)。
5 - 现在搜索另一个字符串 AllowOverride None 并将其替换为 AllowOverride All
6 - 最后保存更改,关闭您的文本编辑器并重新启动您的 apache 服务器。
自动设置“base_url”
/*
|--------------------------------------------------------------------------
| Dynamic Base Url
|--------------------------------------------------------------------------
| Put below code in 'index.php' on top
|
*/
define('APP_URL', ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['SERVER_NAME']}".str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']));
配置设置
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = APP_URL;
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
配置 CSRF 设置(如果你没有在表单中传递 CSRF 令牌,或者没有使用 CodeIgniter Form Helper。否则你将无法通过 ajax 或 Form POST POST 参数或调用控制器方法)
$config['csrf_protection'] = FALSE;
现在,问题解答:(
JavaScript
<script type="text/javascript">
$(document).ready(function() {
// define base url
var _CI_BASE_URL_ = "<?php echo site_url(); ?>";
/**
* ---------------------------
* Example -1 with Url Params
* ---------------------------
*/
// on click cart button call ajax
$(".cart").click(function(e){
// stop default button behaviour
e.preventDefault();
// get subid from button attr
var subid = $(this).data('index');
// ajax function
$.ajax(
{
// *Notice: just send 'subid' with ajax url and CodeIgniter will take it from url
url: _CI_BASE_URL_ + 'home/add_to_cart/' + subid,
// method
type: "POST",
// data type
dataType:'json',
// success callback
success: function(data) {
// why server response, we already have subid in JavaScript vars
// we can user that same to redirect in url
window.location.href = _CI_BASE_URL_ + 'home/add_to_cart/' + subid;
},
// ohh! we got error
error: function(xhr, status, error) {
// get ajax error
var error = xhr.responseText;
alert('Request made some error: ' + error);
}
});
});
/**
* ---------------------------
* Example -2 with POST Params
* ---------------------------
*/
// on click cart button call ajax
$(".cart").click(function(e){
// stop default button behaviour
e.preventDefault();
// get subid from button attr
var subid = $(this).data('index');
// ajax function
$.ajax(
{
// just send subid with url and CodeIgniter will take from url
url: _CI_BASE_URL_ + 'home/add_to_cart_another',
// method
type: "POST",
// POST params
data: {subid: subid},
// data type
dataType:'json',
// success callback
success: function(data) {
// if got the JSON with key cat_id
if(data.cat_id){
// if you want to alert, first alert then redirect
alert('CodeIginter retuned Cat ID: ' + data.cat_id);
// redirect user now..
window.location.href = _CI_BASE_URL_ + 'home/add_to_cart_another/' + data.cat_id;
}else{
alert('Category ID not return or null...');
}
},
// ohh! we got error
error: function(xhr, status, error) {
// get ajax error
var error = xhr.responseText;
alert('Request made some error: ' + error);
}
});
});
});
</script>
现在 CodeIgniter 控制器 :( :(
<?php
// Security First Matters...
(defined('BASEPATH') or exit('No direct script access allowed'));
/**
* Class Home CI Controller
*/
class Home extends CI_Controller
{
/**
* Default run method
* @return [type] [description]
*/
public function index()
{
$this->load->view('home');
}
/**
* [add_to_cart description]
* @param [type] $cat_id [description]
*/
public function add_to_cart($cat_id)
{
/**
* Codeigniter is enough smart to take parameters if passed in url
* if url is 'http://localhost/CodeIgniter/home/add_to_cart/100'
* '100' will be assign to $cat_id by CodeIgniter
*
* intval to make sure only integer
*/
// echo response for ajax call as JSON
echo json_encode(array('cat_id' => intval($cat_id)));
}
/**
* [add_to_cart description]
* @param [type] $cat_id [description]
*/
public function add_to_cart_another()
{
/**
* get cat id from post and sanitize by passing 2nd param as true for XSS Clean,
* Security always matters...
*
* intval to make sure only integer
*/
$cat_id = $this->input->post('subid', true);
// echo response for ajax call as JSON
echo json_encode(array('cat_id' => intval($cat_id)));
}
}
/* End of file home.php */
/* Location: ./application/controllers/home.php */
现在,我要睡觉了.. :D Tada!! ....