how to load js files kept inside modules folder in codeigniter4

Viewed 54

I am using codeigniter4 in HMVC architecture, there I am creating modules and for each module I am having one js folder inside view and I want to load the script.js but I am unable to set the path. I am using below mentioned folder structure

App
Modules
    Home
        Controllers
            Home.php
        Models
            Home_db.php
        Views 
            js -> script.js
            index.php
    Login
        Controllers
            Login.php
        Models
            Login_db.php
        Views 
            js -> script.js
            index.php 

Need to set the route for the same, I am loading script.js dynamically while loading a specific module.

1 Answers

Assets files like js, css or images, should not be inside your modules. They should be in your public folder. The modules folder shouldn't be visible to the outside world.

So in your public folder created something like:

public/modules/login/js/script.js

Then in your module load the js as normally like:

<script src="<?php echo base_url('modules/login/js/script.js') ?>"></script>
Related