CodeIgniter Forums

Full Version: Individual html/php files and CI side by side
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]felyx[/eluser]
What I mean by that is when I have to palce one or more individual html or php files to the root directory where I have my index.php and display them as a standalone page (not with CI). With CodeIginiter just by copying them there they won't display. For example, On my website I have a home page. I can access it by the address http://mysite.com/home. If I place an individual html file like standalone.html I cannot access it by the address http://mysite.com/standalone.html because it is not a controller and I dont want it to be one. I could allow it in htaccess to be accessible but if I have like 30 of them it is a problem. I do not want to put them in a directory and allow them in htaccess either. Unfornutaly sometimes a customer wants that so if anyone could possibly help me with an easy solution I would thank that.

El Forum

[eluser]nfx-nano[/eluser]
What's wrong with placing the files in a dir and adding the dir to .htaccess?

El Forum

[eluser]felyx[/eluser]
[quote author="siric" date="1223271130"]What's wrong with placing the files in a dir and adding the dir to .htaccess?[/quote]

There are plenty reasons for that but for one, the content, usually all files have different type of content. And as I told you I do not want to solve it by htaccess if it is possible.

El Forum

[eluser]Frank Berger[/eluser]
the fastest way would be to change your .htaccess (which is already there, no?) to something like this:
Code:
RewriteEngine on
# RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]

If you want or need to do it through CI, you have the following option (roughly, i didn't play it all through):

First, you create the following file:
application/libraries/MY_Router.php:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
    function MY_Router()
    {
        parent::CI_Router();
    }
    function _validate_request($segments) {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))    {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0) {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))    {            
                    if (!empty($this->routes['fallback_controller']) && file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->routes['fallback_controller'].EXT))  {
                        array_unshift($segments,$this->routes['fallback_controller']);
                    } else {
                        show_404($this->fetch_directory().$segments[0]);
                    }
                }
            }  else {
                $this->set_class($this->default_controller);
                $this->set_method('index');
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))    {
                    $this->directory = '';
                    return array();
                }
            }
            return $segments;
        }    else if (!empty($this->routes['fallback_controller']) && file_exists(APPPATH.'controllers/'.$this->routes['fallback_controller'].EXT))  {
            array_unshift($segments,$this->routes['fallback_controller']);
            return($segments);
        }
        // Can't find the requested controller...
        show_404($segments[0]);
    }
}
// END Router Class

/* End of file MY_Router.php */
/* Location: ./application/libraries/MY_Router.php */

Now you can specify a fallback-controller in your
application/config/routes.php
$route['fallback_controller'] = "plainfile";

so, what effectifly happens now is that instead of the 404 errorpage your fallback controller is called

now you create
application/controllers/Plainfile.php

Code:
<?php

class Plainfile extends Controller {

    function Plainfile() {
        parent::Controller();    
        $this->load->helper('file');
    }

    function _remap($method) {
        global $URI;
        $file = $URI->uri_string;
        $content = read_file('.'.$file);
        if (empty($content)) show_404($file);
        $info = get_file_info('.'.$file);
        $mime = get_mime_by_extension($file);
        $this->output->set_header("HTTP/1.0 200 OK");
        $this->output->set_header("HTTP/1.1 200 OK");
        $this->output->set_header("Content-type: ".$mime);
        $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $info['date']).' GMT');
        $this->output->set_output($content);
    }
}
?>

This controller only has the 'magic' _remap method, which is called instead of anything else. Here you can handle the file as you like, modify it, or just output it..

Only drawback with this is that you can't have a normal file or directory with the same name as one of your controllers, as the controller would be called first. but then again, this might not be bad as well.

BTW, I propose that the 'fallback_router' is implemented as a standard in the Router class.

have fun

Frank

El Forum

[eluser]Phil Sturgeon[/eluser]
The first one is definitely easier, but a smart solution there Frank.

El Forum

[eluser]felyx[/eluser]
[quote author="Frank Berger" date="1223288904"]the fastest way would be to change your .htaccess (which is already there, no?) to something like this:
Code:
RewriteEngine on
# RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]
[/quote]

Thank you very much, I choose the htaccess method and it works. It is really an easy choice, do not have to enable all single files/folders individually. Thank you again.

Edit: I had to change the last line to look like this, else it would not work for me:
Code:
RewriteRule ^(.*)$ /index.php?/$1 [L]