Reputation: 33
I have problem with my codeiginiter project. I use xamp and I have my project folder in htdocs. To see my project i must type http://localhost/myproject/ I want to add some assets to project and I crete folder to do that.
- htdocs
-- myproject
---- application
-----controllers
-----models
----- views
----- assets // folder with js, css ect
---- system
---- index.php
---- .htaccess
Now when I want to load script in specyfic view, like this
<script src="../assets/js/app.js"></script>
I got error 404 and in console
GET http://localhost/assets/app.js
As you can see CodeIgniter want to download assets from the wrong place.
My htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Upvotes: 1
Views: 11424
Reputation:
Wrong place to have your assets folder. Your assets folder should be in the main directory and not your application directory. As the htaccess file in application blocks css and images etc
Place your assets
application
assets
assets > css
assets > images
assets > js
index.php
.htaccess <-- Main directory htaccess file
Then autoload the url helper http://www.codeigniter.com/user_guide/helpers/url_helper.html
And then you would be able to use <?php echo base_url('assets/js/app.js');?>
you don't have to autoload the url helper but it would save you time other wise you will have to load it in each controller you need it in.
Upvotes: 5
Reputation: 2362
codeigniter has some function for this purpose like : base_url() or site_url()
those function have some difference that you can see it in :
what is the difference between site_url() and base_url()?
you can use this for your script src :
<?php
echo '<script src="'.base_url('assets/js/app.js').'"></script>';
?>
Upvotes: 1
Reputation: 159
Try this once. This is better way to access your scripts
<script src="<?php echo site_url('assets/js/app.js'); ?>"></script>
Upvotes: 0