Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions (HMVC) and JavaScript Files
#1

[eluser]Unknown[/eluser]
So, I've been working with CI for a while and recently started working with Modular Extensions; however, I'm having some trouble with accessing my JavaScript files.

The idea of Modules is to have a self contained program, so I do not want to place my module's asset files in the web root. I'd like to just take the module, slap it in place and have it work. Does anyone know how to load a javascript file in an html header from the module's folder?

Here's my structure:

app
- modules
- - MY_module
- - - js
- - - - module_script.js
- - - controllers
- - - models
- - - views
#2

[eluser]toopay[/eluser]
In your template, simply add some js file flag, example you have template.php as your main template file in your view folder, then..
Code:
<html>
<head>
<title><?php echo $title ?>
<?php echo $js_files ?>
<!-- and so on --->
Then, provide a function which render the js tag, including the file path, and merge that along side with other data you send to your view, like:
Code:
//...
$data['js_files'] = $this->some_function(array('path/to/your/js/files1',));
$this->load->view('foo', $data);

I ussually read those file content, and merge it as one big cache js files, save it to some cache folder, then send the "cached" path to the view. If you consider do that too, some member already give a nice example library to do that.
#3

[eluser]Unknown[/eluser]
Thanks for the reply, but I'm still having trouble with accessing the files. When I put in the path/to/my/jsfile, it gives me a "access forbidden" message from my server. Is there something I need to set up in the .htaccess file to allow access to those folders or some other trick?
#4

[eluser]mattex123[/eluser]
Why do you want to put your js files there? Your js files needs to be public, just like your images and css files. In other words, NOT in the applications directory. You can still modularize your js files, just do it in a public folder.
#5

[eluser]InsiteFX[/eluser]
That's because the application directory is protected with an .htaccess file!

You would need to modify the .htaccess file or delete it but then it will not protect your application directory.

InsiteFX
#6

[eluser]Juan Ignacio Borda[/eluser]
My 2 cents:
The point of HMVC is to have modules which are portable, so in order to have a self contained module you need to put css and images an whatever resources you need, into your module folder

so I've modified the .htaccess to look like this

Code:
RewriteEngine on
RewriteCond $1 !(index\.php|images|robots\.txt|user_guide|jscript|css)
RewriteRule ^(.*)$ /ci2/index.php/$1 [L]

The only difference is that I've deleted de "^" character wich match starting urls. so allowing to access all those words at deeper levels.
#7

[eluser]InsiteFX[/eluser]
All resources should be in the root under an assets folder!
The CodeIgniter folders are protected with an .htaccess file for un-autorized use by the public.

The point of HMVC is to module application code not resources. Resources are almost always
global to the application in one place for easy access of them.

So you are just opening up yourself for someone to hack into your CodeIgniter directories.
#8

[eluser]Juan Ignacio Borda[/eluser]
I do agree with you partially (I want my modules to be portable and self contained) so I'm coding around to find a less dangerous way. I'll post it later.

PD: from: https://bitbucket.org/wiredesignz/codeig.../wiki/Home

"... Modular Extensions makes modules 'portable' to other installations "
#9

[eluser]Aken[/eluser]
I'd take a page from ExpressionEngine and have two folders for your modules - module (the actual PHP logic), and theme. Themes would go in your root near your other assets, and modules would continue to be in the application structure, wherever that might be.
#10

[eluser]Juan Ignacio Borda[/eluser]
I came out with this solution:

put all your resources under a folder assets in your module like this:

module_name
assets
jscript
images
css
xml
json

then create a controller in your module called assets.php
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
* ASSETS Controller
* This file allows you to  access assets from within your modules directory
*
* @author Borda Juan Ignacio
*
* @version  1.0 (2012-05-27)
*
*/

class assets extends CI_Controller {

    function __construct() {
        parent::__construct();
        //---get working directory and map it to your module
        $file = getcwd() . '/application/modules/' . implode('/', $this->uri->segments);
        //----get path parts form extension
        $path_parts = pathinfo( $file);
        //---set the type for the headers
        $file_type=  strtolower($path_parts['extension']);
        
        if (is_file($file)) {
            //----write propper headers
            switch ($file_type) {
                case 'css':
                    header('Content-type: text/css');
                    break;

                case 'js':
                    header('Content-type: text/javascript');
                    break;
                
                case 'json':
                    header('Content-type: application/json');
                    break;
                
                case 'xml':
                   header('Content-type: text/xml');
                    break;
                
                case 'pdf':
                  header('Content-type: application/pdf');
                    break;
                
                case 'jpg' || 'jpeg' || 'png' || 'gif':
                    header('Content-type: image/'.$file_type);
                    break;
            }

            include $file;
        } else {
            show_404();
        }
        exit;
    }

}

then in your view you can call the resource you want like this:

Code:
<link rel="stylesheet" type="text/css" href="module_name/assets/css/css_file.css" />

The "assets" controller will give you back the file with the proper headers according to type, making your MVC+assets self contained ;-).






Theme © iAndrew 2016 - Forum software by © MyBB