From c5f99fdcc5c4a918b5b8fe3ddbd56ab25ad1c22b Mon Sep 17 00:00:00 2001 From: Wes Baker Date: Mon, 8 Jul 2013 17:22:21 -0400 Subject: Updating User Guide for 2.1.4. --- user_guide/tutorial/conclusion.html | 2 +- user_guide/tutorial/create_news_items.html | 28 ++++++++++++++-------------- user_guide/tutorial/hard_coded_pages.html | 24 ++++++++++++------------ user_guide/tutorial/index.html | 4 ++-- user_guide/tutorial/news_section.html | 4 ++-- user_guide/tutorial/static_pages.html | 24 ++++++++++++------------ 6 files changed, 43 insertions(+), 43 deletions(-) (limited to 'user_guide/tutorial') diff --git a/user_guide/tutorial/conclusion.html b/user_guide/tutorial/conclusion.html index a439ac131..68daed771 100644 --- a/user_guide/tutorial/conclusion.html +++ b/user_guide/tutorial/conclusion.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 2.1.3

CodeIgniter User Guide Version Location

diff --git a/user_guide/tutorial/create_news_items.html b/user_guide/tutorial/create_news_items.html index 853044c0c..8effe35ef 100644 --- a/user_guide/tutorial/create_news_items.html +++ b/user_guide/tutorial/create_news_items.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 2.1.3

CodeIgniter User Guide Version Location

