I’m new using codeigniter. I using codeigniter 2.X. I have some question regarding basic course of codeigniter.
What the best suggestion for folder location of our external css/ js /image/another ? For now, i put js/css/or another file on the root, i.e site url/css
How to describe path of image on css and javascript file ? On php its easily that just use <?php echo base_url(); ?> but how in css and javascript?
Both question, I need for the best and experience answer because the answer will be my knowledge base for using codeigniter.
Thank you so much.
Store your css/js/image in assets folder or as u want
/www
/code_igniter
/application
/assets
+img
+css
+js
/controllers
/system
then use it in your view as including it
<link type="text/css" rel="stylesheet" href='<?php echo base_url()?>assets/css/mystyle.css'>
<script type="text/javascript" src='<?php echo base_url()?>assets/js/myjs.js'></script>
into your .htacess file add this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
put .htaccess file outside the application folder
Related
My website is working fine.
All controllers works fine unless I put a slash at the end the pages broke.
Here is oringal URL: Saaf.Pk
But when I put a slash at the end, style & images broked: Here
I know the problem is that URL path got changed. But what's the general solution for this situation?
My .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
are you using base_url to define every image, js, css etc?
example img
<img src="<?=base_url('assets/uploads/cms/') . $cms->logo_2?>" alt="Logo">
example js
<script src="<?=base_url('assets/plugins/jquery-3.3.1/jquery.min.js')?>"></script>
example css
<link href="<?=base_url('assets/guide/css/hopscotch.css')?>" rel="stylesheet"/>
First of all, set the base_url in config folder config.php location in application->cofig->config.php and use as $config['base_url'] ='https://saaf.pk/';
Than give the link of images, css and js as ">
Don't forget to use the helper URL in autoload.php file.
I am using Codeigniter on XAMPP server in Windows. I removed index.php from url using .htaccess and Apache mod_rewrite as below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|favicon\.ico|robots\.txt|css|js)
RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
</IfModule>
My folder structure is:
htdocs\mysite\index.php
htdocs\mysite\application\...
htdocs\mysite\system\...
htdocs\mysite\js\...
htdocs\mysite\css\...
...
I have a controller named "main" that is default controller and another controller named "news" and two functions named "index" and "show" in each. All controller functions use a single view file named "header.php" that looks for two file:
<link type="text/css" rel="stylesheet" media="screen" href="css/default.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
But what is the problem? Only main/index could access to .js and .css files. main/show, news/index and news/show look for a url like:
http://localhost/mysite/news/js/jquery.min.js
http://localhost/mysite/news/css/default.css
If I use "../js/jquery.min.js" and "../css/default.css" in header.php the problem will be solved except for main/index. I don't want to use separate header files for main/index and other controllers and functions, because the header is the same in all of them. Is there a solution using Apache mod_rewrite or Codeigniter routing config to solve this problem?
Thank you in advance
Option 1: use full path relative to site
<script type="text/javascript" src="/mysite/js/jquery.min.js"></script>
Option 2: use base_url() (must be set in config.php)
$config['base_url'] = 'http://www.example.com/mysite/';
-
<script type="text/javascript" src="<?php echo base_url() . "js/jquery.min.js"; ?>"></script>
This isn't an issue with routing or your .htaccess.
If you don't mind do this
<script src="<?php echo site_url('js/jquery.min.js'); ?>"></script>
Instead of this :
<script type="text/javascript" src="<?php echo base_url() . "js/jquery.min.js"; ?>"></script>
And remove the type="text/javascript" it's not required
Lets say I'm using Apache and I have a sample web folder like:
http://myserver.com/test/
In this folder I have the following files:
index.html
img.php
sample.jpg
.htaccess
The index.html have the following code:
<html>
<body>
<img src="sample.jpg" />
</body>
</html>
The img.php have the following code:
<?php
if(!isset($_GET["path"])){
exit()
}
?>
<html>
<body>
<img src="<?php echo $_GET["path"] ?>" />
</body>
</html>
I like http://myserver.com/test/sample.jpg to show http://myserver.com/test/img.php?path=sample.jpg content
(with no URL change)
After a lot of search on the net I wrote the following code but it does not work :/
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
RewriteCond %{SCRIPT_FILENAME} \.(php|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)$ /test/img.php?path=$1 [L]
There is probably an ! to put on the third line but I do not know exactly where
Change your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /test/
# block direct access to images
RewriteCond %{HTTP_REFERER} !^http://(www\.)?myserver\.com/ [NC]
RewriteRule ^(.+?\.(?:png|gif|jpe?g))$ img.php?path=$1 [L,NC]
To output an image as base64, you would use something like this:
<img src="data:image/jpg;base64,<?php echo base64_encode(file_get_contents('/path/to/image.jpg'));?>"/>
Remember that's server path, not document root, so you may need to add $_SERVER['DOCUMENT_ROOT'] in front of your normal image file path
Hello I have created a new subdomain of my domain and I am building a code igniter application. Everything is working OK except the fact that I cannot target the stylesheet or any other file in the application.
I have structure like this:
Root
/application
/css
/images
/js
/system
/userguide
Inside the css folder I have a style.css file.
I have tried to add this stylesheet to my application and it is not possible.
The strange thing is that even when I the url directly in the browser it sais that it cannot find the file.
I am on Bluehost, not sure if that has something to do with the configuration of the server or the configuration of the codeigniter.
Please help as I have tryied every possible method.
Thanks to all in advance!
Change your htaccess file to
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon \.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
This is standard which i have been using in my many CI Apps and it works fine :)
Also this htaccess file should be alongwith application, system folder(outside application folder)
Insert following line in your html file:
<link href="css/style.css" rel="stylesheet">
Double check that you have copied the file with correct name in css folder.
I assume that your other settings are correct.
At first it should be better to put images, css and js folders in a folder named "public" on the root.
Then you can use this code:
<LINK href= "<?=site_url(); ?>public/css/style.css" rel="stylesheet" type="text/css">
Another way you can include a css file is by using the template library.
If the problem insists then you may change your root .htaccess file like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
I have managed to resolve my own problems after reading the links that Jigar pointed me to.
My solution is to add in the htaccess file of the root:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
This question already has answers here:
Where do I put image files, css, js, etc. in Codeigniter?
(14 answers)
Closed 9 years ago.
I am doing some testing with CI and have it running in a folder. http://www.example.com/sub-folder/ for instance
My directory structure is like this (where / is sub-folder/):
/
|-/system
|-/application
I have eclipse as my IDE with 2 projects, 1 for system, 1 for application, where application is my project that includes a reference to system.
My .htaccess file prevents access to system directory. I would like to have my images and CSS placed into: application/resources/images and application/resources/css respectively. I would like to be able to use <link rel="stylesheet" href="/sub-folder/css/style.css" /> However, after spending some time testing and searching, I haven't been able to get the CSS file to load from the application directory.
What do I need to change or add in my .htaccess to be able to do this?
The contents of my .htaccess follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Instead of
<link rel="stylesheet" href="/sub-folder/css/style.css" />
You could do
<link rel="stylesheet" href="<?php echo base_url('application/sub-folder/css/style.css'); ?>" />
I usually keep my assets in the root folder (where the main index.php file is) and use this asset library to load everything.
If you want to keep your assets in application/sub-folder you could do this:
I set up my CI projects like this:
application (my application files)
assets (publicly accessible css, js and images etc)
system (codeigniter core)
Typically, if I'm building in a CMS I'll put the CMS assets inside the application folder since they're basically private.