CodeIgniter Forums
Stuck at tutorial - "Adding logic to the controller" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Stuck at tutorial - "Adding logic to the controller" (/showthread.php?tid=67469)



Stuck at tutorial - "Adding logic to the controller" - 00253 - 02-25-2017

Hello, I wanted to learn a PHP framework and started with CI today, but I got stuck at the tutorial about static pages.
The subchapter "Adding logic to the controller" wants me to add a check - I assume to add it into a controller - that the page to be loaded actually exists.


Tutorial says: "In order to load those pages, you’ll have to check whether the requested page actually exists: "
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);


But I can't figure out where this code goes. In my controllers folder, there is pages.php and welcome.php.

Thank you for your help Blush


RE: Stuck at tutorial - "Adding logic to the controller" - Wouter60 - 02-25-2017

This code goes into your pages controller.
There already is a function called "view". The code the tutorial describes here, is taking you a step further the make the function load a view file only if the file really exists.


RE: Stuck at tutorial - "Adding logic to the controller" - 00253 - 02-25-2017

(02-25-2017, 09:17 AM)Wouter60 Wrote: This code goes into your pages controller.
There already is a function called "view". The code the tutorial describes here, is taking you a step further the make the function load a view file only if the file really exists.

Thank you, everything is working as intended now :-)


RE: Stuck at tutorial - "Adding logic to the controller" - wolfgang1983 - 02-25-2017

(02-25-2017, 04:01 AM)00253 Wrote: Hello, I wanted to learn a PHP framework and started with CI today, but I got stuck at the tutorial about static pages.
The subchapter "Adding logic to the controller" wants me to add a check - I assume to add it into a controller - that the page to be loaded actually exists.


Tutorial says: "In order to load those pages, you’ll have to check whether the requested page actually exists: "
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);


But I can't figure out where this code goes. In my controllers folder, there is pages.php and welcome.php.

Thank you for your help Blush


https://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views

General Topics

https://www.codeigniter.com/user_guide/general/index.html