CodeIgniter paths messing up CSS background-image directive - php

So, I am working on a website with backend for two customers. Both will have different URLs. I had a problem with Javascript links (ajax calls using url:) but that was solved using a global :
var SiteURL='<?php echo base_url();?>';
and append it to calls as necessary. The problem is when my paths go deeper e.g. website.com/book/sheet/2
the relative links would continue from 2/
While I am done with the JS, I am really lost on the CSS. I don't want to give absolute path, e.g. for background-image:url() as it will change with new customers.
Any way I can make use of base_url() or any other function?

#DFriend: that somehow failed for mine as it looked for images folder in css...Firebug showed "http://localhost/samplestud/assets/css/images/listicons/arrow_double.png"
Simple "../images" seems to have fixed it pretty well. Thanks fellas.

Related

Highcharts showing in some pages, not showing in root pages

I included Highcharts using php.mysql,ajax in my website. it is running fine in pages with url structure "abc.com/xyz/" but not running un "abc.com/qwerty.php". All other jquery calls are done in header file which is constant throughout. What can be the problem? Any help will be highly appreciated.
Judging by your response to my comment, you are definitely having problems with relative URLs. Change your <script /> URLs to be absolute. For example:
<script type="text/javascript" src="/js/dxJ_uCBsir4yQMacwqm_6l.js"></script>
Notice the preceding / in the src there. If the scripts are not available from /js/... on your Web site, then adjust accordingly, e.g.:
<script ... src="/some/path/js/dxJ_uCBsir4yQMacwqm_6l.js"></script>
If you do this, you will not have problems referencing the scripts from paths of varying / depths, which is clearly the problem you're having based on the symptoms you described. :) Hope this helps.
You can try to change permission of that file.
Right Click that file and Change Permission as Read/view/Write.
I had same problem in CPanel. I done this one. Then it's working fine.
Just Try it.

Absolute paths in CakePHP project

i was given a pretty big cakePHP (built on v. 1.3.10) project to maintain. The problem is that the majority of the paths are absolute (which on my opinion is a bad habit).
Eg. in default.ctp there is:
<link rel="stylesheet" href="/css/public_new.css" />
but then at the bottom of the same file there is:
<?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?>
which prints the correct paths.
It's like the original developers made the site to put in a server's root (not in a subdirectory).
Things ive tried to solve this problem without success:
modified the .htaccess files on /, app/, and app/webroot
adding a tag
I know i can add a $this->base to the beginning of every path, but this is not a solution since there are thousands of files to modifiy :(
So my question: is there any solution using mod_rewrite or such?
Thanks in advance.
$html->script() will automatically prepend /js/ in the HTML output. Cake's .htaccess will then point these file calls to /webroot/js/.
So <?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?> would output
/js/jquery-ui-1.8.16.custom.min.js in the HTML.
To replace all those urls / links you could actually use a program like Actual Search and Replace ;-) I use it a lot.
Solution:
on app/config/boostrap.php, add:
Configure::write('App.base', '/teka_new/');
I take this out from Ho do I change the base path of routes in CakePHP?

wordpress image path changing dependent on file

I'm having problems with the paths to my first wordpress theme. Post images and stuff not related to css is located in wordpress_folder/blog-images/ and in index.php when I link to images I use this path: blog-images/img.jpg
The problem is now that when I want to link to the same image from another file (not index.php) in this case single.php wich displays one blog post, the correct path is now ../../../blog-images/img.jpg
This is causing problems in the includes like sidebar etc. sidebar.php works fine when called from index.php but the images path is changed if sidebar.php is called from single.php.
Does anyone know what's going on?
If you are creating these links from within php scripts, I would suggest using the site_url() function to get the URL for your wordpress install and then appending your images path to the end of that. If you are editing static theme files like css, then you should use /wordpress_folder/blog_images/img.jpg.
Something like <img src="<?php echo site_url() ?>/blog_images/img.jpg" /> should be sufficient from theme files.
The reason that paths are chaning is because if you are in wordpress_folder then the path blog_images/img.jpg resolves to wordpress_folder/blog_images/img.jpg but if you are on a post that has the url yoursite.com/wordpress_folder/2011/09/category/my_great_post then the path would resolve to wordpress_folder/2011/09/category/blog_images/img.jpg which is obviously incorrect.
For this reason you should try to use the absolute path or full URL so that no matter what file/folder/url you are linking from, the path will always be correct.
The main downside you may run into is that if you were to change the name of your wordpress folder, or remove it altogether, then you may need to make a lot of edits to reflect that. But in any case, you should put the / in front of your path so that it can be referenced the same from everywhere.
Also check out the site_url() reference page, it lists some other helpful functions at the bottom that may be useful to you.
I thought this was a little unclear from drew's answer, so I am adding a little bit more in a separate answer. His advice is sound and I agree with him.
If you prepend a url with a / then it will navigate based on your site url. Without the slash it uses relative navigation.
So here are some examples for www.mydomain.com
//always shows the image located at http://www.mydomain.com/myfolder/pic.png
//no matter what the url is
<img src="/myfolder/pic.png" />
//shows the image located relative to the current path
//if current url is http://www.mydomain.com/posts/ then the image will come from
//http://www.mydomain.com/posts/myfolder/pic.png
<img src="myfolder/pic.png" />
If you are creating links dynamically from php side then you will want to use site_url().
If you are creating links to your theme directory folder then you will want to use bloginfo('template_directory')

php website url matching question

i am new to a php site, only familiar with .net web forms sites.
i can't figure out how routing is working on this php site.
www.oursite.com/suggestions.php is to suggestions.php
www.oursite.com/suggestions also loads the php fine
www.oursite.com/suggestions/ loads the php, but no css is applied
www.oursite.com/suggestions/anything - anything that comes after the '/' is ignored and suggestions is loaded without css. so oursite.com/suggestions////// works, as does oursite.com/suggestions/2/2/2/2/whatever
i have searched but not found any good explanation on how this is working. can someone explain or provide a good resource?
thank you.
This is most certainly done using Mod_Rewrite, an Apache extension. You'll probably find a file called .htaccess in the public root, in which these rewriting rules are defined.
DouweM has the right answer as far as the friendly urls are concerned.
As for the CSS, it is probably because you are using relative URLs in your link tags:
<link rel="stylesheet" href="site.css"/>
Change those to absolute URLs and it should solve that problem:
<link rel="stylesheet" href="/site.css"/>
The reason for this is that the browser makes the request for the CSS based on the directory it thinks it is in, even though your URL rewriting is changing that. So, if the url is http://mysite.com/suggestions/ and you are using relative urls, the browser will request the css as http://mysite.com/suggestions/site.css which of course doesn't exist.
www.oursite.com/suggestions.php is to suggestions.php
www.oursite.com/suggestions also loads the php fine
You probably have a .htaccess file that first checks whether or not a file of that name exists, and if it does serves it, then, if it doesn't, tries to route it to a php script.
www.oursite.com/suggestions/ loads the php, but no css is applied
The / means your browser considers '/suggestions/' a directory. If suggestions.php outputs HTML that contains a relative <link> to a stylesheet, e.g. <link href="style.css">, your browser will request www.oursite.com/suggestions/style.css, rather than www.oursite.com/style.css as in the previous two cases.
www.oursite.com/suggestions/anything
Same as the previous case, your browser will request the wrong css file, since it considers '/suggestions/' a directory. (For a potential fix, take a look at Eric Petroelje's answer.)
As DouweM said, though, your best bet is to look directly at your .htaccess file and figure out what it does.

jQuery in CodeIgniter, inside the view or in external js file?

I'm developing web application using CodeIgniter. All this time, I put the custom js code to do fancy stuffs inside the view file. By doing this, I can use site_url() and base_url() function provided by CodeIgniter.
Today I want to separate all custom js code from view file into an external js file. Then it hit me, I cannot use site_url() and base_url() in the external js file. So, I had to move the js code back into view file.
I want to ask opinion, example, and best practice for this kind of problems. Do you put the custom js code inside the view, or in external js file? If you put it in external file, how do you get around the needs for site_url() and base_url() (beside of course put the absolute url that I want to avoid).
I typically keep mine in an external file, but place a single line within my template (view) that declares a javascript variable called "baseurl" that can later be used by my external javascript.
<script type="text/javascript">
var baseurl = "<?php print base_url(); ?>";
</script>
<script type="text/javascript" src="/js/scripts.js"></script>
Now my scripts.js file has access to the base_url() value via its own baseurl variable.
I would do it in a different way - js should be external, obviously, but why not take full advantage of the fact that you have an MVC framework that's perfectly suited to handle all of your javascript magic?
Here's my recipe for Javscript (and CSS) goodness with CI:
Grab a copy of Minify - if you don't know it already, your life will be better. Not in a "Love at first sight / I just discovered jQuery / xkcd / unit testing" kind of way, but at least in a "Dude, prepared statements eradicate SQL injection" kind of way.
Second, create a CI controller that encapsulates Minify (shouldn't be too hard, just remember to set the correct HTTP header and pass the parameters on)
Optionally activate caching to make everything run blazingly fast (Minify has caching built in, but if you're already caching your CI content, you might as well use the same method here.
Optionally define some groups for Minify, to make script loading even nicer
Optionally add the baseurl and siteurl variables (and whatever other values you may need) to the javascript output
And presto, you should now be able to load your scripts by calling the Minify-wrapper:
<script type="text/javascript" src="/min/g=js"></script>
It's crazy fast, it's gzipped, takes just one request rather than many, it gives you full CI control over your scripts, and it even makes your source code cleaner.
Oh, and if you want to be extra nice to your source-code-peeping visitors, you could automatically add something like this to the output:
// Javascript compressed using Minify by Ryan Grove and Steve Clay
// (http://code.google.com/p/minify/)
// Human-readable source files:
// http://www.yourdomain.com/js/core_functions.js
// http://www.yourdomain.com/js/interface.js
// http://www.yourdomain.com/js/newsticker.js
// http://www.yourdomain.com/js/more_magic.js
(...)
At least that's what I do.
Donny, if you start passing through every URL separately you will just be giving yourself a ball-ache. Why not just pass through base_url() and contcat the controller/method on the end?
You lose the ability to swap around index_page and url_suffix settings but they shouldnt really change all that often anyway.

Categories