CodeIgniter study notes

Official Handbook:

http://ellislab.com/codeigniter/user-guide/index.html

 

URL:
example.com/class/function/ID

Removing the index.php file
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.


Controllers:
functions
example.com/index.php/blog/index/

<?php
class Blog extends CI_Controller {

	public function index() {
		echo 'Hello World!';
	}

	public function comments() {
		echo 'Look at this!';
	}
}
?>



Passing URI Segments to your Functions
example.com/index.php/products/shoes/sandals/123
Your function will be passed URI segments 3 and 4 ("sandals" and "123"): 

<?php
class Products extends CI_Controller {
	public function shoes($sandals, $id) {
		echo $sandals;
		echo $id;
	}
}
?>




Defining a Default Controller(application/config/routes.php)
$route['default_controller'] = 'Blog';



Organizing Your Controllers into Sub-folders
example.com/index.php/products/shoes/show/123
//products Sub-folders name
To call the above controller your URI will look something like this:
example.com/index.php/products/shoes/show/123



View:
Loading a View

<?php
class Blog extends CI_Controller {
	function index() {
		$data = array (
			'title' => 'My Title',
			'heading' => 'My Heading',
			'message' => 'My Message'
		);

		$this->load->view('blogview', $data); //'product/blogview' sub folder
	}
}
?>


views page //blogview.php  

<html> 
<head> 
<title><?php echo $title;?></title> 
</head> 
<body> 
<h1><?php echo $heading;?></h1> 
</body> 
</html>

 



 
Creating Loops //multi dimensional arrays

<?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);
	}
}
?>

view file

<html> 
<head> 
<title><?php echo $title;?></title> 
</head> 
<body> 
<h1><?php echo $heading;?></h1>
 <h3>My Todo List</h3>
 <ul> 
<?php foreach ($todo_list as $item):?>
 <li><?php echo $item;?></li>
 <?php endforeach;?> 
</ul>
 </body> 
</html>


 












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值