0

I am new in cakephp, and I have a script, which I converted to cakephp. I have there a rotate background that use 4 images like this:

$.backstretch([
    "img/bg/1.jpg",
    "img/bg/2.jpg",
    "img/bg/3.jpg",
    "img/bg/4.jpg"
], {
     fade: 1000,
     duration: 8000
});

When I open script like name_domain.com with use controller users and action login, it see all that images and all work perfect, but when I try to use register (controller=>users, action=>signup) the images are not load. But I put ../img.... then is working here and not in index.

How can I link correctly those images. I also try with "/img" but not working.

Any help will be appreciated.

4 Answers 4

1

declare a javascript variable in default.ctp

var IMAGE_HOST = "<?php echo $this->webroot.'/img/'; ?>";

and now you can use it in javascript file too

<script>
$.backstretch([
    IMAGE_HOST + "bg/1.jpg",
    IMAGE_HOST + "bg/2.jpg",
    IMAGE_HOST + "bg/3.jpg",
    IMAGE_HOST + "bg/4.jpg"
], {
     fade: 1000,
     duration: 8000
});

hope it helps..!!

Sign up to request clarification or add additional context in comments.

2 Comments

That is what i this to solve the problem, thank you for your reply:)
Great.. Kindly don't forget to accept post as answer.. Enjoy
1

try to use full path something like this.

$this->webroot.'img/myimage.jpg'

OR

$this->Html->url('/img/myimage.jpg')

OR

<?php Router::url('/img/myimage.jpg')?>

4 Comments

Hey, thanks, but this will not work in javascript file. I need something like that, only that i have to include in a javascript file.
you may need to copy js part in .ctp file to make it workable
@Essteffan, I have updated my answer you can also try 3rd option as per the above answer. I feel it will solve your issue.
Thank you Ram, i've combine your answoe and Martin's answer and it's working. Best regards
1

One method is to set a JavaScript variable, and then use that in your subsequent scripts. For example, in your app/Layouts/default.ctp file, you could have:

<script>
    var img_path = "<?php echo Configure::read('App.imageBaseUrl'); ?>";
</script>

And then use it in your scripts like this:

<script>
    $.backstretch([
        img_path + "/bg/1.jpg",
        img_path + "/bg/2.jpg",
        img_path + "/bg/3.jpg",
        img_path + "/bg/4.jpg"
    ], {
         fade: 1000,
         duration: 8000
    });
</script>

2 Comments

That's a great solution, but i have a small problem. When i am on root (index) is working, but when i'm on /users/signup is add users in link like so: name_domain/users/img/bg/... It's working with Ram solution, like so var img_path = "<?php echo $this->webroot ?>";
I missed that App.imageBaseUrl only returns a relative URL. You’ll need to combine it with a reference to your site’s base URL. Something like $this->webroot . Configure::read('App.imageBaseUrl').
1

I would recommend to save webroot path in your layout so that its scope remain global

<div id="webroot" class="hide" data-url="<?php echo $this->webroot;?>"></div>

Then use it backstretch js as

 var webroot = $('#webroot').attr('data-url');
    $.backstretch([
        webroot+ "img/bg/1.jpg",
        webroot+ "img/bg/2.jpg",
        webroot+ "img/bg/3.jpg",
        webroot+ img/bg/4.jpg"
        ], {
          fade: 1000,
          duration: 8000
});

When you use different action having same layout with webroot element, it will keep the correct image path.

2 Comments

I already did it, but that's another approach, if anyone have the same problem. Thanks you
I was suggesting this because this is more generic approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.