Welcome Guest, Not a member yet? Register   Sign In
Static Pages
#6

(07-09-2015, 09:35 AM)christaliise Wrote:
(07-09-2015, 02:45 AM)Dracula Wrote:
(07-09-2015, 02:27 AM)christaliise Wrote:
(07-08-2015, 12:50 PM)Dracula Wrote:
(07-08-2015, 09:55 AM)christaliise Wrote: I am studying the "Static Pages" in the CodeIgniter Tutorial. I'm stuck what appears to be a simple problem.

I understand everything up to this line - 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!”.

But then - In order to load those pages, you’ll have to check whether the requested page actually exists: - (I understand the words in that line but despite reading the page many times, I have no idea where to put the following 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);
}


You have to create a controller in application/controllers directory.
For example, create the file Pages.php in the above directory and paste in this code:






PHP Code:
<?php

class Pages extends CI_Controller
{

 
   public function__construct()
 
   {
 
   }

 
   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);
 
   }




After this, you have to go to application/config/routes.php to set up the default controller.
It should look like this:





PHP Code:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1'

Thanks Dracula, I had added the code to Pages.php but not in the same form as you indicated, however I've now changed it. Is there some reason for the code that you submitted being different by including - public function__construct() - than the code in the Tutorial and a reason why the coding you submitted not being in the box below this line "Create a file at application/controllers/Pages.php with the following code."?

I had also added the code to routes.php but I'm confused with "Remove all other code that sets any element in the $route array." I don't know what code will set an element in the $route array, but I guessed that this line - $route['default_controller'] = 'welcome'; - should be deleted, and the 2 added lines inserted to above $route['404_override'] = '';.

In any event I still get the XAMPP "Welcome Page" coming up in my browser, despite my numerous and varying attempts. The URL keeps reverting to http://localhost/xampp/


Hi there,

First, the method __construct is not always required but it is better that you create it in your controllers.
Mabe you whant to load some models in your controller and this is the place you do it.

For the routes.php file.
The $route['default_controler'] requires a controller name that will be accesed by default when you go to www.yourtsitename.com

For the XAMPP problem, you must create a separete folder in your xampp httdocs for your project, codeigniter, and then paste all the codeigniter files and directories there, after that you can acces it from going to localhost/yourproject

Thanks Dracula. Yes, I had created a separate folder for my project and pasted the CodeIgniter files there, but I'm thinking my problem maybe the config link not being changed e.g. $config['base_url'] = ''; or some other setting. I have tried inserting the project name in $config but still get the XAMPP "Welcome Page" coming up in my browser. Does some setting like that need to be changed?

You don't have to set base_url.
Do you have an .htaccess file in the root of your project?
If not, create one and paste this in:


Code:
<IfModule mod_rewrite.c>

 Options +FollowSymLinks
 RewriteEngine on

 # Send request via index.php
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply


Messages In This Thread
Static Pages - by christaliise - 07-08-2015, 09:55 AM
RE: Static Pages - by Dracula - 07-08-2015, 12:50 PM
RE: Static Pages - by christaliise - 07-09-2015, 02:27 AM
RE: Static Pages - by Dracula - 07-09-2015, 02:45 AM
RE: Static Pages - by christaliise - 07-09-2015, 09:35 AM
RE: Static Pages - by Dracula - 07-09-2015, 09:46 AM
RE: Static Pages - by christaliise - 07-09-2015, 06:31 PM
RE: Static Pages - by christaliise - 07-17-2015, 08:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB