Welcome Guest, Not a member yet? Register   Sign In
CI HMVC index.php and img/css/js paths
#1

[eluser]neoblasted[/eluser]
Hi,
I just started to play with Ci and I have a question about IMG/CSS/JS relatives path when a user will use the

URL http:\\site\CI\index.php\welcome (Please note that the welcome controller is in the modules/welcome folder.

all the images/CSS/JS path are relative this way "css/intranet.css" (CSS Folder is at the level of the application and system folder.

When I browse this url all the relatives path are wrongs because it add the index.php in the paths (when checking the image properties in the webbrowser). So i did the HTACESS REWRITE mod to be able to call a method using only
HTTP:\\SITE\CI\welcome without the index.php in the url and it works great.

But my problem is that the user still can navigate to http:\\SITE\CI\index.php\welcome and of course all the IMG/CSS/JS path will be wrong.

So is there Someone that can explain to me what Im doing wrong or a better way so even if I use the index.php in the url the images path will be correct ?
#2

[eluser]Twisted1919[/eluser]
First of all, who told you to access the website at one address like: http:\\www.site.com\index.php\something ?
Are you aware that this is wrong from so many points of view ?
Don't confuse the windows vs linux directory separator.
In linux, the directory separator is / while in windows it is \ (though, giving the / for windows is working)
In php, there is a constant named DIRECTORY_SEPARATOR which will put the directory separator correctly depending on the OS the server is running.
BUT, all of this is doable only for system PATHS, NOT for HTTP url's.

The http protocol only uses the / (forward slash), so you will ALWAYS construct a url like:
Code:
echo site_url('controller/method/param1/param2');//generating http://site.com/controller/method/param1/param2
Same deal for images/css/js:
Code:
echo base_url().'css/style.css'; // http://site.com/css/style.css
echo base_url().'js/script.js'; // http://site.com/js/script.js
echo base_url().'image/dummy.png'; // http://site.com/images/dummy.png

Just remember that the directory separator only matters when you play with the file system to retrieve/write/delete/move/copy/rename files using PHP, and in this case you will use the constant i told you about.
When you create a url, you will always use the forward slash (/).
#3

[eluser]neoblasted[/eluser]
Hi, thanks for your replay

first of all, yeah im using the "/" not the "\" it was an error.

Also do I really need to use the base_url() function for the paths ?

and my real problem is that if the url entered in the browser is http://site/ci/index.php/welcome, then the image path are wrong, because if check the properties of the img, it try to call the image with the url http://site/ci/index.php/img/menuSettings.gif. this url is wrong because index.php is not a directory.

But if I use HTTP://site/ci/welcome, then the path is ok. (http://site/ci/img/right.gif).
#4

[eluser]KarlBallard[/eluser]
[quote author="Twisted1919" date="1306281203"]
Code:
echo base_url().'css/style.css'; // http://site.com/css/style.css
echo base_url().'js/script.js'; // http://site.com/js/script.js
echo base_url().'image/dummy.png'; // http://site.com/images/dummy.png
[/quote]

Why not use the html BASE tag?
#5

[eluser]InsiteFX[/eluser]
Code:
<base href="<?php echo base_url();?>">
If your images are in a css file then you would access them like so
../img or ../../img depending on there scope.

For HMVC I have my setup like this:
Code:
application
system
index.php

assets
  -- css
  -- images
  -- img
  -- icons
  -- js
  -- scripts
  -- media
  -- xml


Then in your .htaccess add this or HMVC will not find your assets direcory:
[code]
RewriteCond $1 !^(index\.php|assets|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]

InsiteFX
#6

[eluser]neoblasted[/eluser]
Thanks for your Reply InsiteFX,

By adding the line <base href="<?php echo base_url();?> in the header of the main Html file.

I no longer need to add "<?php echo base_url();?>" in all the IMG/CSS/JS paths.

Thanks a lot again!
#7

[eluser]InsiteFX[/eluser]
Here is another tip that I just found out, I installed a new CI and HMVC setup! Testing it I clicked on the user_guide in the view and guess what 404 Error!

The fix: add the user_guide to your htaccess file.
Code:
RewriteCond $1 !^(index\.php|assets|user_guide|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

InsiteFX
#8

[eluser]Agustín Villalba[/eluser]
To know how to install HMVC you can read HMVC: Aplicaciones modulares en CodeIgniter




Theme © iAndrew 2016 - Forum software by © MyBB