Welcome Guest, Not a member yet? Register   Sign In
User Guide (Static Page) Documentation Clarification Please
#1

Hello everyone, 

While I have been using PHP for quite some time, I am totally new to CodeIgniter so please forgive my ignorance.

I have downloaded and installed CodeIgniter and am running it on MAMP. I am working my way through the documentation and am following the instructions for a static page. 

I have come to this section: https://codeigniter.com/user_guide/tutor...controller

It reads (3rd paragraph): 

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

It then provides some sample code: 



Code:
public function view($page = 'home')
{
        if ( ! file_exists(APPPATH.'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);
}


My question is, WHERE does that code go? Does it go in the applications/controllers/Pages.php file?

The instructions don't really tell you where that is supposed to go. 

Again, I know that this is probably really stupid question, but like I said, I am spanking new to CodeIgniter ... however, feel free to make fun of me Smile
Reply
#2

It does state it in the tutorial

"Adding logic to the controller"
.....
In order to load those pages, you’ll have to check whether the requested page actually exists:

The view function might already exist in your controller then you just add the if contruct to show the 404 error

I hope that helps.
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#3

Yes, it goes in the applications/controllers/Pages.php file.

See the Routing section to se how it is invoked.
Simpler is always better
Reply
#4

Thank (both of you) for your kind responses ... when I add it to the controller (Pages.php) then I get the following error when trying to view them


Code:
An uncaught Exception was encountered
Type: ParseError
Message: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file

Filename: /Applications/MAMP/htdocs/CodeIgniter-3.1.5/application/controllers/Pages.php

Line Number: 9
Backtrace:

File: /Applications/MAMP/htdocs/CodeIgniter-3.1.5/index.php
Line: 315
Function: require_once


So, not sure why that would be the case, it is supposed to work ...
Reply
#5

Look at line number 9
there seem to be something missing. Either a semicolon or } or )
Most likey you did not select the entire code and pasted only parts of it.
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#6

Hello Kuler,

I rember going though this as well, and thought it looked odd, but you have to remember that the provided code below
Quote:In order to load those pages, you’ll have to check whether the requested page actually exists:
is supposed to replace the code currently in the Pages controller:
Code:
       public function view($page = 'home')
       {
       }

So the controller goes from:
Code:
<?php
class Pages extends CI_Controller {

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

to:
Code:
<?php
class Pages extends CI_Controller {

   public function view($page = 'home')
     {
        if ( ! file_exists(APPPATH.'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);
}
}

:-)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB