Skip to content

Commit e347927

Browse files
committed
Merge branch 'develop' of git://github.com/EllisLab/CodeIgniter into develop
2 parents 7447927 + 77a8c1f commit e347927

File tree

6 files changed

+612
-1
lines changed

6 files changed

+612
-1
lines changed

user_guide_src/source/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ CodeIgniter is right for you if:
4242
libraries/index
4343
database/index
4444
helpers/index
45-
documentation/index
45+
documentation/index
46+
tutorial/index
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
##########
2+
Conclusion
3+
##########
4+
5+
This tutorial did not cover all of the things you might expect of a
6+
full-fledged content management system, but it introduced you to the
7+
more important topics of routing, writing controllers, and models. We
8+
hope this tutorial gave you an insight into some of CodeIgniter's basic
9+
design patterns, which you can expand upon.
10+
11+
Now that you've completed this tutorial, we recommend you check out the
12+
rest of the documentation. CodeIgniter is often praised because of its
13+
comprehensive documentation. Use this to your advantage and read the
14+
"Introduction" and "General Topics" sections thoroughly. You should read
15+
the class and helper references when needed.
16+
17+
Every intermediate PHP programmer should be able to get the hang of
18+
CodeIgniter within a few days.
19+
20+
If you still have questions about the framework or your own CodeIgniter
21+
code, you can:
22+
23+
- Check out our `forums <http://codeigniter.com/forums>`_
24+
- Visit our `IRC chatroom <http://codeigniter.com/wiki/IRC>`_
25+
- Explore the `Wiki <http://codeigniter.com/wiki/>`_
26+
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#################
2+
Create news items
3+
#################
4+
5+
You now know how you can read data from a database using CodeIgnite, but
6+
you haven't written any information to the database yet. In this section
7+
you'll expand your news controller and model created earlier to include
8+
this functionality.
9+
10+
Create a form
11+
-------------
12+
13+
To input data into the database you need to create a form where you can
14+
input the information to be stored. This means you'll be needing a form
15+
with two fields, one for the title and one for the text. You'll derive
16+
the slug from our title in the model. Create the new view at
17+
application/views/news/create.php.
18+
19+
::
20+
21+
<h2>Create a news item</h2>
22+
23+
<?php echo validation_errors(); ?>
24+
25+
<?php echo form_open('news/create') ?>
26+
27+
<label for="title">Title</label>
28+
<input type="input" name="title" /><br />
29+
30+
<label for="text">Text</label>
31+
<textarea name="text"></textarea><br />
32+
33+
<input type="submit" name="submit" value="Create news item" />
34+
35+
</form>
36+
37+
There are only two things here that probably look unfamiliar to you: the
38+
form_open() function and the validation_errors() function.
39+
40+
The first function is provided by the `form
41+
helper <../helpers/form_helper.html>`_ and renders the form element and
42+
adds extra functionality, like adding a hidden `CSFR prevention
43+
field <../libraries/security.html>`_. The latter is used to report
44+
errors related to form validation.
45+
46+
Go back to your news controller. You're going to do two things here,
47+
check whether the form was submitted and whether the submitted data
48+
passed the validation rules. You'll use the `form
49+
validation <../libraries/form_validation.html>`_ library to do this.
50+
51+
::
52+
53+
public function create()
54+
{
55+
$this->load->helper('form');
56+
$this->load->library('form_validation');
57+
58+
$data['title'] = 'Create a news item';
59+
60+
$this->form_validation->set_rules('title', 'Title', 'required');
61+
$this->form_validation->set_rules('text', 'text', 'required');
62+
63+
if ($this->form_validation->run() === FALSE)
64+
{
65+
$this->load->view('templates/header', $data);
66+
$this->load->view('news/create');
67+
$this->load->view('templates/footer');
68+
69+
}
70+
else
71+
{
72+
$this->news_model->set_news();
73+
$this->load->view('news/success');
74+
}
75+
}
76+
77+
The code above adds a lot of functionality. The first few lines load the
78+
form helper and the form validation library. After that, rules for the
79+
form validation are set. The set\_rules() method takes three arguments;
80+
the name of the input field, the name to be used in error messages, and
81+
the rule. In this case the title and text fields are required.
82+
83+
CodeIgniter has a powerful form validation library as demonstrated
84+
above. You can read `more about this library
85+
here <../libraries/form_validation.html>`_.
86+
87+
Continuing down, you can see a condition that checks whether the form
88+
validation ran successfully. If it did not, the form is displayed, if it
89+
was submitted **and** passed all the rules, the model is called. After
90+
this, a view is loaded to display a success message. Create a view at
91+
application/view/news/success.php and write a success message.
92+
93+
Model
94+
-----
95+
96+
The only thing that remains is writing a method that writes the data to
97+
the database. You'll use the Active Record class to insert the
98+
information and use the input library to get the posted data. Open up
99+
the model created earlier and add the following:
100+
101+
::
102+
103+
public function set_news()
104+
{
105+
$this->load->helper('url');
106+
107+
$slug = url_title($this->input->post('title'), 'dash', TRUE);
108+
109+
$data = array(
110+
'title' => $this->input->post('title'),
111+
'slug' => $slug,
112+
'text' => $this->input->post('text')
113+
);
114+
115+
return $this->db->insert('news', $data);
116+
}
117+
118+
This new method takes care of inserting the news item into the database.
119+
The third line contains a new function, url\_title(). This function -
120+
provided by the `URL helper <../helpers/url_helper.html>`_ - strips down
121+
the string you pass it, replacing all spaces by dashes (-) and makes
122+
sure everything is in lowercase characters. This leaves you with a nice
123+
slug, perfect for creating URIs.
124+
125+
Let's continue with preparing the record that is going to be inserted
126+
later, inside the $data array. Each element corresponds with a column in
127+
the database table created earlier. You might notice a new method here,
128+
namely the post() method from the `input
129+
library <../libraries/input.html>`_. This method makes sure the data is
130+
sanitized, protecting you from nasty attacks from others. The input
131+
library is loaded by default. At last, you insert our $data array into
132+
our database.
133+
134+
Routing
135+
-------
136+
137+
Before you can start adding news items into your CodeIgniter application
138+
you have to add an extra rule to config/routes.php file. Make sure your
139+
file contains the following. This makes sure CodeIgniter sees 'create'
140+
as a method instead of a news item's slug.
141+
142+
::
143+
144+
$route['news/create'] = 'news/create';
145+
$route['news/(:any)'] = 'news/view/$1';
146+
$route['news'] = 'news';
147+
$route['(:any)'] = 'pages/view/$1';
148+
$route['default_controller'] = 'pages/view';
149+
150+
Now point your browser to your local development environment where you
151+
installed CodeIgniter and add index.php/news/create to the URL.
152+
Congratulations, you just created your first CodeIgniter application!
153+
Add some news and check out the different pages you made.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
########
2+
Tutorial
3+
########
4+
5+
This tutorial is intended to introduce you to the CodeIgniter framework
6+
and the basic principles of MVC architecture. It will show you how a
7+
basic CodeIgniter application is constructed in step-by-step fashion.
8+
9+
In this tutorial, you will be creating a **basic news application**. You
10+
will begin by writing the code that can load static pages. Next, you
11+
will create a news section that reads news items from a database.
12+
Finally, you'll add a form to create news items in the database.
13+
14+
This tutorial will primarily focus on:
15+
16+
- Model-View-Controller basics
17+
- Routing basics
18+
- Form validation
19+
- Performing basic database queries using "Active Record"
20+
21+
The entire tutorial is split up over several pages, each explaining a
22+
small part of the functionality of the CodeIgniter framework. You'll go
23+
through the following pages:
24+
25+
- Introduction, this page, which gives you an overview of what to
26+
expect.
27+
- `Static pages <static_pages.html>`_, which will teach you the basics
28+
of controllers, views and routing.
29+
- `News section <news_section.html>`_, where you'll start using models
30+
and will be doing some basic database operations.
31+
- `Create news items <create_news_items.html>`_, which will introduce
32+
more advanced database operations and form validation.
33+
- `Conclusion <conclusion.html>`_, which will give you some pointers on
34+
further reading and other resources.
35+
36+
Enjoy your exploration of the CodeIgniter framework.
37+
38+
.. toctree::
39+
:glob:
40+
:hidden:
41+
:titlesonly:
42+
43+
index
44+
static_pages
45+
news_section
46+
create_news_items
47+
conclusion

0 commit comments

Comments
 (0)