Monday, January 31, 2011

Re: $html->image no show

On Mon, Jan 31, 2011 at 8:27 AM, ugarch <ugarch74@gmail.com> wrote:
> Hello, (I am new in CakePHP)
>
> I setup a cake project on my local. http://localhost/~Alejandro/Alvaro/
> I'm running MacOs Snow Leopard.
>
> I already set my Alejandro.conf:
>
> DocumentRoot "/Users/Alejandro/Sites/"
> <Directory "/Users/Alejandro/Sites/">
>    Options Indexes MultiViews FollowSymlinks
>    AllowOverride all
>    Order allow,deny
>    Allow from all
> </Directory>
>
> also changed the .htaccess in the project directory for /.htaccess,
>
> <IfModule mod_rewrite.c>
>   RewriteEngine on
>   RewriteBase /~Alejandro/Alvaro/
>   RewriteRule    ^$ app/webroot/    [L]
>   RewriteRule    (.*) app/webroot/$1 [L]
> </IfModule>
>
> app/.htaccess,
>
> <IfModule mod_rewrite.c>
>    RewriteEngine on
>    RewriteBase /~Alejandro/Alvaro/
>    RewriteRule    ^$    webroot/    [L]
>    RewriteRule    (.*) webroot/$1    [L]
>  </IfModule>
>
>
> and app/webroot/htaccess,
>
> <IfModule mod_rewrite.c>
>    RewriteEngine On
>    RewriteBase /~Alejandro/Alvaro/
>    RewriteCond %{REQUEST_FILENAME} !-d
>    RewriteCond %{REQUEST_FILENAME} !-f
>    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
> </IfModule>
>
> I did not make any changes in the app/webroot/index.php. However in
> cake/config/paths.php I placed the
>
>        if (!defined('WEBROOT_DIR')) {
>                define('WEBROOT_DIR', 'app'.DS.'webroot');
>        }
>
> Now ... everything is working well in the main root page
> (controller=>"page", action=>''display', 'home'),
> In the default.ctp I have a set of links like:
>
> echo $html->link('Areas Comunes', array('controller' =>
> 'informations', 'action' => 'area'), array('class'=>'navigation',
> 'escape'=>false));
>
> and also a banner div that displays images with respect to the current
> page, and I do it like this:
>
> <div id="banner">
>        <?php echo $banner ?>
> </div>
>
> The $banner is set in the controllers, so If we are in the main root
> page, I set $ banner in pages/display in the following way:
>
> App::import('Helper', 'Html');
> $html = new HtmlHelper();
> $banner =  $html->image('ubicacion.jpg',
> array('class'=>'banner','title'=>"Foo"));
> $this->set(compact('banner'));
>
> I do exactly the same for the informations/area but once I clicked in
> a link in default.ctp like:
>
> echo $html->link('Areas Comunes', array('controller' =>
> 'informations', 'action' => 'area'), array('class'=>'navigation',
> 'escape'=>false));
>
> (HERE COMES THE PROBLEM) the image I set in banner is no show for
> (informations/area).
> However if in firebug I modify the image link from "img/banner.jpg" to
> "../img/banner.jpg" WORKS!
> Nevertheless the $banner for the main page (page/display) works fine
> and displays the image.
>
> <div id="banner">
>        <?php echo $banner ?>
> </div>
>
> I tried almost everything, but I don't know what I did not tried cause
> I cannot make it work. I would really appreciate any help.
>

I'm not sure I'm following all that, but a couple of points:

As you have the ability to create the virtualhost config, you should
disable .htaccess and place the mod_rewrite stuff in the VHost config.
This will speed up requests. Set AllowOverride to None, and place
this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~Alejandro/Alvaro/
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

... inside the Directory block.

Next, you have DocumentRoot "/Users/Alejandro/Sites/" but your
mod_rewrite rule points to "/~Alejandro/Alvaro/". I've never dealt
with setting up UserDir for Cake, so I'm not sure what to say about
this. But it looks wrong to me.

The other thing is that you're importing HtmlHelper inside your
controller and creating the img tag there. This is unnecessary. You'd
be better off creating a banner.ctp element. In the controller action:

$this->set('banner_img', 'ubicacion.jpg');

In the layout:

$this->element('banner');

app/views/elements/banner.ctp:

$html->image('img/banners/'.$banner_img, array('class'=>'banner',
'title'=>"Foo"));

You probably don't really need the 'class'=>'banner', either, as you
could wrap the img tag with <div id="banner"></div> and refer to it in
your stylesheet as #banner img. That's how I'd do it, anyway.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: