Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load assets in Code Igniter such as JS files and CSS files

Given the file structure and using WAMP

www
   application
       \views
           \templates
               header.php
   assets
       \js
          index.js
       \css
          index.css

I was trying to follow along the example given in this solution, Codeigniter: How to include javascript files, however I have been unsuccessful.

header.php

<html>
    <head>
            <?php $this->load->helper('url');?>
            <script type="text/javascript" src="<?php echo base_url();?>assets/js/index.js" ></script>
            <title>CodeIgniter Tutorial</title>

    </head>
    <body>


            <h1><?php echo $title ?></h1>

the error thrown in the browser is

GET http://localhost/index.php/localhost/assets/js/index.js 404 (Not Found)

header.php is being called, as it's the my only php file with a js reference, sorry if I misinterpreted the other guy's solution I really can't understand why it's not working

like image 286
steff_bdh Avatar asked Oct 20 '22 04:10

steff_bdh


2 Answers

Locate your httpd.conf file.

In my case it is located in C:\wamp\bin\apache\apache2.4.9\conf

Please uncomment LoadModule rewrite_module modules/mod_rewrite.so

In your config.php located in application\config\config.php set the following value.

$config['base_url'] = '';

One of the reason to answer why I uncomment mod_rewrite.so, here's the explaination:

like image 79
Charlesliam Avatar answered Nov 01 '22 12:11

Charlesliam


On constants.php below config.php

Open constants.php and write a piece of this code for your ease

define('ROUTE_STIE_PATH','`http://localhost/yoursitename/`');
define('ADMIN_CSS_DIR_FULL_PATH',ROUTE_STIE_PATH.'assets/css/');
define('ADMIN_JS_DIR_FULL_PATH',ROUTE_STIE_PATH.'assets/js/');

on header

<script src="<?php echo ADMIN_JS_DIR_FULL_PATH; ?>index.js" type="text/javascript"></script>
like image 23
Mohmmad Ali Avatar answered Nov 01 '22 13:11

Mohmmad Ali