• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple sub-directory pages

#1
Hello, As I am a beginer with CI, I am having a issue by defining sub pages which are in the sub folders.

What I want :
  • Directory should be like : application/views/pages/hosting/hosting-about.php
I am successfully done working with : application/views/pages/aboutme.php

My Pages controller :
Code:
class Pages extends CI_Controller{

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

       if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
         show_404();
       }

       $data['title'] = ucfirst($page);

       $this->load->view('partials/header');
       $this->load->view('pages/'.$page, $data);
       $this->load->view('partials/footer');

     }

     // public function subview($page = 'hosting'){
     //
     //   if(!file_exists(APPPATH.'views/pages/hosting/'.$page.'.php')){
     //     show_404();
     //   }
     //
     //   $data['title'] = ucfirst($page);
     //
     //   $this->load->view('partials/header');
     //   $this->load->view('pages/hosting/'.$page, $data);
     //   $this->load->view('partials/footer');
     //
     // }

   }

My Routes :
Code:
$route['default_controller'] = 'pages/view';


$route['(:any)'] = 'pages/view/$1';
//$route['(:any)'] = 'pages/subview/$1';
//$route['hosting/(:any)'] = 'hosting/view/$1';
//$route['hosting/(:any)'] = 'pages/hosting/view/$1';
//$route['(:any)/(:any)'] = "pages/hosting/view/$1";


$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


With this configuration I can access : http://localhost/mysite/contact/
But I want to access like this : http://localhost/mysite/hosting/hosting-about.php

There is no internal relation between pages like parent or child, I just want to access through the sub folder within the pages folder.

As I am new with CI, please help me with the broad explanation and from best practice perspectives. 
Thanks in Advance !
Reply

#2
In CI, multiple segments after the controller/method path are handled as subsequent parameters.
One solution could be this:
PHP Code:
public function view($page 'home'$subview=NULL){

 
 if ($subview) {
 
   $view $page '/' $subview //$page is handled as a folder
 
   $title ucfirst($subview);
 
 }
 
 else {
 
   $view $page //$page is handled as a file
 
   $title ucfirst($page);
 
 }

 
 if(!file_exists(APPPATH 'views/pages/' $view 'php')){
 
   show_404();
 
 }
 
 
  $data
['title'] =$title;
 
 $this->load->view('partials/header');
 
 $this->load->view('pages/'.$view$data);
 
 $this->load->view('partials/footer');

Reply

#3
I have done this by doing something like :

Code:
public function view($page = 'home'){

       if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
         show_404();
       }

       $data['title'] = ucfirst($page);

       $this->load->view('partials/header');
       $this->load->view('pages/'.$page, $data);
       $this->load->view('partials/footer');

     }

     public function subview($page = 'hosting'){

       if(!file_exists(APPPATH.'views/pages/hosting/'.$page.'.php')){
         show_404();
       }

       $data['title'] = ucfirst($page);

       $this->load->view('partials/header');
       $this->load->view('pages/hosting/'.$page, $data);
       $this->load->view('partials/footer');

     }

And route :
Code:
$route['default_controller'] = 'pages/view';


$route['(:any)'] = 'pages/view/$1';
$route['hosting/(:any)'] = 'pages/subview/$1';

But I will probably get the problem when I will have another folder inside pages/ , in that case I have set another route for that.

So, yeah your way is a better solution, 
Can you show me how I am going to manage route in your case ?

Thanks for the help.
Reply

#4
Quote:So, yeah your way is a better solution, 
Can you show me how I am going to manage route in your case ?
You don't need any specific route to handle the url.
Code:
$route['(:any)'] = 'pages/view/$1';
is just fine I guess.
Reply

#5
It's not working when I am trying to access http://localhost/mysite/hosting/web-hosting.php
It says not found !
Reply

#6
Quote:It's not working when I am trying to access http://localhost/mysite/hosting/web-hosting.php

In CI, you don't refer to a php file, but to a controller and a method.

The correct url should be:
Code:
http://localhost/mysite/hosting/web-hosting

In this case, CI is looking for a controller called "hosting" and a method inside that controller called "web-hosting", unless you've setup routes to route the url to a different controller (and method).
Reply

#7
I know in CI, we don't refer to directly .php file, I was just saying that I couldn't access that page.
and yeah your solution still not working.
Can you check again please, thanks for the help
Reply

#8
Try this route:
PHP Code:
$route['hosting/(:any)'] = 'pages/view/hosting/$1'

In my example view function, there was a dot missing:
PHP Code:
 if(!file_exists(APPPATH 'views/pages/' $view 'php')){ 

To make it work, insert a . before php:
PHP Code:
 if(!file_exists(APPPATH 'views/pages/' $view '.php')){ 

I tested this, and on my localhost it works.
Is this how you want it to work?
Reply

#9
Your 'not found' message, is that the Codeigniter 404 message like so:

[Image: HDFFodA.png]

Then you indeed have some routing issues.

Or, if you are seeing such an error more like:

[Image: HkrTrQC.png]

Then your CodeIgniter app is not loaded at all and it's (probably) a .htaccess problem.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.