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>


 












你可能感兴趣的:(CodeIgniter study notes)