Codeigniter assets folder best practice
another angle on this -- and i think what they were trying to tell you to do - is putting your application & system folders one level up from the "public" html folder. that way your source files are not accessible. so like in Mamp the public folder is called htdocs, in most web hosting its called html.
/application/html/
/system /html/
// your main index page is in the public html folder
/.….. /html/index.php
// an assets folder in the public html folder with css, img, etc folders
/.…… /html/assets/css/
/.…… /html/assets/img/
then in your index.php, the path is going one level 'up' like
$system_path = '../system';
$application_folder = '../application';
BONUS - here are two tips that have helped me a lot.
1) Name your application folder, then you can easily switch between different versions,
'roll back' very quickly, etc.
/app_alpha/html/
/app_beta01/html/
/app_beta02/html/
2) Put the base url on the index.php page -- not in the config.
$assign_to_config['base_url'] = 'https://awesomedomain.com';
that way you can have a local development version, separate live server version -- and you never have to worry about over writing the base url because its on the index.php page - not in the config.
In codeigniter, how to make assets folder work by putting it in the application folder?
The best practice in codeigniter for set up the assets as below flow:
- application
- assets
- system
But you want:
- application > assets > css > style.css
- system
but if you want to keep the assets folder inside the application, then you've to do some extra functionality as below:
First, make sure that you've loaded the URL helper in your controller which is:
$this->load->helper("url");
you have to first create a helper named "my_helper.php" at "application/helpers" directory with this code:
if ( ! function_exists('asset_url()'))
{
function asset_url() {
return base_url().'application/assets/';
}
}
Now, you have to load this helper into your controller as below:
$this->load->helper("my_helper");
Now replace your .htaccess code at 'application/'' directory with the below code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
After doing all the above functionality, you have to declare your assets in view as below:
<link rel="stylesheet" href="<?php echo asset_url(); ?>css/style.css">
how to setup the assets folder in CodeIgniter like in Laravel
In fact the answer is very simple, you only need to put ../ before every file that is under assets folder, because the root of the codeigniter is in application folder, but how you can't put the assets folder there and for the best practices, let the structure of the project like below and apply this solution to the path of the files:
Website Folder structure
|---|application\
|-------(CodeIgniter application folders...)
|---|assets\
|-------css\
|-------images\
|-------js\
|-------robots.txt
|---|system
|-----(CodeIgniter system folder...)
|index.php
Path of the files
ex.:
CSS
<link href="../assets/css/bootstrap.min.css" rel="stylesheet">
<link href="../assets/css/plugins/font-awesome.css" rel="stylesheet">
IMAGES and others
<img src="../assets/images/build/banner.png">
JAVASCRIPT
<script src="../assets/js/jquery-2.1.4.js"></script>
<script src="../assets/js/plugins/jquery.scrollSpeed.js"></script>
Note.:
Always remember to applay the best practices to the code too (this will help for the performance of the the page too), like below:
<html>
<head>
meta tags
favicons
css files styles
</head>
<body>
page structure code
javascript files
</body>
</html>
Best practice for assets in codeigniter
Well, isn't it dangerous to have the data on the root level of the directory structure? All that is protecting the key folders (e.g. system) from outsiders is a .htaccess
Assuming you're running Apache, .htaccess
with deny from all
should be fine.
If not, this is one reason why you usually see something like this at the beginning of each file:
defined('BASEPATH') or exit('No direct script access.');
This ensures the script can't be accessed directly.
You may also be able to keep your application and system files below the web root, something like this:
htdocs
application
system
public_html
index.php
public
css
js
img...
Just use a relative path in index.php to define your CI paths:
$system_path = '../system';
$application_folder = '../application';
How to protect ASSETS FOLDER in codeigniter
You can do two things
- via
.htaccess
(Not Tested) - via
index.html
1. .htaccess
add .htaccess
inside the assets folder
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
2. index.html
Create index.html
file inside the directory and add this
<!DOCTYPE html>
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>
Output
When some one access outside you get this on browser
Directory access is forbidden.
Where should assets go in a CodeIgniter project?
I usually put separate folders at the root level, so I end up with a directory structure like this:
/system
/css
/js
/img
Seems to work for me - when you use site_url(url)
, the URL it generates is from the root, so you can use site_url('css/file.css')
to generate URLs to your stylesheets etc.
In an external assets folder the best place to put libraries for CodeIgniter 2.x?
As long as they are both publicly accessible then there is no difference between
application
...
system
...
assets
css
style.css
images
image.png
and
application
...
system
...
css
style.css
images
image.png
Depending on your .htaccess file you may need to make a change but CodeIgniter doesn't require you to use any specific scheme for storing assets.
Related Topics
Exclude Hidden Files from Scandir
Check If Two Arrays Have the Same Values
How to Make a Catch-All Route in Laravel
How to Prefix a Positive Number with Plus Sign in PHP
Python Format Datetime with "St", "Nd", "Rd", "Th" (English Ordinal Suffix) Like PHP's "S"
Phpmysql Error - #1273 - #1273 - Unknown Collation: 'Utf8Mb4_General_Ci'
Can Closing the Browser Terminate the PHP Script on the Server
Error 500: Premature End of Script Headers
MySQL Stored Procedure VS. Complex Query
What's the Performance Cost of "Include" in PHP
Warning: MySQLi_Real_Escape_String() Expects Exactly 2 Parameters, 1 Given... What I Do Wrong
PHP - Flushing While Loop Data with Ajax
PHP Pdo with Foreach and Fetch
PHP Shell_Exec() Command Is Not Working
Premature End of Script Headers: Index.Php, Mod_Fcgid: Read Data Timeout in 61 Seconds