2

So I'm a newbie at this sort of thing - I just started developing with CodeIgniter and I'm trying to integrate javascript libraries. However, I have my .htaccess set up so that all requests get /index.php/ tacked in front of it, which makes it difficult to include files. For CSS I just use a php include to get around this problem and have it inline, which is faster anyway. But this seems like a bad idea for javascript libraries.

Any thoughts? Should I just create a index.php/ folder and stick it in there?

Thanks!
Mala

4 Answers 4

8

You can avoid the rewrite rule by just adding a condition to it:

RewriteCond $1 !^(index\.php|images|scripts|styles|robots\.txt)

Then you can put all your scripts, images, etc., in your docroot.

See the Apache Rewrite docs for more info.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! this solves my problem, and a few others i'd been having too :)
1

I actually use a resource controller to bring in all of my external files:

 class Resources extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function javascript()
    {
        $arr = func_get_args();
        if( sizeof( $arr ) == 0 )
        {
            show_404();
            return;
        }
        if( is_numeric( $arr[ sizeof( $arr ) - 1 ] ) )
        {
            array_pop( $arr );
        }
        $name = implode( "/", $arr );
        $this->load->view( "javascript", array( "importscript" => $name ) );
    }
    public function css()
    {
        $arr = func_get_args();
        if( sizeof( $arr ) == 0 )
        {
            show_404();
            return;
        }
        if( is_numeric( $arr[ sizeof( $arr ) - 1 ] ) )
        {
            array_pop( $arr );
        }
        $name = implode( "/", $arr );
        $this->load->view( "css", array( "importscript" => $name ) );
    }
    public function image()
    {
        $arr = func_get_args();
        if( sizeof( $arr ) == 0 )
        {
            show_404();
            return;
        }
        if( is_numeric( $arr[ sizeof( $arr ) - 1 ] ) )
        {
            array_pop( $arr ); 
            // if the last item is a number, that means it was 
            // automatically generated to prevent caching
        }
        $name = implode( "/", $arr );
        $this->load->view( "images", array( "importscript" => $name ) );
    }
}

The different views are all something like this:

$import = dirname( __FILE__ ) . "/javascript/$importscript";
if( !showjs( $import ) && is_dir( $import ) )
{
    if( !showjs( "$import/$importscript" ) )
    {
        show_404();
    }
}

function showjs( $imp )
{
    if( is_file( "$imp.js" ) )
    {
        header('Content-type: application/javascript');
        echo "/*----- Imported into PHP so JavaScript can all be dynamically declared -----*/\n\n";
        echo file_get_contents( "$imp.js" );
        return true;
    }
    elseif( is_file( "$imp.php" ) )
    {
        header('Content-type: application/javascript');
        echo "/*----- Imported into PHP so JavaScript can all be dynamically declared -----*/\n\n";
        include_once( "$imp.php" );
        return true;
    }
    return false;
}

As you can see, the controller passes the file name to the view. The view then sees if there is a js file or a php file associated with the importscript variable. If there is, it sets the headers and then displays the files.

1 Comment

This looks useful, but is overkill for my needs
1

If you are using mod_rewrite, just add a RewriteCond in front of your RewriteRule. For example:

RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|png|gif)$
RewriteRule ^([^/]+)/([^/]+)/? index.php?ctrl=$1&event=$2 [L,QSA]

Comments

0

Maybe i'm missunderstanding something but why not just include javascript libraries with absolute urls

<script type="text/javascript" src="/js/javascript.js"></script>

Same with CSS

<link rel="stylesheet" type="text/css" href="/css/main.css">

if you start at the doc root using / then you won't have any problems finding files.

1 Comment

because when the user's browser sends a request for /js/javascript.js it will be forwarded to /index.php/js/javascript.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.