开发者

CodeIgniter and JS libraries

So I'm a newbie at this sort of thing - I just s开发者_如何学Gotarted 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


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.


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.


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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