codeigniter assets handling trouble - codeigniter

i am having trouble about to link my css and js files on code igniter. i am new on codeigniter and need some help. i look for it but couldnt find a really working solution. my folder structure is like:
-> system
-> application
-> js
-> jquery.js
-> css
-> style.css
-> images
-> .htaccess
-> index.php
my base url is like http://subdomain.mydomain.com/ working on a sub domain
here is my .htaccess file for pass out /index.php/controller i m giving this because i dont know if it effect the issue.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteCond {REQUEST_FILENAME} !-f
RewriteCond {REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
and here is my header.php - i use a simple template structure, header content footer loading
<?php
$css = array(
'href' => 'css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'media' => 'screen'
);
echo link_tag($css);
?>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" />
<script src="<?php echo base_url(); ?>js/jquery.js" type="text/javascript"></script>
here i tried html helper for css and also i tried base_url()/css but didnt work both. and also for jquery lib didnt work. it returns on page source like
src="http://subdomain.mydomain.com/js/jquery.js"
when i click it, it must show the codes which inside of my file but it shows 404 not found. thanks for your help.

Remove this line and try once:
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
OR Use this instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Related

Putting Slash / in the URL in Codeigniter breaks the page style/images etc

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.

How to configure CakePHP 2.5.x for working with Ajax?

I'm working on localhost with xampp.
I installed the cakePHP application on C:\xampp\htdocs\mynewapp\ so I can access my CakePHP website via
http://localhost/mynewapp
I can access all my pages. It is working perfectly but I encounter some problems by making some Ajax calls.
This is my .htaccess configurations:
C:\xampp\htdocs\mynewapp.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /linagal/app/
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
C:\xampp\htdocs\mynewapp\app.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
C:\xampp\htdocs\mynewapp\app\webroot.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /mynewapp/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I created a view C:\xampp\htdocs\mynewapp\app\View\Photos\manage.ctp. On this file, I make an Ajax call
$this->Js->sortable(array(
'complete' => '$.post("/mynewapp/photos/reorder",
$("#sortableitems").sortable("serialize"))'));
My problem is that I have to put mynewapp in the path given to $.post to make it works. I would like to write $.post("/photos/reorder"; ...
Any idea how i can resolve this?
My problem is that I have to put mynewapp in the path given to $.post
This has nothing to do with configuring CakePHP (or the webserver) to work with AJAX - it's a simple logic problem. There are two obvious ways to deal with what's describing.
Use the router
Taken from the question, this is PHP code from a view file:
$this->Js->sortable(array(
'complete' => '$.post("/mynewapp/photos/reorder",
$("#sortableitems").sortable("serialize"))'));
As such, instead simply pass the url through the router first:
$url = $this->url('/photos/reorder');
$this->Js->sortable(array(
'complete' => '$.post("' . $url . '",
$("#sortableitems").sortable("serialize"))'));
This is in-keeping with writing Js using the Js helper but for anything beyond trivial js you may well find that that helper doesn't "Help" (in which case - don't use it).
Use a js variable
Alternatively create a variable that can be used as the base url and prefix all urls with that. There are many ways to do that, one is:
<?php
// default layout file
$app = array(
'root' => $this->url('/'),
// add anything else the app's js might need here
);
?>
<html>
<head>
...
<script>
var APP = <?= json_encode($app); ?>;
</script>
</head>
<body>
...
And then modify the js helper call to be:
$this->Js->sortable(array(
'complete' => '$.post(APP.root + "photos/reorder",
$("#sortableitems").sortable("serialize"))'));
Or similar.

Block direct link of an image but keep displaying it in pages

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

CodeIgniter form_open not hitting controller

Hi all I am new to CodeIgniter framework. I tried to develop a page which contains a form.
Here is the view
<?php echo form_open('Site/check');?>
User Name: <?php echo form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " ')); ?>
Add User:<?php echo form_submit('submit','Add User','class="btn btn-primary"');?>
<?php echo form_close();?>
This is the controller
<?php class Site extends CI_Controller{
function login() {
$data['records']="BHOOM!!!";
$this->load->view('login',$data);
}
function check() {
$this->load->view('showmessage');
}
}
?>
The showmessage is a PHP page with a hello in <h1> tags.
I already googled and searched for similar issues, but I didn't get any resolution
The base url is localhost/ci/index.php
Error
Unable to load the requested file: showmessage.php
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|js|css|img\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Here is a tutorial straight from EllisLab. Follow steps and you will get your form working.
The most common mistakes,
not loading form helper
htaccess. is set wrong, or paths are wrong (capital letters etc.).
config.php is not set properly
not passing data to view ($this->load->view('some_view', $data);
Hence you did not provide us error message if one exists there is no possible help from the community.
For more debug info please turn on codeigniters profiler ($this->output->enable_profiler(TRUE);), use PHPs native function var_dump($variable);.
Make sure error reporting is set to E_ALL in your index.php
edit
Make sure your /config/config.php
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";
make your .htaccess this file should be located in root of your project eg. /ci/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci/
# Reenable after getting ssl cert
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Removes access to the system folder by users
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# When your application folder isn't in the system folder This snippet prevents user access to the application folder
RewriteCond %{REQUEST_URI} ^application.*
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]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
just use this type of code for your trial
view
<?php
echo form_open('controllet_name/method_name');
form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " '));
form_close();
?>
controller
()
{
$name=$this->input->post('username');
echo $name ;
}
I think your RewriteRule is wrong.
Try:
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

Cannot target files in Code Igniter

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]

Resources