wrong image and css files paths in view files |
[eluser]cb951303[/eluser]
hello there everyone. I have a simple main.php in my view folder like this: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" Controller cmain.php looks like this (it's also configured as the default controller): Code: <?php Here's how my http server root looks like: Code: public_html/ Here is my problem: When I go to the address: http://localhost/ or http://localhost/index.php everything is fine. The page loads and renders normally. But when I go to the address http://localhost/index.php/ or http://localhost/index.php/cmain/index, the page loads but the image and styling doesn't show (my guess is it can't find logo.png and test.css) I tried to move image and css files to view and controller directories but no luck. Any help would be appreciated Thanks in advance
[eluser]Sarfaraz Momin[/eluser]
try putting a starting slash to all ur css and image file paths Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
[eluser]cb951303[/eluser]
[quote author="Sarfaraz Momin" date="1220770469"]try putting a starting slash to all ur css and image file paths Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" thanks for quick reply it made it worst. now none of the address combinations work.
[eluser]charlie spider[/eluser]
if you are removing the index.php with an .htaccess file, then make sure your images directory is allowed (depending on what style of .htaccess you use). for example: RewriteEngine on RewriteCond $1 !^(index\.php) RewriteRule ^(.*)$ /index.php?/$1 [L] will not allow access to your images folder whereas: RewriteEngine on RewriteCond $1 !^(index\.php|flash|icons|images|javascript|style|uploads|favicon\.ico) RewriteRule ^(.*)$ /index.php?/$1 [L] will allow access
[eluser]esra[/eluser]
Most of my applications also need to run under other servers such as IIS, so I try to avoid using .htaccess whenever possible. My solution is a bit more complex, but more controllable. I modified index.php to load a defines.php file containing additional application constants. The changes look like so: Code: /* I store defines.php in the extensions/ directory at the same level as the system/ directory. A sample defines.php is so: Code: <?php The ASSETPATH is of interest in your case. When your application loads, all paths are relative to the location where index.php is stored, so you can take advantage of this and use a helper to handle paths in your main.php or other templates you might use. In my case, I made a copy of the url helper, renamed it to link_helper.php and stored it under application/helpers/, then added some additional functions to insert absolute urls into my templates. All functions were based on the base_url() function, appended with a constant. For example: // ------------------------------------------------------------------------ Code: /** What the above does is pull the base_url config variable from config.php and append the specific path from defines.php to create an absolute url to the asset. You need to autoload the helper or load it in a controller. You can create a directory at any location under any name in a directory under the index.php location based on whatever directory paths you define in defines.php. In my case, I made a assets/ directory with the subdirectories assets/js/, assets/css/ and assets/images/. Then in your templates, you can add css, javascript, and images by inserting <?=asset_url();?> in the various template paths, followed by js/, css/, or images/ plus whatever file you need to load. The added benefit of this approach is that you can create defines for any special paths you might need for a CI application and use those in your templates and controllers. For example, I use separate directories for storing templates, modules, and blocks (view fragments or partials). The approach might seem a bit complex for some people, but I create the setup once for each new CI version and reuse the setup as the starting point for all of my projects.
[eluser]cb951303[/eluser]
[quote author="charlie spider" date="1220780877"]if you are removing the index.php with an .htaccess file, then make sure your images directory is allowed (depending on what style of .htaccess you use). for example: RewriteEngine on RewriteCond $1 !^(index\.php) RewriteRule ^(.*)$ /index.php?/$1 [L] will not allow access to your images folder whereas: RewriteEngine on RewriteCond $1 !^(index\.php|flash|icons|images|javascript|style|uploads|favicon\.ico) RewriteRule ^(.*)$ /index.php?/$1 [L] will allow access[/quote] unfortunately I'm not using .htaccess :/
[eluser]cb951303[/eluser]
[quote author="esra" date="1220796225"]Most of my applications also need to run under other servers such as IIS, so I try to avoid using .htaccess whenever possible. My solution is a bit more complex, but more controllable. I modified index.php to load a defines.php file containing additional application constants. The changes look like ....... [/quote] I see how this works. I'll use it unless anyone have anything more elegant. I feel like it should have very simple solution thanks very much
[eluser]esra[/eluser]
There are other solutions mentioned in the Ignited Code forum (AssetLib and a few others) and elsewhere in other forums. If you do a search for assets_helper, you will find a helper and config file to use as a similar asset-only solution. However, if your application projects range from simple to complex like mine do, the above approach is probably more useful and versatile. For me, I save a lot of time by building a base CI installation with a collection of basic extensions to reuse in all of my projects.
[eluser]cb951303[/eluser]
[quote author="esra" date="1220801057"]There are other solutions mentioned in the Ignited Code forum (AssetLib and a few others) and elsewhere in other forums. If you do a search for assets_helper, you will find a helper and config file to use as a similar asset-only solution. However, if your application projects range from simple to complex like mine do, the above approach is probably more useful and versatile. For me, I save a lot of time by building a base CI installation with a collection of basic extensions to reuse in all of my projects.[/quote] thanks for the info. I'll go that way
[eluser]Sumon[/eluser]
I am not sure my approach is standard or not. But i use it without getting any trouble. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" Only add base url before any css or image location. this produce absolute path and doesn't create any problem for me. |
Welcome Guest, Not a member yet? Register Sign In |