Welcome Guest, Not a member yet? Register   Sign In
Link to view inside subfolder does not display correctly
#1

(This post was last modified: 09-06-2016, 10:04 PM by 678238515702801. Edit Reason: Plea for help my crops are dying )

I need to show a view that is inside a subfolder.

Just please tell me straight if "controller/method" is the only way and that "subfolder/view_file" is not possible in this context.

Here is my views structure:

Code:
practice/
   application/
       views/
           pages/
               item-1.php    
               item-2.php
               item-2/
                   subitem-1.php

PROBLEM:

I am able to show the top-level "pages/item-1.php" and "pages/item-2.php" but when I try to show "pages/item-2/subitem-1.php", it just shows the page for "pages/item-2.php".

I want "pages/item-2/subitem-1.php" to be shown correctly. However it is not.

routes.php

PHP Code:
$controller_views 'pages/view/';
$route['(:any)'] = $controller_views.'$1';
$route['pages/(:any)'] = $controller_views.'$1';
$route['item-2/(:any)'] = $controller_views.'item-2/$1';

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

.htaccess

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /practice/

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /practice/$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /practice/index.php/$1 [L]
</IfModule>

Controller

PHP Code:
<?php
class Pages extends CI_Controller
{
 
   function __construct()
 
   {
 
       parent::__construct();
 
   }

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

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


welcome_message.php
Code:
<div id="container">
   <ul>
       <li>
           <a href='item-1'>Item 1</a>
       </li>
       <li>
           <a href='item-2'>Item 2</a>
       </li>
       <li>
           <a href='item-2/subitem-1'>Item 2/SubItem-2</a>
       </li>
   </ul>
</div>

I do not understand, when I visit Pages::view() with URL "item-2/subitem-1" the variable $page=="item-2" when in my routes.php it is declared as "$route['item-2/(:any)'] = $controller_views.'item-2/$1';" so I expect it will find the view.

I have also tried doing this just to hardcode catch "pages/view/item-2/subitem-1" and still 404.


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

It has to do with the way CI handles URL's, namely in segments.
The first segment is the controller.
The seconde segment is the method inside that controller
All subsequent segments are parameters for the method.

So, the url "page/view/item-2/subitem-1" has 2 parameters, where your view method is expecting only one.
You should find a way to combine the segments that follow after segment 2.
See documentation about the URI class.
Reply
#3

See the CodeIgniter Users Guide:

URI Class

PHP Code:
$product_id $this->uri->segment(30); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(09-06-2016, 07:38 PM)678238515702801 Wrote: I need to show a view that is inside a subfolder.

Just please tell me straight if "controller/method" is the only way and that "subfolder/view_file" is not possible in this context.

Here is my views structure:

Code:
practice/
   application/
       views/
           pages/
               item-1.php    
               item-2.php
               item-2/
                   subitem-1.php

PROBLEM:

I am able to show the top-level "pages/item-1.php" and "pages/item-2.php" but when I try to show "pages/item-2/subitem-1.php", it just shows the page for "pages/item-2.php".

I want "pages/item-2/subitem-1.php" to be shown correctly. However it is not.

routes.php

PHP Code:
$controller_views 'pages/view/';
$route['(:any)'] = $controller_views.'$1';
$route['pages/(:any)'] = $controller_views.'$1';
$route['item-2/(:any)'] = $controller_views.'item-2/$1';

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

.htaccess

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /practice/

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /practice/$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /practice/index.php/$1 [L]
</IfModule>

Controller

PHP Code:
<?php
class Pages extends CI_Controller
{
 
   function __construct()
 
   {
 
       parent::__construct();
 
   }

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

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


welcome_message.php
Code:
<div id="container">
   <ul>
       <li>
           <a href='item-1'>Item 1</a>
       </li>
       <li>
           <a href='item-2'>Item 2</a>
       </li>
       <li>
           <a href='item-2/subitem-1'>Item 2/SubItem-2</a>
       </li>
   </ul>
</div>

I do not understand, when I visit Pages::view() with URL "item-2/subitem-1" the variable $page=="item-2" when in my routes.php it is declared as "$route['item-2/(:any)'] = $controller_views.'item-2/$1';" so I expect it will find the view.

I have also tried doing this just to hardcode catch "pages/view/item-2/subitem-1" and still 404.


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

Try:


routes.php

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


Pages.php (Controller)

PHP Code:
public function view($page$subpage '')
{
    
$page $page . ($subpage "/$subpage'');

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

 
   $this->load->view('template/header');
 
   $this->load->view("pages/$page");
 
   $this->load->view('template/footer');

[Just a programmer] Cool [/Just a programmer]
Reply
#5

It will never get to the second /(:any) because the first one will always execute any is a catch all parameter in the routes.

This is why I mentioned the uri segment method.

If he wants multiple parameters then use /(.+) instead of any, it will split the parameters after the /
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB