• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Static files controller

#1
[eluser]antonagestam[/eluser]
Hi there!

I'm quite new around here so please tell me if I do something wrong!

I've put together a controller that handles static files, like CSS, images and javascript files. There might already be some solution for this but the little resarch I did didn't find any. Please tell me if you find this useful!

You will need to redirect all requests to the 'static' folder to this controller. You also have to define the following variables in the config file:

Code:
// the folder where you keep your static files, defaults to 'static'
$config['static_files_dir'] = "static";
// defines in wich segment your static folder is. if you use mod_rewrite, it's probably 2
$config['static_files_starting_segment'] = 2;

Here is the controller:

Code:
<?php
    class Static_files extends Controller
    {
        
        public static $data = "";
        public static $file_name = "";
        
        public function __controller()
        {
        
            parent::Controller();
            $this->Index();
            
        }
        
        public function Index()
        {
            
            $path = $this->get_path();
            $file_status = $this->get_file($path);
            
            if( $file_status === true )
            {
                
                $this->set_output();
                
            }
            else
            {
                
                show_404();
                
            }
            
        }
        
        // @ Returns true on success or false as boolean on fail
        private function Get_file($full_path)
        {
            
            if( file_exists( $full_path ) )
            {
            
                $this->data = read_file( $full_path );
                $this->file_name = basename( $full_path );
                return true;
                
            }
            else
            {
                
                $this->data = (bool) false;
                $this->file_name = (bool) false;
                return false;
                
            }
            
        }
        
        private function Get_path()
        {
            
            $path = $this->config->item('static_files_dir') . "/";
            $starting_segment = $this->config->item('static_files_starting_segment');
            
            for( $int = $starting_segment;$int <= $this->uri->total_segments();$int ++ )
            {
                
                $path .= $this->uri->segment($int) . "/";
                
            }
            
            return trim( $path, "/" );
            
        }
        
        private function Set_output()
        {
            
            // Get mime type
            $mime = get_mime_by_extension( $this->file_name );
            
            // Set content-type header
            $this->output->set_header('Content-type: '.$mime);
            
            // Set output
            $this->output->set_output($this->data);
            
        }
        
    }

#2
[eluser]the_yk[/eluser]
hi antonagestam,

I can't see why would we need a controller that handle a static file while we can use a direct url to that file.

#3
[eluser]Marfo[/eluser]
If it's just for css/js, take a look at my loader extension ;-)

#4
[eluser]antonagestam[/eluser]
[quote author="the_yk" date="1260249499"]hi antonagestam,

I can't see why would we need a controller that handle a static file while we can use a direct url to that file.[/quote]

Well, if you don't the client will not be able to cache those files and you can't transfer them gzipped. That is kind of the thaught behind this, though you'll have to add an expires header yourself.

#5
[eluser]antonagestam[/eluser]
[quote author="Marfo" date="1260285944"]If it's just for css/js, take a look at my loader extension ;-)[/quote]

Well it's not just for css/js it's for any static files that you would want to add an expires header to or transfer gzipped.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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