@@ -72,13 +72,13 @@ Create news items <?php echo form_open('news/create') ?> - +
@@ -94,18 +94,18 @@ public function create() { $this->load->helper('form'); $this->load->library('form_validation'); - + $data['title'] = 'Create a news item'; - + $this->form_validation->set_rules('title', 'Title', 'required'); $this->form_validation->set_rules('text', 'text', 'required'); - + if ($this->form_validation->run() === FALSE) { - $this->load->view('templates/header', $data); + $this->load->view('templates/header', $data); $this->load->view('news/create'); $this->load->view('templates/footer'); - + } else { @@ -129,27 +129,27 @@ public function create() public function set_news() { $this->load->helper('url'); - + $slug = url_title($this->input->post('title'), 'dash', TRUE); - + $data = array( 'title' => $this->input->post('title'), 'slug' => $slug, 'text' => $this->input->post('text') ); - + return $this->db->insert('news', $data); }

This new method takes care of inserting the news item into the database. The third line contains a new function, url_title(). This function - provided by the URL helper - strips down the string you pass it, replacing all spaces by dashes (-) and makes sure everything is in lowercase characters. This leaves you with a nice slug, perfect for creating URIs.

- +

Let's continue with preparing the record that is going to be inserted later, inside the $data array. Each element corresponds with a column in the database table created earlier. You might notice a new method here, namely the post() method from the input library. This method makes sure the data is sanitized, protecting you from nasty attacks from others. The input library is loaded by default. At last, you insert our $data array into our database.

Routing

Before you can start adding news items into your CodeIgniter application you have to add an extra rule to config/routes.php file. Make sure your file contains the following. This makes sure CodeIgniter sees 'create' as a method instead of a news item's slug.

- +
 $route['news/create'] = 'news/create';
 $route['news/(:any)'] = 'news/view/$1';
diff --git a/user_guide/tutorial/hard_coded_pages.html b/user_guide/tutorial/hard_coded_pages.html
index 2f4218222..77ad7f2dd 100644
--- a/user_guide/tutorial/hard_coded_pages.html
+++ b/user_guide/tutorial/hard_coded_pages.html
@@ -28,7 +28,7 @@
 
- +

CodeIgniter User Guide Version 2.1.3

CodeIgniter User Guide Version Location

@@ -86,13 +86,13 @@ class Pages extends CI_Controller {

CodeIgniter 2 Tutorial

- +

Our header doesn't do anything exciting. It contains the basic HTML code that we will want to display before loading the main view. You can also see that we echo the $title variable, which we didn't define. We will set this variable in the Pages controller a bit later. Let's go ahead and create a footer at application/views/templates/footer.php that includes the following code.

- + @@ -106,36 +106,36 @@ class Pages extends CI_Controller {

The first thing we do is checking whether the page we're looking for does actually exist. We use PHP's native file_exists() to do this check and pass the path where the file is supposed to be. Next is the function show_404(), a CodeIgniter function that renders the default error page and sets the appropriate HTTP headers.

-

In the header template you saw we were using the $title variable to customize our page title. This is where we define the title, but instead of assigning the value to a variable, we assign it to the title element in the $data array. The last thing we need to do is loading the views in the order we want them to be displayed. We also pass the $data array to the header view to make its elements available in the header view file.

- +

In the header template you saw we were using the $title variable to customize our page title. This is where we define the title, but instead of assigning the value to a variable, we assign it to the title element in the $data array. The last thing we need to do is loading the views in the order we want them to be displayed. We also pass the $data array to the header view to make its elements available in the header view file.

+

Routing

Actually, our controller is already functioning. Point your browser to index.php/pages/view to see your homepage. When you visit index.php/pages/view/about you will see the about page, again including your header and footer. Now we're going to get rid of the pages/view part in our URI. As you may have seen, CodeIgniter does its routing by the class, method and parameter, separated by slashes.

Open the routing file located at application/config/routes.php and add the following two lines. Remove all other code that sets any element in the $route array.

- + -

CodeIgniter reads its routing rules from top to bottom and routes the request to the first matching rule. These routes are stored in the $route array where the keys represent the incoming request and the value the path to the method, as described above.

+

CodeIgniter reads its routing rules from top to bottom and routes the request to the first matching rule. These routes are stored in the $route array where the keys represent the incoming request and the value the path to the method, as described above.

The first rule in our $routes array matches every request - using the wildcard operator (:any) - and passes the value to the view method of the pages class we created earlier. The default controller route makes sure every request to the root goes to the view method as well, which has the first parameter set to 'home' by default.

diff --git a/user_guide/tutorial/index.html b/user_guide/tutorial/index.html index e5648f9e3..5baf52c6b 100644 --- a/user_guide/tutorial/index.html +++ b/user_guide/tutorial/index.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 2.1.3

CodeIgniter User Guide Version Location

@@ -59,7 +59,7 @@ Introduction

Tutorial − Introduction

-

This tutorial is intended to introduce you to the CodeIgniter framework and the basic principles of MVC architecture. It will show you how a basic CodeIgniter application is constructed in step-by-step fashion.

+

This tutorial is intended to introduce you to the CodeIgniter framework and the basic principles of MVC architecture. It will show you how a basic CodeIgniter application is constructed in step-by-step fashion.

In this tutorial, you will be creating a basic news application. You will begin by writing the code that can load static pages. Next, you will create a news section that reads news items from a database. Finally, you'll add a form to create news items in the database.

diff --git a/user_guide/tutorial/news_section.html b/user_guide/tutorial/news_section.html index 64c693246..db199848c 100644 --- a/user_guide/tutorial/news_section.html +++ b/user_guide/tutorial/news_section.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 2.1.3

CodeIgniter User Guide Version Location

@@ -103,7 +103,7 @@ public function get_news($slug = FALSE) $query = $this->db->get('news'); return $query->result_array(); } - + $query = $this->db->get_where('news', array('slug' => $slug)); return $query->row_array(); } diff --git a/user_guide/tutorial/static_pages.html b/user_guide/tutorial/static_pages.html index 13e406358..be05436ef 100644 --- a/user_guide/tutorial/static_pages.html +++ b/user_guide/tutorial/static_pages.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 2.1.3

CodeIgniter User Guide Version Location

@@ -109,15 +109,15 @@ We will be creating two "views" (page templates) that act as our page

CodeIgniter 2 Tutorial

- + -

The header contains the basic HTML code that you'll want to display before loading the main view, together with a heading. +

The header contains the basic HTML code that you'll want to display before loading the main view, together with a heading. It will also output the $title variable, which we'll define later in the controller. Now create a footer at application/views/templates/footer.php that includes the following code:

- + @@ -127,8 +127,8 @@ Now create a footer at application/views/templates/footer.php that in

Earlier you set up a controller with a view() method. The method accepts one parameter, which is the name of the page to be loaded. The static page templates will be located in the application/views/pages/ directory.

-

In that directory, create two files named home.php and about.php. -Within those files, type some text − anything you'd like − and save them. +

In that directory, create two files named home.php and about.php. +Within those files, type some text − anything you'd like − and save them. If you like to be particularly un-original, try "Hello World!".

In order to load those pages, you'll have to check whether the requested page actually exists:

@@ -136,15 +136,15 @@ If you like to be particularly un-original, try "Hello World!".

 public function view($page = 'home')
 {
-			
+
 	if ( ! file_exists('application/views/pages/'.$page.'.php'))
 	{
 		// Whoops, we don't have a page for that!
 		show_404();
 	}
-	
+
 	$data['title'] = ucfirst($page); // Capitalize the first letter
-	
+
 	$this->load->view('templates/header', $data);
 	$this->load->view('pages/'.$page, $data);
 	$this->load->view('templates/footer', $data);
@@ -160,7 +160,7 @@ public function view($page = 'home')
 
 

The last thing that has to be done is loading the views in the order they should be displayed. The second parameter in the view() method is used to pass values to the view. Each value in the $data array is assigned to a variable with the name of its key. So the value of $data['title'] in the controller is equivalent to $title in the view.

- +

Routing

The controller is now functioning! Point your browser to [your-site-url]index.php/pages/view to see your page. When you visit index.php/pages/view/about you'll see the about page, again including the header and footer.

@@ -169,7 +169,7 @@ The second parameter in the view() method is used to pass values to t http://example.com/[controller-class]/[controller-method]/[arguments]

Let's do that. Open the routing file located at application/config/routes.php and add the following two lines. Remove all other code that sets any element in the $route array.

- +
 $route['default_controller'] = 'pages/view';
 $route['(:any)'] = 'pages/view/$1';
-- 
cgit v1.2.3-24-g4f1b