summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/tutorial/news_section.rst
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2015-05-21 14:13:47 +0200
committerAndrey Andreev <narf@devilix.net>2015-05-21 14:13:47 +0200
commit3a4f9cd467b23ca5aeb93d20214685c86c0d520c (patch)
tree7861a9c8d08789e050de39d152dd367d0328d611 /user_guide_src/source/tutorial/news_section.rst
parentb8cd5e657363795f127ae5e02e69972627b3fe9c (diff)
[ci skip] Update tutorial
Related: #3866
Diffstat (limited to 'user_guide_src/source/tutorial/news_section.rst')
-rw-r--r--user_guide_src/source/tutorial/news_section.rst54
1 files changed, 28 insertions, 26 deletions
diff --git a/user_guide_src/source/tutorial/news_section.rst b/user_guide_src/source/tutorial/news_section.rst
index 688f2cb19..286d620dc 100644
--- a/user_guide_src/source/tutorial/news_section.rst
+++ b/user_guide_src/source/tutorial/news_section.rst
@@ -15,10 +15,9 @@ should be placed in a model, so they can easily be reused later. Models
are the place where you retrieve, insert, and update information in your
database or other data stores. They represent your data.
-Open up the application/models directory and create a new file called
-News_model.php and add the following code. Make sure you've configured
-your database properly as described
-:doc:`here <../database/configuration>`.
+Open up the *application/models/* directory and create a new file called
+*News_model.php* and add the following code. Make sure you've configured
+your database properly as described :doc:`here <../database/configuration>`.
::
@@ -37,8 +36,8 @@ library. This will make the database class available through the
``$this->db`` object.
Before querying the database, a database schema has to be created.
-Connect to your database and run the SQL command below. Also add some
-seed records.
+Connect to your database and run the SQL command below (MySQL).
+Also add some seed records.
::
@@ -75,7 +74,7 @@ following code to your model.
With this code you can perform two different queries. You can get all
news records, or get a news item by its `slug <#>`_. You might have
-noticed that the $slug variable wasn't sanitized before running the
+noticed that the ``$slug`` variable wasn't sanitized before running the
query; :doc:`Query Builder <../database/query_builder>` does this for you.
Display the news
@@ -83,9 +82,9 @@ Display the news
Now that the queries are written, the model should be tied to the views
that are going to display the news items to the user. This could be done
-in our pages controller created earlier, but for the sake of clarity, a
-new "news" controller is defined. Create the new controller at
-application/controllers/News.php.
+in our ``Pages`` controller created earlier, but for the sake of clarity,
+a new ``News`` controller is defined. Create the new controller at
+*application/controllers/News.php*.
::
@@ -96,6 +95,7 @@ application/controllers/News.php.
{
parent::__construct();
$this->load->model('news_model');
+ $this->load->helper('url_helper');
}
public function index()
@@ -113,11 +113,13 @@ Looking at the code, you may see some similarity with the files we
created earlier. First, the ``__construct()`` method: it calls the
constructor of its parent class (``CI_Controller``) and loads the model,
so it can be used in all other methods in this controller.
+It also loads a collection of :doc:`URL Helper <../helpers/url_helper>`
+functions, because we'll use one of them in a view later.
-Next, there are two methods to view all news items and one for a
-specific news item. You can see that the $slug variable is passed to the
-model's method in the second method. The model is using this slug to
-identify the news item to be returned.
+Next, there are two methods to view all news items and one for a specific
+news item. You can see that the ``$slug`` variable is passed to the model's
+method in the second method. The model is using this slug to identify the
+news item to be returned.
Now the data is retrieved by the controller through our model, but
nothing is displayed yet. The next thing to do is passing this data to
@@ -136,9 +138,9 @@ the views.
}
The code above gets all news records from the model and assigns it to a
-variable. The value for the title is also assigned to the $data['title']
+variable. The value for the title is also assigned to the ``$data['title']``
element and all data is passed to the views. You now need to create a
-view to render the news items. Create application/views/news/index.php
+view to render the news items. Create *application/views/news/index.php*
and add the next piece of code.
::
@@ -151,20 +153,20 @@ and add the next piece of code.
<div class="main">
<?php echo $news_item['text']; ?>
</div>
- <p><a href="<?php echo $news_item['slug']; ?>">View article</a></p>
+ <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>
<?php endforeach; ?>
Here, each news item is looped and displayed to the user. You can see we
-wrote our template in PHP mixed with HTML. If you prefer to use a
-template language, you can use CodeIgniter's :doc:`Template
+wrote our template in PHP mixed with HTML. If you prefer to use a template
+language, you can use CodeIgniter's :doc:`Template
Parser <../libraries/parser>` class or a third party parser.
The news overview page is now done, but a page to display individual
news items is still absent. The model created earlier is made in such
way that it can easily be used for this functionality. You only need to
add some code to the controller and create a new view. Go back to the
-news controller and update ``view()`` with the following:
+``News`` controller and update ``view()`` with the following:
::
@@ -198,12 +200,12 @@ The only things left to do is create the corresponding view at
Routing
-------
-Because of the wildcard routing rule created earlier, you need an
-extra route to view the controller that you just made. Modify your
-routing file (application/config/routes.php) so it looks as follows.
-This makes sure the requests reaches the news controller instead of
-going directly to the pages controller. The first line routes URI's with
-a slug to the view method in the news controller.
+Because of the wildcard routing rule created earlier, you need an extra
+route to view the controller that you just made. Modify your routing file
+(*application/config/routes.php*) so it looks as follows.
+This makes sure the requests reaches the ``News`` controller instead of
+going directly to the ``Pages`` controller. The first line routes URI's
+with a slug to the ``view()`` method in the ``News`` controller.
::