<commit_before>Static pages
###############################################################################

.. note:: This tutorial assumes you've downloaded CodeIgniter and
   :doc:`installed the framework <../installation/index>` in your
   development environment.

The first thing you're going to do is set up a **controller** to handle
static pages. A controller is simply a class that helps delegate work.
It is the glue of your web application.

For example, when a call is made to:

::

    http://example.com/news/latest/10

We might imagine that there is a controller named "news". The method
being called on news would be "latest". The news method's job could be to
grab 10 news items, and render them on the page. Very often in MVC,
you'll see URL patterns that match:

::

    http://example.com/[controller-class]/[controller-method]/[arguments]

As URL schemes become more complex, this may change. But for now, this
is all we will need to know.

Let's make our first controller
-------------------------------------------------------

Create a file at **app/Controllers/Pages.php** with the following
code.

::

    <?php

    namespace App\Controllers;

    use CodeIgniter\Controller;

    class Pages extends Controller
    {
        public function index()
        {
            return view('welcome_message');
        }

        public function view($page = 'home')
        {
            // ...
        }
    }


You have created a class named ``Pages``, with a ``view()`` method that accepts
one argument named ``$page``. It also has an ``index()`` method, the same
as the default controller found in **app/Controllers/Home.php**; that method
displays the CodeIgniter welcome page.

The ``Pages`` class is extending the
``CodeIgniter\Controller`` class. This means that the new Pages class can access the
methods and variables defined in the ``CodeIgniter\Controller`` class
(**system/Controller.php**).

The **controller is what will become the center of every request** to
your web application. Like any PHP class, you refer to
it within your controllers as ``$this``.

Now that you've created your first method, it's time to make some basic page
templates. We will be creating two "views" (page templates) that act as
our page footer and header.

Create the header at **app/Views/templates/header.php** and add
the following code:

::

    <!doctype html>
    <html>
    <head>
        <title>CodeIgniter Tutorial</title>
    </head>
    <body>

        <h1><?= esc($title) ?></h1>

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 **app/Views/templates/footer.php** that
includes the following code:

::

        <em>&copy; 2021</em>
    </body>
    </html>

.. note:: If you look closely in **header.php** template we are using an ``esc()``
    function. It's a global function provided by CodeIgniter to help prevent
    XSS attacks. You can read more about it :doc:`here </general/common_functions>`.

.. warning:: There are two **view()** functions referred to in this tutorial.
    One is the class method created with ``public function view($page = 'home')``
    and ``echo view('welcome_message');`` for displaying a view.
    Both are *technically* a function. But when you create a function in a class,
    it's called a method.

Adding logic to the controller
-------------------------------------------------------

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 bodies will be located in the **app/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.
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. This will be the body of the ``view()`` method
in the ``Pages`` controller created above:

::

    public function view($page = 'home')
    {
        if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            throw new \CodeIgniter\Exceptions\PageNotFoundException($page);<commit_msg>docs: move note to appropriate place<commit_after>Static pages
###############################################################################

.. note:: This tutorial assumes you've downloaded CodeIgniter and
   :doc:`installed the framework <../installation/index>` in your
   development environment.

The first thing you're going to do is set up a **controller** to handle
static pages. A controller is simply a class that helps delegate work.
It is the glue of your web application.

For example, when a call is made to:

::

    http://example.com/news/latest/10

We might imagine that there is a controller named "news". The method
being called on news would be "latest". The news method's job could be to
grab 10 news items, and render them on the page. Very often in MVC,
you'll see URL patterns that match:

::

    http://example.com/[controller-class]/[controller-method]/[arguments]

As URL schemes become more complex, this may change. But for now, this
is all we will need to know.

Let's make our first controller
-------------------------------------------------------

Create a file at **app/Controllers/Pages.php** with the following
code.

::

    <?php

    namespace App\Controllers;

    use CodeIgniter\Controller;

    class Pages extends Controller
    {
        public function index()
        {
            return view('welcome_message');
        }

        public function view($page = 'home')
        {
            // ...
        }
    }


You have created a class named ``Pages``, with a ``view()`` method that accepts
one argument named ``$page``. It also has an ``index()`` method, the same
as the default controller found in **app/Controllers/Home.php**; that method
displays the CodeIgniter welcome page.

.. note:: There are two ``view()`` functions referred to in this tutorial.
    One is the class method created with ``public function view($page = 'home')``
    and ``echo view('welcome_message')`` for displaying a view.
    Both are *technically* a function. But when you create a function in a class,
    it's called a method.

The ``Pages`` class is extending the
``CodeIgniter\Controller`` class. This means that the new Pages class can access the
methods and variables defined in the ``CodeIgniter\Controller`` class
(**system/Controller.php**).

The **controller is what will become the center of every request** to
your web application. Like any PHP class, you refer to
it within your controllers as ``$this``.

Now that you've created your first method, it's time to make some basic page
templates. We will be creating two "views" (page templates) that act as
our page footer and header.

Create the header at **app/Views/templates/header.php** and add
the following code:

::

    <!doctype html>
    <html>
    <head>
        <title>CodeIgniter Tutorial</title>
    </head>
    <body>

        <h1><?= esc($title) ?></h1>

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 **app/Views/templates/footer.php** that
includes the following code:

::

        <em>&copy; 2021</em>
    </body>
    </html>

.. note:: If you look closely in **header.php** template we are using an ``esc()``
    function. It's a global function provided by CodeIgniter to help prevent
    XSS attacks. You can read more about it :doc:`here </general/common_functions>`.

Adding logic to the controller
-------------------------------------------------------

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 bodies will be located in the **app/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.
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. This will be the body of the ``view()`` method
in the ``Pages`` controller created above:

::

    public function view($page = 'home')
    {
        if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
{'fraction_non_alphanumeric': 0.13832071420499492, 'fraction_numerical': 0.0022601423889705053, 'mean_word_length': 3.1086350974930363, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5993410', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2268, 'n_words': 1130}