CodeIgniter Forums

Full Version: Newbie, failed at the first hurdle!!!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm following the tutorial and I have reached "Adding logic to the controller"

This says at the following code to load the page, but my question is, where do I add this code?
Sorry if I am being a bit thick, but it doesn't say where to put it!!
Thanks for your patience

Ian

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

In the Page Controller that was created in the beggining of the tutorial.
PHP Code:
<?php

// Page Controller class 

class Pages extends CodeIgniter\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);
        } 

You can use this code for adding this code for check view file

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    function __construct() {
        Parent::__construct();        
    }
    
    public function index($page = 'home') {

        echo APPPATH.'views/pages/'.$page.'.php'; die;
        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);
    }
}

But this is not correct way to handle view file.
(01-08-2018, 11:34 PM)XtreemDeveloper Wrote: [ -> ]    function __construct() {
        Parent::__construct();        
    }

Don't do this, it's pointless.

(01-08-2018, 11:34 PM)XtreemDeveloper Wrote: [ -> ]But this is not correct way to handle view file.

So don't suggest it then. The thread is about the tutorial anyway, not asking for random code.